本次实践参考官方文档:mybatis-3.4.1.pdf。
2.1 Getting started
public class MyBatisTest { /** * 1、接口式编程 * 原生: Dao ====> DaoImpl * mybatis: Mapper ====> xxMapper.xml * * 2、SqlSession代表和数据库的一次会话;用完必须关闭; * 3、SqlSession和connection一样是非线程安全。每次使用都应该去获取新的对象。 * 4、mapper接口没有实现类,但是mybatis会为这个接口生成一个代理对象。 * (将接口和xml进行绑定) * EmployeeMapper empMapper = sqlSession.getMapper(EmployeeMapper.class); * 5、两个重要的配置文件: * mybatis的全局配置文件:包含数据库连接池信息,事务管理器信息等...系统运行环境信息 * sql映射文件:保存了每一个sql语句的映射信息: * 将sql抽取出来。 */ @Test public void test() throws IOException{ String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession openSession = sqlSessionFactory.openSession(); try{ Employee employee = openSession.selectOne("com.it.mybatis.EmployeeMapper.selectEmp", 1); System.out.println(employee); }finally{ openSession.close(); } } /** * 1、根据xml配置文件(全局配置文件)创建一个SqlSessionFactory对象 有数据源一些运行环境信息 * 2、sql映射文件;配置了每一个sql,以及sql的封装规则等。 * 3、将sql映射文件注册在全局配置文件中 * 4、写代码: * 1)、根据全局配置文件得到SqlSessionFactory; * 2)、使用sqlSession工厂,获取到sqlSession对象使用他来执行增删改查 * 一个sqlSession就是代表和数据库的一次会话,用完关闭 * 3)、使用sql的唯一标志来告诉MyBatis执行哪个sql。sql都是保存在sql映射文件中的。 */ @Test public void test2() throws IOException{ String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession openSession = sqlSessionFactory.openSession(); try{ EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class); Employee emp = mapper.getEmployeeById(1); System.out.println(emp); }finally{ openSession.close(); } } }
相应dao
public interface EmployeeMapper { Employee getEmployeeById(Integer id); }
3.1 Configuration
<?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> <!-- 1、mybatis可以使用properties来引入外部properties配置文件的内容; resource:引入类路径下的资源 url:引入网络路径或者磁盘路径下的资源 --> <properties resource="dbconfig.properties"></properties> <!-- 2、settings包含很多重要的设置项 setting:用来设置每一个设置项 name:设置项名 value:设置项取值 --> <settings> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> <!-- 3、typeAliases:别名处理器:可以为我们的java类型起别名 别名不区分大小写 --> <typeAliases> <!-- 1、typeAlias:为某个java类型起别名 type:指定要起别名的类型全类名;默认别名就是类名小写;employee alias:指定新的别名 --> <!-- <typeAlias type="com.it.mybatis.bean.Employee"/> --> <!-- 2、package:为某个包下的所有类批量起别名 name:指定包名(为当前包以及下面所有的后代包的每一个类都起一个默认别名(类名小写),) --> <package name="com.it.mybatis.bean" /> <!-- 3、批量起别名的情况下,使用@Alias注解为某个类型指定新的别名 --> </typeAliases> <environments default="dev_mysql"> <environment id="dev_mysql"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="${mysql.driver}" /> <property name="url" value="${mysql.url}" /> <property name="username" value="${mysql.username}" /> <property name="password" value="${mysql.password}" /> </dataSource> </environment> <environment id="dev_oracel"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="${oracle.driver}" /> <property name="url" value="${oracle.url}" /> <property name="username" value="${oracle.username}" /> <property name="password" value="${oracle.password}" /> </dataSource> </environment> </environments> <!-- 5、databaseIdProvider:支持多数据库厂商的; type="DB_VENDOR":VendorDatabaseIdProvider 作用就是得到数据库厂商的标识(驱动getDatabaseProductName()),mybatis就能根据数据库厂商标识来执行不同的sql; MySQL,Oracle,SQL Server,xxxx --> <databaseIdProvider type="DB_VENDOR"> <!-- 为不同的数据库厂商起别名 --> <property name="MySQL" value="mysql"/> <property name="Oracle" value="oracle"/> <property name="SQL Server" value="sqlserver"/> </databaseIdProvider> <!-- 将我们写好的sql映射文件(EmployeeMapper.xml)一定要注册到全局配置文件(mybatis-config.xml)中 --> <!-- 6、mappers:将sql映射注册到全局配置中 --> <mappers> <!-- mapper:注册一个sql映射 注册配置文件 resource:引用类路径下的sql映射文件 mybatis/mapper/EmployeeMapper.xml url:引用网路路径或者磁盘路径下的sql映射文件 file:///var/mappers/AuthorMapper.xml 注册接口 class:引用(注册)接口, 1、有sql映射文件,映射文件名必须和接口同名,并且放在与接口同一目录下; 2、没有sql映射文件,所有的sql都是利用注解写在接口上; 推荐: 比较重要的,复杂的Dao接口我们来写sql映射文件 不重要,简单的Dao接口为了开发快速可以使用注解; --> <mapper resource="com/it/mybatis/dao/EmployeeMapper.xml" /> <mapper class="com.it.mybatis.dao.EmployeeMapperAnnotation" /> <!-- 批量注册: EmployeeMapper.java与EmployeeMapper.xml同名同路径--> <!-- <package name="com.it.mybatis.dao"/> --> </mappers> </configuration>
针对 5、databaseIdProvider:支持多数据库厂商的
<?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"> <!-- namespace:名称空间; 指定为接口的全类名 id:唯一标识 resultType:返回值类型 #{id}:从传递过来的参数中取出id值 Employee getEmployeeById(Integer id); --> <mapper namespace="com.it.mybatis.dao.EmployeeMapper"> <select id="getEmployeeById" resultType="com.it.mybatis.bean.Employee" > select * from tbl_employee where id = #{id} </select> <!--databaseId 参见mybatis-config中多数据库databaseIdProvider中property的value --> <select id="getEmployeeById" resultType="com.it.mybatis.bean.Employee" databaseId="mysql"> select * from tbl_employee where id = #{id} </select> <select id="getEmployeeById" resultType="com.it.mybatis.bean.Employee" databaseId="oracle"> select id,last_name,gender,email from tbl_employee where id = #{id} </select> </mapper>
针对:6、mappers:将sql映射注册到全局配置中-注册接口
public interface EmployeeMapperAnnotation { @Select(value={"select * from tbl_employee where id = #{id}"}) Employee getEmployeeById(Integer id); }
相应测试代码:
/** * 基于注解 * @throws IOException */ @Test public void test3() throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession openSession = sqlSessionFactory.openSession(); try{ EmployeeMapperAnnotation mapper = openSession.getMapper(EmployeeMapperAnnotation.class); Employee emp = mapper.getEmployeeById(1); System.out.println(emp); }finally{ openSession.close(); } }
原文:https://www.cnblogs.com/gzhcsu/p/12660515.html