mybatis的设计理念:把SQL交给开发人员,把预编译、参数设置、sql执行、封装结果等步骤交给框架。
mybatis三种使用方式
传统使用方式示例
1) 必要的依赖包
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
| <dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.39</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.24</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.24</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> </dependencies>
|
2) 代码结构 和 库表准备
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| test01.sql_session_factory | | | |
create database if not exists test;
create table if not exists test.t_emp( id int not null primary key auto_increment, last_name varchar(50), email varchar(50), gender varchar(10) );
|
3) mybatis-test01-config.xml(全局配置文件)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| <?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="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://192.168.1.96:3306/test" /> <property name="username" value="xxx" /> <property name="password" value="xxx" /> </dataSource> </environment> </environments> <mappers> <mapper resource="test01/sql_session_factory/mybatis-test01-mapper.xml" /> </mappers> </configuration>
|
4) mybatis-test01-mapper.xml(SQL映射文件)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <?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="test01.sql_session_factory"> <select id="selectEmp" resultType="test01.sql_session_factory.Emp"> select id, last_name as lastName, email, gender from t_emp where id = #{id} </select> </mapper>
|
5) Emp.java
1 2 3 4 5 6 7
| public class Emp { private Integer id; private String lastName; private String email; private String gender; }
|
6) Test01.java
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
|
@Test public void createSqlSessionFactoryWithXML() throws IOException { String resource = "test01/sql_session_factory/mybatis-test01-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); System.err.println(sqlSessionFactory);
SqlSession session = null; try { session = sqlSessionFactory.openSession(); Emp emp = session.selectOne("selectEmp", 1); System.err.println(emp); } finally { session.close(); } }
@Test public void createSqlSessionFactoryWithoutXML() { Properties properties = new Properties(); properties.setProperty("driver", "com.mysql.jdbc.Driver"); properties.setProperty("url", "jdbc:mysql://192.168.1.96:3306/test"); properties.setProperty("username", "xxx"); properties.setProperty("password", "xxx"); PooledDataSourceFactory pooledDataSourceFactory = new PooledDataSourceFactory(); pooledDataSourceFactory.setProperties(properties); DataSource dataSource = pooledDataSourceFactory.getDataSource(); TransactionFactory transactionFactory = new JdbcTransactionFactory(); Environment environment = new Environment("development", transactionFactory, dataSource); Configuration configuration = new Configuration(environment); configuration.addMapper(Mapper.class); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration); System.err.println(sqlSessionFactory); }
|
使用接口方式示例(推荐)
1) 在方式一的基础上增加 EmpMapper 接口
1 2 3 4
| public interface EmpMapper { Emp getEmpById(Integer id); }
|
2) mapper映射文件绑定 Mapper接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <?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="test02.sql_session_factory.EmpMapper"> <select id="getEmpById" resultType="test02.sql_session_factory.Emp"> select id, last_name as lastName, email, gender from t_emp where id = #{id} </select> </mapper>
|
3) 测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| @Test public void createSqlSessionFactoryWithXML() throws IOException { String resource = "test02/sql_session_factory/mybatis-test02-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); System.err.println(sqlSessionFactory);
SqlSession session = null; try { session = sqlSessionFactory.openSession(); EmpMapper empMapper = session.getMapper(EmpMapper.class); Emp emp = empMapper.getEmpById(1); System.err.println(empMapper); System.err.println(emp); } finally { session.close(); } }
|
使用注解映射SQL示例(应用和SQL耦合,不推荐)
1) 编写mapper接口
1 2 3 4
| public interface EmpAnnotationMapper { @Select("select * from t_emp where id=#{id}") Emp getEmpById(Integer id); }
|
2) 引入SQL映射接口
1 2 3
| <mappers> <mapper class="test03.configuration.EmpAnnotationMapper"/> </mappers>
|
3) 测试(略)
全局配置文件简介
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
| <?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="test03/configuration/jdbc.properties"> </properties>
<settings> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings>
<typeAliases> <package name="test03.configuration"/> </typeAliases>
<typeHandlers> </typeHandlers>
<environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </dataSource> </environment> </environments>
<databaseIdProvider type="DB_VENDOR"> <property name="MySQL" value="mysql"/> <property name="Oracle" value="oracle" /> <property name="SQL Server" value="sqlserver"/> <property name="DB2" value="db2"/> </databaseIdProvider>
<mappers> <package name="test03.configuration"/>
</mappers> </configuration>
|