Mybatis环境搭建-实现查询所有的功能
Posted 遍唱阳春
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis环境搭建-实现查询所有的功能相关的知识,希望对你有一定的参考价值。
1.在数据库中新建一个表与java中的实体类对应,我这里新建一个mybatis数据库,在数据库中新建一个user表:
2.打开IDEA,新建一个maven项目,可以什么都不选直接创建:
3.创建好maven项目之后,在pom.xml中引入需要的依赖:
<groupId>com.itheima</groupId> <artifactId>day02_mybatis</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.15</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.12</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </dependencies>
4.在src-main-resources里面加入log4j.properties:
5.在src-main-java下新建类com.itheima.domain.User类并实现Serializable:
6.在User类中建立与数据库mybatis中的user表对应的属性,生成getter and setter方法和toString()方法:
package com.itheima.domain; import java.io.Serializable; import java.util.Date; public class User implements Serializable { private Integer id; private String username; private String address; private String sex; private Date birthday; 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 getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } @Override public String toString() { return "User{" + "id=" + id + ", username=\'" + username + \'\\\'\' + ", address=\'" + address + \'\\\'\' + ", sex=\'" + sex + \'\\\'\' + ", birthday=" + birthday + \'}\'; } }
7.在src-main-java-com-itheima下新建一个dao包,并且新建接口IUserDao:
8.在IUserDao中写查询所有的方法:
package com.itheima.dao; import com.itheima.domain.User; import java.util.List; public interface IUserDao { //查询所有 List<User> findAll(); }
9.在src-main-resources里面新建file,命名为SqlMapConfig.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> <!-- 配置mysql信息--> <environments default="mysql"> <environment id="mysql"> <!-- 配置事务--> <transactionManager type="JDBC"/> <!-- 配置资源(连接池)--> <dataSource type="POOLED"> <property name="driver" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <!-- 配置mapper信息--> <mappers> <mapper resource="com/itheima/dao/IUserDao.xml"/> </mappers> </configuration>
注意:因为我的数据库是8.0版本,所以pom.xml中引入的jar包也是mysql-connector-java 8.0.15版本,所以这里的driver里面有一个"cj",而且我的数据库会报TimeZone异常,所以我在url后面加上了参数“?serverTimezone=UTC",其实可以不加。
10.在resources下面按照java里面的dao路径,新建IUserDao.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.itheima.dao.IUserDao"> <!-- 查询所有--> <select id="findAll" resultType="com.itheima.domain.User"> select * from user ; </select> </mapper>
11.在test目录下编写测试类:
package com.itheima.test; import com.itheima.dao.IUserDao; import com.itheima.domain.User; 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 org.junit.Test; import java.io.IOException; import java.io.InputStream; import java.util.List; //测试mybatis的crud public class MybatisTest { @Test public void testFindAll() throws IOException { //1.读取配置文件,生成字节输入流 InputStream in= Resources.getResourceAsStream("SqlMapConfig.xml"); //2.获取SqlSessionFactory SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(in); //3.获取SqlSession对象 SqlSession session=factory.openSession(); //4.获取dao的代理对象 IUserDao userDao=session.getMapper(IUserDao.class); //5.执行查询所有对象 List<User> users = userDao.findAll(); for (User user:users){ System.out.println(user); } //6.释放资源 session.close(); in.close(); } }
12.运行测试方法testFindAll(),结果如下:
以上是关于Mybatis环境搭建-实现查询所有的功能的主要内容,如果未能解决你的问题,请参考以下文章
Mybatis关于复杂的SQL查询的处理&Mybatis的缓存机制
Mybatis关于复杂的SQL查询的处理&Mybatis的缓存机制