Mybaits是支持普通的SQL查询,存储过程和高级映射的优秀持久层框架。
Mybatis封装了几乎所有的JDBC代码和参数的手工设置以及结果集的检索。
MyBatis使用XML或注解做配置和定义映射关系。
MyBatis体现结构:
1、加载配置:MyBatis将SQL的配置信息加载成为一个个的MappedStatement对象。
2、SQL解析:当接收到请求时,MyBatis会根据传入的SQL的ID找到MappedStatement,然后根据传入参数的对象对MappedStatement进行解析。解析成最后要执行的SQL语句
3、SQL执行:将最终得到的SQL和参数放到数据库中进行执行,得到结果。
4、结果映射:将结果按照映射关系进行转换,可以转换为HashMap,JavaBean,基本数据类型。并将结果返回。
MyBatis的XML配置文件:
1、sqlMapConfig.xml 定义数据库连接参数和框架参数。
<?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:xe" />
<property name="username" value="root" />
<property name="password" value="" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/shop/dao/EmpMapper.xml" />
</mappers>
</configuration>
2、sqlMap.xml(多个) 定义SQL语句和映射关系。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="com.shop.dao.EmpMapper">
<select id="findAll"
resultType="com.shop.entity.Emp">
select * from t_emp
</select>
<select id="findById"
parameterType="integer"
resultType="com.shop.entity.Emp">
select * from t_emp where empno=#{id}
</select>
<insert id="save"
parameterType="com.shop.entity.Emp">
insert into t_emp values(
emp_seq.nextval,
#{ename},
#{job},
#{mgr},
#{hiredate},
#{sal},
#{comm},
#{deptno}
)
</insert>
<update id="update"
parameterType="com.shop.entity.Emp">
update t_emp set
ename=#{ename},
job=#{job},
mgr=#{mgr},
hiredate=#{hiredate},
sal=#{sal},
comm=#{comm},
deptno=#{deptno}
where empno=#{empno}
</update>
<delete id="delete"
parameterType="integer">
delete from t_emp where empno=#{id}
</delete>
</mapper>
如果查询结果字段名和Java POJO属性不一致时,可以自己定义ResultMap:
<resultMap id="empMap",type="com.shop.entity.Emp">
<result property="no" column="id">
<result property="name" column="dName">
</resultMap>
常用的几个接口:
SqlSessionFactoryBuilder:负责加载SqlMapConfig.xml对象构建SqlSessionFactory对象。
SqlSessionFactory:负责创建SqlSession对象。
SqlSession:包含了所有咨询SQL操作的方法。
String conf = "sqlMapConfig.xml";
Reader reader = Resources.getResourceAsReader(conf);
SqlSessionFactoryBuilder sfb = new SqlSessionFactoryBuilder();
SqlSessionFactory sf = sfb.build(reader);
SqlSession session = sf.openSession()
利用SqlSession实现CRUD操作的步骤:
1、编写实体类
2、编写SqlMap.xml映射文件,定义SQL操作和映射信息。
3、获取SqlSession对象。
4、提交事务。
5、释放SqlSession对象资源。
返回Map类型查询结果:
<seelct id="findById" parameterType="int" resultType="java.util.HashMap">
select name,slary from emp where id=#{id}
</select>
Mapper映射器:开发者创建绑定映射语句的接口。
DeptMapper mapper = session.getMapper(DeptMapper.class)
其中DeptMapper接口定义了几个方法分别为:
public List<Dept> findAll()
public Dept findById(int id)
public void addDept(Dept dept)
public void updateDept(Dept dept)
public void deleteById(int id)
主要的是:这些方法应该和sqlMap.xml中的SQL的id保持一致。