1.mybatis框架简介
- mybatis:持久层框架,封装来几乎所有的jdbc代码和参数的手工设置及结果集的检索(我们可以把精力主要放在sql语句上,sql的执行及sql获取的数据交个mybatis处理)
- mybatis配置文件
1.sqlMapConfig.xml(1个):主配置文件,用于指定数据库参数和框架参数。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-config.dtd">
<configuration>
<environments default="environment">
<environment id="environment">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="oracle.jdbc.OracleDriver" />
<property name="url"
value="jdbc:oracle:thin:@localhost:1521:orcl" />
<property name="username" value="system" />
<property name="password" value="123456" />
</dataSource>
</environment>
</environments>
<!-- 映射文件位置 -->
<mappers>
<mapper resource="com/entity/EmpMapper.xml" />
</mappers>
</configuration>
2.SqlMap.xml(n个):映射定义文件,用于定义sql语句和映射信息。
<?xml version="1.0" encoding="UTF-8" ?>
<!-- Dept.xml 在com.tarena.entity 包中 -->
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="test">
<select id="findById" parameterType="int" resultType="com.entity.Emp">
select * from emp where id=#{id }
</select>
</mapper>
- 框架API
1.SqlSessionFactoryBuilder:负责根据mybatis配置文件SqlMapConfig.xml构建SQLSessionFactory实例
2.SQLSessionFactory:每一个Mybatis应用程序都以一个SqlSessionFactory对象为核心,该对象负责创建SqlSession对象实例。
3.SqlSession:该对象包含了所有执行sql操作的方法,用于执行以映射的sql语句
2.mybatis基本应用
- 环境的搭建和基本的应用
1.添加mybatis开发包和数据库驱动包(mybatis…)
2.在src下添加SqlMapConfig.xml
3. 修改SqlMapConfig.xml,指定数据库连接参数
4. 利用Mybatis API编程,获取SqlSession实例
5. 利用SqlSession实现crud操作
①.根据数据表编写实体类(类的属性最好与表字段一致)
②编写SqlMap.xml映射文件,定义sql的操作和映射信息
③获取Sqlsession对象,执行sql操作
④提交事务
⑤释放SqlSession
@Before
public void init(){
SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder();
SqlSessionFactory ssf =
ssfb.build(
TestCase.class.getClassLoader().
getResourceAsStream(
"SqlMapConfig.xml"));
session = ssf.openSession();
}
@Test
public void test3(){
Emp emp = session.selectOne("test.findById",21);
System.out.println(emp);
session.close();
}
//SqlMap.xml的配置,根据namespace、id调用相应的方法,parameterType为参数类型
//resultType返回值类型
<mapper namespace="test">
<select id="findById" parameterType="int" resultType="com.entity.Emp">
select * from emp where id=#{id }
</select>
</mapper>
- 使用Mapper映射器
1.在原有的基础上编写映射器接口
2.在SqlSession中获取映射器接口的实例,然后调用接口中的方法执行sql
public interface EmpDao{
//Mapper接口中的方法要和SqlMap.xml中的sql的id保持一致
//SqlMap.xml中namespace的值要与接口保持一致(namespace=“com.dao.EmpDao”)
public Emp findById(int id);
}
public class TestCase {
private SqlSession session;
private EmpDao dao;
@Before
public void init(){
SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder();
SqlSessionFactory ssf = ssfb.build(
TestCase.class.getClassLoader().getResourceAsStream("SqlMapConfig.xml"));
session = ssf.openSession();
dao = session.getMapper(EmpDao.class);
}
@Test
public void test3(){
Emp emp = dao.findById(81);
System.out.println(emp);
session.close();
}
}
- ResultMap映射定义
在SqlMap.xml定义select操作时,如果查询字段名与java对象属性不一致时,需要resultMap元素显示指定映射关系
<select id="findById" resultMap="EmpMap">
select eno,ename from emp
<select/>
<resultMap id="EmpMap" type="com.entity.Emp">
<result property="no" column="eno"/>
<result property="name" column="ename"/>
<resultMap/>