MyBatis簡介
MyBatis 本是apache的一個開源項目iBatis, 2010年這個項目由apache software foundation
遷移到了google code,並且改名爲MyBatis 。2013年11月遷移到Github。
iBATIS一詞來源於“internet”和“abatis”的組合,是一個基於Java的持久層框架。iBATIS提供的持久層框架包括SQL Maps和Data Access Objects(DAO)
2.MyBatis是對JDBC技術做的封裝。
封裝了以下功能
- 封裝了獲取連接、創建statement、設置sql參數、執行sql、釋放連接過程
- 封裝了將查詢結果ResultSet記錄映射成實體對象過程
- 封裝了Dao對象實現過程(只提供接口,框架生成實現對象)
3.程序員使用MyBatis需要做以下工作:
- 搭建MyBatis框架環境
- 編寫實體類
- 編寫SQL語句
- 編寫Dao接口
- 獲取SqlSession進行數據庫操作
4.Mybatis基本應用
要求:對DEPT表進行增刪改查操作。
1. 搭建MyBatis框架環境
- 引入mybatis.jar和ojdbc.jar包
- 添加SqlMapConfig.xml,設置數據庫連接參數
<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="SCOTT" />
<property name="password" value="TIGER" />
</dataSource>
</environment>
</environments>
<!-- 加載SQL定義文件 -->
<mappers>
<mapper resource="cn/xdl/sql/DeptMapper.xml" />
</mappers>
</configuration>
2. 編寫實體類Dept
public class Dept implements Serializable{
private int deptno;
private String dname;
private String loc;
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
//省略其他set/get方法
}
3. 編寫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">
<!-- namespace指定和哪個接口映射 -->
<mapper namespace="cn.xdl.dao.DeptMapper">
<!-- resultType是select特有 -->
<select id="findAll" resultType="cn.xdl.entity.Dept">
select * from DEPT
</select>
<select id="findById" resultType="cn.xdl.entity.Dept" parameterType="int">
select * from DEPT where DEPTNO=#{no}
</select>
<insert id="save" parameterType="cn.xdl.entity.Dept">
insert into DEPT(DEPTNO,DNAME,LOC) values (#{deptno},#{dname},#{loc})
</insert>
<update id="update" parameterType="cn.xdl.entity.Dept">
update DEPT set DNAME=#{dname},LOC=#{loc} where DEPTNO=#{deptno}
</update>
<!-- 如果parameterType爲單個值,#{標識符}表達式標識符沒有約定 -->
<delete id="delete" parameterType="int">
delete from DEPT where DEPTNO=#{id}
</delete>
</mapper>
SQL參數部分,可以使用
${標識符}或#{標識符}
,如果使用#{}內部採用預編譯機制執行SQL操作。如果使用${}內部採用非預編譯過程。
4. 編寫Mapper接口(Mapper映射器)
/**
* 方法定義參考SQL定義的id、parameterType、resultType屬性
* @author Administrator
* 1.方法名與id屬性一致
* 2.參數類型與parameterType屬性一致
* 3.返回結果:多行查詢List<resultType>;單行查詢 resultType;增刪改爲void或int
* 4.SQL定義文件中namespace="cn.xdl.dao.DeptMapper"
*/
public interface DeptMapper {
public List<Dept> findAll();
public Dept findById(int id);
public int save(Dept dept);
public int update(Dept dept);
public int delete(int id);
}
5. 獲取SqlSession操作
public class MyBatisUtil {
public static SqlSession getSession() throws IOException{
// SqlSessionFactoryBuilder
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
Reader reader = Resources.getResourceAsReader("sqlmap-config.xml");
// SqlSessionFactory
SqlSessionFactory factory = builder.build(reader);
// SqlSession
SqlSession session = factory.openSession();
return session;
}
}
6. 利用SqlSession獲取DeptMapper接口對象
@Test//按規則定義DeptMapper接口,由框架生成實現對象
public void test1() throws IOException{
SqlSession session = MyBatisUtil.getSession();
//由框架生成DeptMapper接口實現對象
DeptMapper deptDao = session.getMapper(DeptMapper.class);
System.out.println(deptDao.getClass().getName());
List<Dept> list = deptDao.findAll();
for(Dept dept:list){
System.out.println(dept.getDeptno()+" "+dept.getDname()+" "+dept.getLoc());
}
session.close();
}
Mybatis擴展操作
- 如果SQL參數#{}獲取null值,會出現無效列類型:11111
解決方法:需要在#{key,jdbcType=類型},類型從下面表中選擇
2. 爲自定義類型指定別名
- 在sqlmapconfig.xml中定義別名
<typeAliases> <typeAlias type="cn.xdl.entity.Dept"
alias="dept"/> </typeAliases>
- 在SQL定義部分給parameterType或resultType屬性使用dept
<select id="findAll" resultType="dept">
select * from DEPT </select>
3. 顯示MyBatis底層SQL執行日誌
在sqlmapconfig.xml中開啓日誌輸出
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
分頁查詢
- 下載pageHelper分頁工具包
- 在sqlmapconfig.xml中配置工具包
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
<property name="dialect" value="oracle"/>
</plugin>
</plugins>
- 分頁代碼
PageHelper.startPage(2, 5);//分頁查詢設置,查詢第2頁,一頁5條
List<Dept> list = deptDao.findAll();
使用sqlSession.selectList("sql的id",sql參數,RowBounds對象)
RowBounds bound = new RowBounds(5,5);
SqlSession session = MyBatisUtil.getSession();
List<Dept> list = session.selectList("findAll", null, bound);