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保持一致。