![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 package com.itheima.mapper; 2 3 import com.github.pagehelper.Page; 4 import com.github.pagehelper.PageHelper; 5 import com.github.pagehelper.PageInfo; 6 import com.itheima.domain.User; 7 import org.apache.ibatis.io.Resources; 8 import org.apache.ibatis.session.SqlSession; 9 import org.apache.ibatis.session.SqlSessionFactory; 10 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 11 import org.junit.Test; 12 13 import java.io.IOException; 14 import java.io.InputStream; 15 import java.util.Arrays; 16 import java.util.Date; 17 import java.util.List; 18 19 public class UserMapperTest { 20 21 @Test 22 public void pageHelperTest() throws IOException { 23 //获取配置 24 InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml"); 25 //创建SqlSessionFactory 26 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in); 27 //创建SqlSession 28 SqlSession sqlSession = sqlSessionFactory.openSession(); 29 UserMapper mapper = sqlSession.getMapper(UserMapper.class); 30 31 //配置分页参数 32 Page page = PageHelper.startPage(3, 4); 33 34 //PageHelper只对紧跟着的第一个SQL语句起作用 35 List<User> byCondition = mapper.findByCondition(new User()); 36 37 //获得分页相关参数 38 PageInfo<User> userPageInfo = new PageInfo<>(byCondition); 39 40 //PageInfo实现了toString方法,不用我们自己打印 41 System.out.println(userPageInfo); 42 43 System.out.println("总页数=" + userPageInfo.getPages()); 44 System.out.println("总条数=" + userPageInfo.getTotal()); 45 System.out.println("当前页=" + userPageInfo.getPageNum()); 46 System.out.println("页大小=" + userPageInfo.getPageSize()); 47 System.out.println("当前页条数=" + userPageInfo.getSize()); 48 System.out.println("是否是首页=" + userPageInfo.isIsFirstPage()); 49 System.out.println("是否是末页=" + userPageInfo.isIsLastPage()); 50 System.out.println("是否有上一页=" + userPageInfo.isHasPreviousPage()); 51 System.out.println("是否有下一页=" + userPageInfo.isHasNextPage()); 52 System.out.println("上一页=" + userPageInfo.getPrePage()); 53 System.out.println("下一页=" + userPageInfo.getNextPage()); 54 System.out.println("首页=" + userPageInfo.getFirstPage()); 55 System.out.println("末页=" + userPageInfo.getLastPage()); 56 //默认显示几个页码 57 System.out.println("导航页码数=" + userPageInfo.getNavigatePages()); 58 //页码的数组 59 System.out.println("所有导航页码=" + Arrays.toString(userPageInfo.getNavigatepageNums())); 60 61 62 // List<User> userList = mapper.findAll(); 63 // System.out.println(userList);//传回的并不是Collection中List的实现类,而是自定义List实现类,toString方法处理有区别 64 // for (User user : userList) { 65 // System.out.println(user); 66 // } 67 // userList.forEach(System.out::println); 68 sqlSession.close(); 69 } 70 71 72 @Test 73 public void saveTest() throws IOException { 74 //获取配置 75 InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml"); 76 //创建SqlSessionFactory 77 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in); 78 //创建SqlSession 79 SqlSession sqlSession = sqlSessionFactory.openSession(); 80 UserMapper mapper = sqlSession.getMapper(UserMapper.class); 81 //准备User 82 User user = new User(); 83 user.setUsername("xxx"); 84 user.setPassword("123"); 85 user.setBirthday(new Date()); 86 87 int rows = mapper.save(user); 88 sqlSession.commit(); 89 System.out.println(rows); 90 91 sqlSession.close(); 92 } 93 94 @Test 95 public void findByConditionTest() throws IOException { 96 //获取配置 97 InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml"); 98 //创建SqlSessionFactory 99 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in); 100 //创建SqlSession 101 SqlSession sqlSession = sqlSessionFactory.openSession(); 102 UserMapper mapper = sqlSession.getMapper(UserMapper.class); 103 104 //条件 105 User user = new User(); 106 user.setId(1); 107 // user.setUsername("lisi"); 108 // user.setPassword("123"); 109 110 List<User> userList = mapper.findByCondition(user); 111 System.out.println(userList); 112 113 sqlSession.close(); 114 } 115 116 @Test 117 public void findByIdsTest() throws IOException { 118 //获取配置 119 InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml"); 120 //创建SqlSessionFactory 121 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in); 122 //创建SqlSession 123 SqlSession sqlSession = sqlSessionFactory.openSession(); 124 UserMapper mapper = sqlSession.getMapper(UserMapper.class); 125 126 // int[] ids = {1,2,3}; 127 Integer[] ids = {1,2,3}; 128 List<User> userList = mapper.findByIds(ids); 129 System.out.println(userList); 130 131 sqlSession.close(); 132 } 133 134 @Test 135 public void findByIdTest() throws IOException { 136 //获取配置 137 InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml"); 138 //创建SqlSessionFactory 139 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in); 140 //创建SqlSession 141 SqlSession sqlSession = sqlSessionFactory.openSession(); 142 UserMapper mapper = sqlSession.getMapper(UserMapper.class); 143 144 User user = mapper.findById(1); 145 System.out.println(user); 146 147 sqlSession.close(); 148 } 149 150 @Test 151 public void findAllTest() throws IOException { 152 //获取配置 153 InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml"); 154 //创建SqlSessionFactory 155 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in); 156 //创建SqlSession 157 SqlSession sqlSession = sqlSessionFactory.openSession(); 158 UserMapper mapper = sqlSession.getMapper(UserMapper.class); 159 160 List<User> userList = mapper.findAll(); 161 System.out.println(userList); 162 163 sqlSession.close(); 164 } 165 }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 package com.itheima.mapper; 2 3 import com.itheima.domain.User; 4 5 import java.util.List; 6 7 public interface UserMapper { 8 9 List<User> findByCondition(User condition); 10 11 List<User> findAll(); 12 13 // List<User> findByIds(int[] ids); 14 List<User> findByIds(Integer[] ids); 15 16 User findById(int id); 17 18 //如果不写返回值,则调用方法不会有影响行数返回值 19 int save(User user); 20 }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 package com.itheima.handler; 2 3 import org.apache.ibatis.type.BaseTypeHandler; 4 import org.apache.ibatis.type.JdbcType; 5 6 import java.sql.CallableStatement; 7 import java.sql.PreparedStatement; 8 import java.sql.ResultSet; 9 import java.sql.SQLException; 10 import java.util.Date; 11 12 public class DateTypeHandler extends BaseTypeHandler<Date> { 13 @Override 14 public void setNonNullParameter(PreparedStatement preparedStatement, int i, Date date, JdbcType jdbcType) throws SQLException { 15 preparedStatement.setLong(i, date.getTime()); 16 } 17 18 @Override 19 public Date getNullableResult(ResultSet resultSet, String s) throws SQLException { 20 return new Date(resultSet.getLong(s)); 21 } 22 23 @Override 24 public Date getNullableResult(ResultSet resultSet, int i) throws SQLException { 25 return new Date(resultSet.getLong(i)); 26 } 27 28 @Override 29 public Date getNullableResult(CallableStatement callableStatement, int i) throws SQLException { 30 return new Date(callableStatement.getLong(i)); 31 } 32 }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 package com.itheima.domain; 2 3 import java.util.Date; 4 5 /* 6 CREATE TABLE `user` ( 7 `id` int(11) NOT NULL AUTO_INCREMENT, 8 `username` varchar(50) DEFAULT NULL, 9 `password` varchar(50) DEFAULT NULL, 10 `birthday` bigint(20) DEFAULT NULL, 11 PRIMARY KEY (`id`) 12 ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8; 13 */ 14 public class User { 15 private int id; 16 private String username; 17 private String password; 18 private Date birthday; 19 20 public User() { 21 } 22 23 public int getId() { 24 return id; 25 } 26 27 public void setId(int id) { 28 this.id = id; 29 } 30 31 public String getUsername() { 32 return username; 33 } 34 35 public void setUsername(String username) { 36 this.username = username; 37 } 38 39 public String getPassword() { 40 return password; 41 } 42 43 public void setPassword(String password) { 44 this.password = password; 45 } 46 47 public Date getBirthday() { 48 return birthday; 49 } 50 51 public void setBirthday(Date birthday) { 52 this.birthday = birthday; 53 } 54 55 @Override 56 public String toString() { 57 return "User{" + 58 "id=" + id + 59 ", username='" + username + '\'' + 60 ", password='" + password + '\'' + 61 ", birthday=" + birthday + 62 '}'; 63 } 64 }
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 7 <!--外部的资源文件--> 8 <properties resource="jdbc.properties"></properties> 9 10 <!--别名--> 11 <typeAliases> 12 <typeAlias type="com.itheima.domain.User" alias="user"></typeAlias> 13 </typeAliases> 14 15 <!--注册类型处理器--> 16 <typeHandlers> 17 <typeHandler handler="com.itheima.handler.DateTypeHandler" ></typeHandler> 18 </typeHandlers> 19 20 <!--配置分页助手插件--> 21 <plugins> 22 <plugin interceptor="com.github.pagehelper.PageHelper"> 23 <property name="dialect" value="mysql"></property> 24 </plugin> 25 </plugins> 26 27 <environments default="development"> 28 <environment id="development"> 29 <transactionManager type="JDBC"></transactionManager> 30 <dataSource type="POOLED"> 31 <property name="driver" value="${jdbc.driver}"></property> 32 <property name="url" value="${jdbc.url}"></property> 33 <property name="username" value="${jdbc.username}"></property> 34 <property name="password" value="${jdbc.password}"></property> 35 </dataSource> 36 </environment> 37 </environments> 38 39 <mappers> 40 <mapper resource="com/itheima/mapper/UserMapper.xml"></mapper> 41 </mappers> 42 </configuration>
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <mapper namespace="com.itheima.mapper.UserMapper"> 6 7 <insert id="save" parameterType="user"> 8 insert into user(username, password, birthday) values(#{username},#{password},#{birthday}) 9 </insert> 10 11 <sql id="selectUser"> 12 select * from user 13 </sql> 14 15 <select id="findAll" resultType="user"> 16 <include refid="selectUser"></include> 17 </select> 18 19 <select id="findById" parameterType="int" resultType="user"> 20 <include refid="selectUser"></include> where id=#{id} 21 </select> 22 23 24 <select id="findByIds" parameterType="int[]" resultType="user"> 25 <include refid="selectUser"></include> 26 <where> 27 <foreach collection="array" item="id" open="id in(" separator="," close=")" > 28 #{id} 29 </foreach> 30 </where> 31 </select> 32 33 <select id="findByCondition" parameterType="user" resultType="user"> 34 <include refid="selectUser"></include> 35 <where> 36 <if test="id!=0"> 37 and id=#{id} 38 </if> 39 <if test="username!=null"> 40 and username=#{username} 41 </if> 42 <if test="password!=null"> 43 and password=#{password} 44 </if> 45 </where> 46 </select> 47 </mapper>
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.itheima</groupId> 8 <artifactId>mybatis-day02</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 <packaging>war</packaging> 11 12 <name>mybatis-day02 Maven Webapp</name> 13 <!-- FIXME change it to the project's website --> 14 <url>http://www.example.com</url> 15 16 <properties> 17 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 18 <maven.compiler.source>1.8</maven.compiler.source> 19 <maven.compiler.target>1.8</maven.compiler.target> 20 </properties> 21 22 <dependencies> 23 <!--junit--> 24 <dependency> 25 <groupId>junit</groupId> 26 <artifactId>junit</artifactId> 27 <version>4.11</version> 28 <scope>test</scope> 29 </dependency> 30 <!--mybatis--> 31 <dependency> 32 <groupId>org.mybatis</groupId> 33 <artifactId>mybatis</artifactId> 34 <version>3.4.6</version> 35 </dependency> 36 <!--mysql--> 37 <dependency> 38 <groupId>mysql</groupId> 39 <artifactId>mysql-connector-java</artifactId> 40 <version>5.1.37</version> 41 </dependency> 42 <!--log4j--> 43 <dependency> 44 <groupId>log4j</groupId> 45 <artifactId>log4j</artifactId> 46 <version>1.2.17</version> 47 </dependency> 48 <!--pagehelper--> 49 <dependency> 50 <groupId>com.github.pagehelper</groupId> 51 <artifactId>pagehelper</artifactId> 52 <version>3.7.5</version> 53 </dependency> 54 <dependency> 55 <groupId>com.github.jsqlparser</groupId> 56 <artifactId>jsqlparser</artifactId> 57 <version>0.9.1</version> 58 </dependency> 59 </dependencies> 60 61 <build> 62 <finalName>mybatis-day02</finalName> 63 <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> 64 <plugins> 65 <plugin> 66 <artifactId>maven-clean-plugin</artifactId> 67 <version>3.1.0</version> 68 </plugin> 69 <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> 70 <plugin> 71 <artifactId>maven-resources-plugin</artifactId> 72 <version>3.0.2</version> 73 </plugin> 74 <plugin> 75 <artifactId>maven-compiler-plugin</artifactId> 76 <version>3.8.0</version> 77 </plugin> 78 <plugin> 79 <artifactId>maven-surefire-plugin</artifactId> 80 <version>2.22.1</version> 81 </plugin> 82 <plugin> 83 <artifactId>maven-war-plugin</artifactId> 84 <version>3.2.2</version> 85 </plugin> 86 <plugin> 87 <artifactId>maven-install-plugin</artifactId> 88 <version>2.5.2</version> 89 </plugin> 90 <plugin> 91 <artifactId>maven-deploy-plugin</artifactId> 92 <version>2.8.2</version> 93 </plugin> 94 </plugins> 95 </pluginManagement> 96 </build> 97 </project>
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3307/test jdbc.username=root jdbc.password=root
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
### direct log messages to stdout ### log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.err log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n ### direct messages to file mylog.log ### log4j.appender.file=org.apache.log4j.FileAppender log4j.appender.file.File=c:/mylog.log log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n ### set log levels - for more verbose logging change 'info' to 'debug' ### log4j.rootLogger=debug, stdout