无法获取到sql的数据,请老师指点。
/mybatisDemo1/src/main/resources/mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="db.properties"></properties>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
<environment id="product">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
<environment id="test">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/usersMapper.xml"/>
</mappers>
</configuration>/mybatisDemo1/pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.rhm.mb</groupId> <artifactId>mybatisDemo1</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>mybatisDemo1 Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.13</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> </dependencies> <build> <finalName>mybatisDemo1</finalName> </build> </project>
/mybatisDemo1/src/main/resources/mapper/usersMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.rhm.entity.Users"> <select id="findAll" resultType="com.rhm.entity.Users"> select * from users </select> </mapper>
/mybatisDemo1/src/main/java/com/rhm/entity/Users.java
package com.rhm.entity;
import java.util.Date;
public class Users {
private Integer id;
private String username;
private String userpass;
private String nickname;
private Integer age;
private String gender;
private String phone;
private String email;
private Date createTime;
private Date updateTime;
private Date lastLogin;
private Integer userStatus;
private String remark;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserpass() {
return userpass;
}
public void setUserpass(String userpass) {
this.userpass = userpass;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
public Date getLastLogin() {
return lastLogin;
}
public void setLastLogin(Date lastLogin) {
this.lastLogin = lastLogin;
}
public Integer getUserStatus() {
return userStatus;
}
public void setUserStatus(Integer userStatus) {
this.userStatus = userStatus;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
}/mybatisDemo1/src/main/java/com/rhm/listener/InitSqlSessionListener.java
package com.rhm.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import com.rhm.utils.SqlSessionFactoryUtils;
@WebListener
public class InitSqlSessionListener implements ServletContextListener{
public void contextInitialized(ServletContextEvent sce) {
System.out.println("容器加载中。。。");
// 初始化SqlSessionFactory对象
SqlSessionFactoryUtils.initSqlSessionFactory();
}
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("容器销毁中。。。");
// 关闭SqlSessionFactory对象
SqlSessionFactoryUtils.close();
}
}/mybatisDemo1/src/main/java/com/rhm/utils/SqlSessionFactoryUtils.java
package com.rhm.utils;
import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class SqlSessionFactoryUtils {
private static String RESOURCE = "mybatis-config.xml";
private static SqlSessionFactory sqlSessionFactory;
private static ThreadLocal<SqlSession> threadLocal = new ThreadLocal<SqlSession>();
public static void initSqlSessionFactory() {
try {
InputStream is = Resources.getResourceAsStream(RESOURCE);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSessionFactory getSqlSessionFactory() {
return sqlSessionFactory;
}
public static void close() {
SqlSession session = threadLocal.get();
if (session != null) {
session.close();
threadLocal.set(null);
}
}
}/mybatisDemo1/src/main/java/com/rhm/dao/UsersDAO.java
package com.rhm.dao;
import com.rhm.entity.Users;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import com.rhm.utils.SqlSessionFactoryUtils;
public class UsersDAO {
private SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSessionFactory().openSession();
private List<Users> list;
public List<Users> findAll() {
try {
list = sqlSession.selectList("findAll");
} catch (Exception e) {
e.printStackTrace();
} finally {
sqlSession.close();
}
return list;
}
}/mybatisDemo1/src/main/java/com/rhm/servlet/UsersFindServlet.java
package com.rhm.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.rhm.dao.UsersDAO;
import com.rhm.entity.Users;
@WebServlet("/index")
public class UsersFindServlet extends HttpServlet {
private UsersDAO usersDAO = new UsersDAO();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List<Users> list = usersDAO.findAll();
req.setAttribute("usersList", list);
req.getRequestDispatcher("index.jsp").forward(req, resp);
}
}/mybatisDemo1/src/main/webapp/home.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title></title>
</head>
<body>
<%
response.sendRedirect("/index");
%>
</body>
</html>/mybatisDemo1/src/main/webapp/index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>用户管理中心</title>
<link rel="stylesheet" href="lib/bootstrap-3.3.7-dist/css/bootstrap.min.css">
<script src="lib/2.2.4/jquery-1.12.4.min.js"></script>
<script src="lib/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<div class="page-header">
<h1>后台管理系统 <small>用户数据管理中心</small></h1>
</div>
</div>
<div class="row">
<div class="jumbotron">
<h1>MyBatis基础入门!</h1>
<p>通过一个项目来完成基础部门的学习</p>
<p><a class="btn btn-primary btn-lg" href="#" role="button">more</a></p>
</div>
</div>
<div class="row">
<table class="table table-hover table-striped">
<tr>
<th>用户编号</th>
<th>登录账号</th>
<th>用户昵称</th>
<th>用户年龄</th>
<th>邮箱</th>
<th>联系方式</th>
<th>账号创建时间</th>
<th>用户状态</th>
<th>操作</th>
</tr>
<c:forEach var="user" items="${usersList}">
<tr>
<td>${user.id}</td>
<td>${user.username}</td>
<td>${user.nickname}</td>
<td>${user.email}</td>
<td>${user.phone}</td>
<td>${user.createTime}</td>
<c:if test="${user.userStatus == 0}">
<td>正常</td>
</c:if>
<c:if test="${user.userStatus == 1}">
<td>锁定</td>
</c:if>
<c:if test="${user.userStatus == 2}">
<td>删除</td>
</c:if>
<td>
<a href="">查看</a>
<a href="">修改</a>
<a href="">删除</a>
</td>
</tr>
</c:forEach>
</table>
</div>
</div>
</body>
</html>/mybatisDemo1/src/main/webapp/WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>mybatisDemo1</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>home.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
所有的代码都在这里了,就是无法获取到sql的数据,请老师指点。排查了2天了。
正在回答 回答被采纳积分+1
你可以试一下不用db.properties,直接在mybatis-config.xml中写driver,url,username,password,
我遇到的问题和你一样,刚开始从db.properties中获取,后台打印list为null,改过来之后可以获取到值了。
从同学贴的信息开看,没有报错,
同学是指访问http://localhost:8080/index 报错么,报的是404错误是么?
1、我想先测试一下有没有进入UsersFindServlet,可以在doget或者dopust方法中写个输出语句,看看是否访问到了。
2、我们测试一下是否拿到了数据,请同学在UsersFindServlet的
List<Users> list = usersDAO.findAll(); 这句代码的后边打印一下list。看看是没有从数据库中拿到数据还是前台页面显示的问题。
祝学习愉快。
十二月 13, 2018 7:11:15 下午 org.apache.tomcat.util.digester.SetPropertiesRule begin
警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.j2ee.server:mybatisDemo1' did not find a matching property.
十二月 13, 2018 7:11:15 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Server version: Apache Tomcat/9.0.12
十二月 13, 2018 7:11:15 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Server built: Sep 4 2018 22:13:41 UTC
十二月 13, 2018 7:11:15 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Server number: 9.0.12.0
十二月 13, 2018 7:11:15 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: OS Name: Mac OS X
十二月 13, 2018 7:11:15 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: OS Version: 10.12.6
十二月 13, 2018 7:11:15 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Architecture: x86_64
十二月 13, 2018 7:11:15 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Java Home: /Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre
十二月 13, 2018 7:11:15 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: JVM Version: 1.8.0_171-b11
十二月 13, 2018 7:11:15 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: JVM Vendor: Oracle Corporation
十二月 13, 2018 7:11:15 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: CATALINA_BASE: /Users/RHM/RHMJava/eclipse-workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0
十二月 13, 2018 7:11:15 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: CATALINA_HOME: /Users/RHM/apache-tomcat-9.0.12
十二月 13, 2018 7:11:15 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Dcatalina.base=/Users/RHM/RHMJava/eclipse-workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0
十二月 13, 2018 7:11:15 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Dcatalina.home=/Users/RHM/apache-tomcat-9.0.12
十二月 13, 2018 7:11:15 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Dwtp.deploy=/Users/RHM/RHMJava/eclipse-workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps
十二月 13, 2018 7:11:15 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Djava.endorsed.dirs=/Users/RHM/apache-tomcat-9.0.12/endorsed
十二月 13, 2018 7:11:15 下午 org.apache.catalina.startup.VersionLoggerListener log
信息: Command line argument: -Dfile.encoding=UTF-8
十二月 13, 2018 7:11:15 下午 org.apache.catalina.core.AprLifecycleListener lifecycleEvent
信息: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/Users/RHM/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.]
十二月 13, 2018 7:11:15 下午 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["http-nio-8080"]
十二月 13, 2018 7:11:15 下午 org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
信息: Using a shared selector for servlet write/read
十二月 13, 2018 7:11:15 下午 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["ajp-nio-8009"]
十二月 13, 2018 7:11:15 下午 org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
信息: Using a shared selector for servlet write/read
十二月 13, 2018 7:11:15 下午 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 1283 ms
十二月 13, 2018 7:11:16 下午 org.apache.catalina.core.StandardService startInternal
信息: Starting service [Catalina]
十二月 13, 2018 7:11:16 下午 org.apache.catalina.core.StandardEngine startInternal
信息: Starting Servlet Engine: Apache Tomcat/9.0.12
十二月 13, 2018 7:11:20 下午 org.apache.jasper.servlet.TldScanner scanJars
信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
十二月 13, 2018 7:11:24 下午 org.apache.jasper.servlet.TldScanner scanJars
信息: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
容器加载中。。。
十二月 13, 2018 7:11:25 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-nio-8080"]
十二月 13, 2018 7:11:25 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["ajp-nio-8009"]
十二月 13, 2018 7:11:25 下午 org.apache.catalina.startup.Catalina start
信息: Server startup in 9359 msmysql的xml文件配置
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis"/> <property name="username" value="root"/> <property name="password" value="*******"/> </dataSource> </environment> </environments> <mappers> <!--<mapper resource="org/mybatis/example/BlogMapper.xml"/>--> </mappers> <
你的mysql的XML文件没有配置吗?
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="db.properties"></properties>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
<environment id="product">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
<environment id="test">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/usersMapper.xml"/>
</mappers>
</configuration>- 参与学习 人
- 提交作业 277 份
- 解答问题 4297 个
Java数据库开发的必备技能,从流行的MySQL数据库开始,到Java原生的数据库管理接口JDBC的使用,再到常用的数据持久化框架MyBatis,让你向Java工程师的目标又迈进了一步!
了解课程


恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星