查不到数据,值为null,怀疑是数据库没连接上,用的是mysql8.0.17的
driver=com.mysql.cj.jdbc.Driver url=jdbc:mysql://localhost:3306/mydb?useSSL=false&serverTimezone=UTC username=root password=root package com.imooc.entity; import java.util.Date; import java.util.List; /** * Created by mouwe on 2017/5/17. * resource:mybatis-config.xml * InputStream * SqlSessionFactory * SqlSession * 执行我们的配置好的SQL语句 */ public class Users { private Integer id; // 用户编号 // private String username; // 登录账号 private String name; // 登录账号 private String userpass; // 登录密码 private String nickname; // 用户昵称 private Integer age; // 用户年龄 private String gender; // 用户性别 private String email; // 用户邮箱 private String phone; // 联系方式 private Date createTime; // 创建时间 private Date updateTime; // 账号最后修改时间 private Date lastLogin; // 用户最后登录时间 private Integer userStatus; // 用户账号状态 0 正常 1 锁定 2 删除 private String remark; // 用户备注信息 private List<Address> addresses; // 用户地址信息【地址列表,有一个是默认地址】 public Users() { } public Users(Integer id) { this.id = id; } public Users(String name, String userpass, String nickname, Integer age, String gender, String email, String phone) { this.name = name; this.userpass = userpass; this.nickname = nickname; this.age = age; this.gender = gender; this.email = email; this.phone = phone; } public Users(Integer id, String nickname, Integer age, String gender, String email, String phone, Date updateTime, String remark) { this.id = id; this.nickname = nickname; this.age = age; this.gender = gender; this.email = email; this.phone = phone; this.updateTime = updateTime; this.remark = remark; } public Users(String name, String userpass, String nickname, Integer age, String gender, String email, String phone, Date createTime, Date updateTime, Date lastLogin, Integer userStatus) { this.name = name; this.userpass = userpass; this.nickname = nickname; this.age = age; this.gender = gender; this.email = email; this.phone = phone; this.createTime = createTime; this.updateTime = updateTime; this.lastLogin = lastLogin; this.userStatus = userStatus; } public Users(Integer id, String nickname, Integer age, String gender, String email, String phone, String remark) { this.id = id; this.nickname = nickname; this.age = age; this.gender = gender; this.email = email; this.phone = phone; this.remark = 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 getName() { return name; } public void setName(String name) { this.name = name; } 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; } public List<Address> getAddresses() { return addresses; } public void setAddresses(List<Address> addresses) { this.addresses = addresses; } @Override public String toString() { return "Users{" + "id=" + id + ", name='" + name + '\'' + ", userpass='" + userpass + '\'' + ", nickname='" + nickname + '\'' + ", age=" + age + ", gender='" + gender + '\'' + ", phone='" + phone + '\'' + ", email='" + email + '\'' + ", createTime=" + createTime + ", updateTime=" + updateTime + ", lastLogin=" + lastLogin + ", userStatus=" + userStatus + ", remark='" + remark + '\'' + ", addresses=" + addresses + '}'; } } /* Users{id=1, name='damu', userpass='damu', nickname='大慕', age=21, gender='男', phone='189@163.com', email='18989898989', createTime=Tue May 16 00:00:00 CST 2017, updateTime=Thu Jun 01 20:05:39 CST 2017, lastLogin=null, userStatus=1, remark='慕课大牧', addresses=[Address{id=1, user=null, isDefault=true, province='河南', city='郑州', country='金水区', street='二环路', remark='瑞隆城7号楼601'}, Address{id=2, user=null, isDefault=false, province='陕西', city='西安', country='北城区', street='开元路', remark='文景小区1号楼202'}, Address{id=3, user=null, isDefault=false, province='云南', city='昆明', country='盘龙区', street='小康大道', remark='银河星辰3号楼402'}]} */ <mapper namespace="com.imooc.entity.Users"> <select id="findAll" resultType="com.imooc.entity.Users"> select * from users </select> </mapper> <?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配置,用于加载外部properties配置文件 --> <properties resource="db.properties"></properties> <!-- environments 主要用于进行数据源的配置 可以配置多个数据源~ 通过default属性来指定当前项目运行过程中使用的是哪个数据源 --> <environments default="development"> <!-- environment 用于配置一个具体的独立的数据源 id属性用于给当前数据源定义一个名称,方便我们的项目指定 --> <environment id="development"> <!-- transactionManager用于配置事务管理,默认情况下使用的是JDBC事务管理 --> <transactionManager type="JDBC"/> <!--使用数据库连接池, dataSource具体数据源的链接信息;type属性用于指定是否使用连接池--> <dataSource type="POOLED"> <property name="driver" value="${dirver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <!-- 映射配置文件的路径--> <!-- mappers主要用于配置我们外部的映射配置文件 在主配置文件中需要引入加载映射配置文件 --> <mappers> <!-- mapper主要配置引入某一个具体的映射文件,resource进行路径方式的引入 --> <!--<mapper resource="mapper/usersMapper.xml"/>--> <mapper resource="mapper/usersMapper.xml"/> </mappers> </configuration> package com.imooc.listener; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener; import com.imooc.util.SqlSessionFactoryUtils; //之后需要添加监听器配置,才能监听到容器的启动和销毁 @WebListener //这样监听器就被注册到容器中了 public class InitSqlSessionListener implements ServletContextListener{ //必须让当前的监听器实现servletContextListener //才能监听到容器的启动和销毁 public void contextInitialized(ServletContextEvent sce) { System.out.println("容器加载中..."); //初始化我们的SqlSessionFactory对象 SqlSessionFactoryUtils.initSqlSessionFactry(); } private void contextDestoryed(ServletContextEvent sce) { System.out.println("容器销毁中..."); // 关闭SqlSession对象 SqlSessionFactoryUtils.close(); } //之后需要添加监听器配置,才能监听到容器的启动和销毁 } package com.imooc.servlet; 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.imooc.dao.UsersDao; import com.imooc.entity.Users; import java.io.IOException; import java.util.List; /** * Created by mouwe on 2017/5/23. */ //为了方便和前台进行交互,添加servlet层, //用于调用DAO中的一些数据 @WebServlet("/index") public class UsersFindServlet extends HttpServlet { //直接调用DAO中的操作 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(); System.out.println(list); req.setAttribute("usersList", list); req.getRequestDispatcher("index.jsp").forward(req, resp); } } package com.imooc.util; 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.xml"; private static SqlSessionFactory sqlSessionFactory; //将获取到的sqlSession对象存放在ThreadLocal里进行管理 private static ThreadLocal<SqlSession> threadLocal = new ThreadLocal<SqlSession>(); /** * 创建一个初始化SqlSessionFactory的方法 */ public static void initSqlSessionFactry() { try { InputStream is = Resources.getResourceAsStream(RESOURCE); sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); } catch (IOException e) { e.printStackTrace(); } } /** * 获取工厂对象的方法 * @return */ public static SqlSessionFactory getSqlSessionFactory() { return sqlSessionFactory; } /** * 关闭SqlSession的方法 */ public static void close(){ SqlSession session = threadLocal.get(); if(session != null) { session.close(); threadLocal.set(null); } } }
1
收起
正在回答 回答被采纳积分+1
1回答
慕用1236393
2019-10-07 18:39:26
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mydb?useSSL=false&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
问题算是解决了吧,数据可以显示,就是把properties标签去掉,换成上面的,本来以为是driver单词写错了,可是写对了还是不可以,就得把properties标签去掉,不知道怎么回事
SSM主流框架入门与综合项目实战2018版
- 参与学习 人
- 提交作业 205 份
- 解答问题 4317 个
Java中非常实用的SSM整合开发内容,从Spring开始,到MyBaits的进阶内容,再到SpringMVC的应用,最后是SSM整合开发案例,逐步深入,助你成长为一名Java工程师!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星