接着上章内容mybatis源码环境搭建 ,根据官方文档,运行一个demo项目。
项目结构
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 mybatis-learning/mybatis-learning-01 ├── mybatis-learning-01.iml ├── pom.xml └── src ├── main │ ├── java │ │ ├── cn │ │ │ └── z201 │ │ │ └── mybatis │ │ │ └── learning │ │ │ ├── SetUpSessionFactory.java │ │ │ ├── User.java │ │ │ └── UserDao.java │ │ └── package-info.java │ └── resources │ ├── init_table.sql │ ├── mybatis │ │ └── UserDao.xml │ └── mybatis-config.xml └── test └── java └── cn └── z201 └── mybatis └── test ├── MybatisTest01.java └── package-info.java 15 directories, 11 files
根据上面注释的顺序,我们依次介绍项目代码。
0x01 项目依赖管理 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 <?xml version="1.0" encoding="UTF-8"?> <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/xsd/maven-4.0.0.xsd" > <modelVersion > 4.0.0</modelVersion > <groupId > cn.z201</groupId > <artifactId > mybatis-learning-01</artifactId > <version > 1.0-SNAPSHOT</version > <build > <plugins > <plugin > <groupId > org.apache.maven.plugins</groupId > <artifactId > maven-compiler-plugin</artifactId > <configuration > <source > 8</source > <target > 8</target > </configuration > </plugin > </plugins > </build > <properties > <mybatis.version > 3.5.0</mybatis.version > <junit.version > 4.12</junit.version > <lmbok.version > 1.18.4</lmbok.version > <mysql.version > 5.1.47</mysql.version > <h2.version > 1.4.197</h2.version > <project.build.sourceEncoding > UTF-8</project.build.sourceEncoding > </properties > <dependencies > <dependency > <groupId > org.mybatis</groupId > <artifactId > mybatis</artifactId > <version > ${mybatis.version}</version > </dependency > <dependency > <groupId > org.projectlombok</groupId > <artifactId > lombok</artifactId > <version > ${lmbok.version}</version > </dependency > <dependency > <groupId > mysql</groupId > <artifactId > mysql-connector-java</artifactId > <version > ${mysql.version}</version > </dependency > <dependency > <groupId > com.h2database</groupId > <artifactId > h2</artifactId > <version > ${h2.version}</version > <scope > test</scope > </dependency > <dependency > <groupId > junit</groupId > <artifactId > junit</artifactId > <version > ${junit.version}</version > <scope > test</scope > </dependency > </dependencies > </project >
0x02 mybatis 配置文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 <?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 ="org.h2.Driver" /> <property name ="url" value ="jdbc:h2:mem:mybatis_learning_01_db;MODE=MYSQL;INIT=RUNSCRIPT FROM './src/main/resources/init_table.sql'" /> <property name ="username" value ="root" /> <property name ="password" value ="" /> </dataSource > </environment > </environments > <mappers > <mapper resource ="mybatis/UserDao.xml" /> </mappers > </configuration >
0x03 mybatis 初始化 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 package cn.z201.mybatis.learning;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 SetUpSessionFactory { public SqlSession initSessionFactory () throws IOException { String resource = "mybatis-config.xml" ; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); return sqlSessionFactory.openSession(); } }
0x04 h2数据库初始化sql 1 2 3 4 5 6 7 8 9 10 11 CREATE TABLE `user` ( id SMALLINT PRIMARY KEY AUTO_INCREMENT , name VARCHAR (50 ), age SMALLINT , gender SMALLINT ); INSERT INTO `user` (name , age , gender) VALUES ('TOM' , 12 , 0 );INSERT INTO `user` (name , age , gender) VALUES ('JON' , 13 , 1 );INSERT INTO `user` (name , age , gender) VALUES ('King' , 11 , 0 );
0x05 实体bean 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 package cn.z201.mybatis.learning;import lombok.AllArgsConstructor;import lombok.Data;import lombok.ToString;@Data @AllArgsConstructor @ToString public class User { private Integer id; private String name; private Integer age; private Integer gender; }
0x06 实体dao 1 2 3 4 5 6 7 8 9 package cn.z201.mybatis.learning;import java.util.List;public interface UserDao { List<User> list () ; }
0x07 实体dao对应的mapper.xml 1 2 3 4 5 6 7 8 9 <?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 ="cn.z201.mybatis.learning.UserDao" > <select id ="list" resultType ="cn.z201.mybatis.learning.User" > SELECT * FROM `user` </select > </mapper >
0x08 单元测试 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 package cn.z201.mybatis.test;import cn.z201.mybatis.learning.SetUpSessionFactory;import cn.z201.mybatis.learning.User;import cn.z201.mybatis.learning.UserDao;import org.apache.ibatis.session.SqlSession;import org.junit.Assert;import org.junit.Test;import java.io.IOException;import java.util.List;public class MybatisTest01 { @Test public void test01 () throws IOException { SetUpSessionFactory setUpSessionFactory = new SetUpSessionFactory(); SqlSession sqlSession = setUpSessionFactory.initSessionFactory(); UserDao userDao = sqlSession.getMapper(UserDao.class ) ; List<User> userList = userDao.list(); sqlSession.close(); Assert.assertNotNull(userList); userList.forEach(System.out::println); } }
1 2 3 User(id=1, name=TOM, age=12, gender=0) User(id=2, name=JON, age=13, gender=1) User(id=3, name=King, age=11, gender=0)