看了别的提问,还是拿不到数据库的值
看了别的提问,能改的都试过了 就是拿不到值。
数据库 密码用户名 都确认了 XML里也是先到home.jsp再跳转
不知道怎么找错了 输出信息里 也没有SQL异常和报错
正在回答 回答被采纳积分+1
SessionFactoryUtils
package com.wyh.utils; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException; import java.io.InputStream; public class SqlSessionFactoryUtils { private static String RESOURCE ="mybatis-config.xml"; private static SqlSessionFactory sqlSessionFactory; private static ThreadLocal<SqlSession> threadLocal = new ThreadLocal<SqlSession>(); // 创建一个初始化SqlSessionFactory的方法 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); } } }
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> </environments> <mappers> <mapper resource="mapper/usersMapper.xml"/> </mappers> </configuration>
db.properties 因为我的mysql 用户名root 密码 111111 所以做了如下更改
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/mydb username=root password=111111
index.jsp
<%-- Created by IntelliJ IDEA. User: Administrator Date: 2018/5/7/007 Time: 17:59 To change this template use File | Settings | File Templates. --%> <%@ 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">查看更多</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> </tr> <c:forEach var="user" items="${userList}"> <tr> <td>123</td> <td>213</td> <td>1</td> <td>1</td> <td>4</td> <td>4</td> <td>314</td> <td>5r23</td> <td>213</td> <td>132</td> </tr> </c:forEach> </table> </div> </div> </body> </html>
只要加了 <c:forEach var="user" items="${userList}"> 我的底下的 <td>标签里的测试数据就会消失 所以我感觉这是没有从数据库取到数据的缘故
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属性用来配置命名空间,主要进行session级别的缓存管理 命名空间默认情况下,使用我们当前操作的实体类的全路径 --> <mapper namespace="com.wyh.entity.Users"> <select id="findAll" resultType="com.wyh.entity.Users"> select * FROM USERS; </select> </mapper> UsersFindServlet.java package com.wyh.utils; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException; import java.io.InputStream; public class SqlSessionFactoryUtils { private static String RESOURCE ="mybatis-config.xml"; private static SqlSessionFactory sqlSessionFactory; private static ThreadLocal<SqlSession> threadLocal = new ThreadLocal<SqlSession>(); // 创建一个初始化SqlSessionFactory的方法 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); } } }
UsersDao.java
package com.wyh.dao;
import com.wyh.entity.Users;
import com.wyh.utils.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import java.util.List;
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;
}
}
- 参与学习 人
- 提交作业 277 份
- 解答问题 4297 个
Java数据库开发的必备技能,从流行的MySQL数据库开始,到Java原生的数据库管理接口JDBC的使用,再到常用的数据持久化框架MyBatis,让你向Java工程师的目标又迈进了一步!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星