文章目錄
- 一、框架概述
- 1.1、三層架構
- 1.2、什么是框架
- 1.3、MyBatis框架
- 1.3.1、使用JDBC的缺陷
- 1.3.2、MyBatis解決的主要問題
- 1.3.3、MyBatis提供的功能
- 二、MyBatis使用傳統Dao開發方式
- 2.1、撰寫Student物體類
- 2.2、撰寫StudentDao介面
- 2.3、撰寫Dao介面Mapper映射檔案StudentDao.xml
- 2.4、創建MyBatis主組態檔
- 2.5、創建工具類(MyBatisUtils類)
- 2.6、創建Dao介面的實作類
- 2.7、創建測驗類
- 2.8、主要類介紹
- 三、MyBatis實作Dao的動態代理
- 3.1Dao的動態代理
- 3.2、深入理解引數
- 3.2.1、parameterType
- 3.2.2、一個簡單型別的引數
- 3.2.3、多個引數,使用@Param命名引數
- 3.2.4、多個引數-使用java物件
- 3.2.5、多個引數-按位置
- 3.2.6、多個引數-使用Map
- 3.2.7、# 和 $
- 3.3、MyBatis的輸出結果
- 3.3.1、resultType
- 3.3.2、resultMap
- 3.3.3、物體類屬性名和列名不同的處理方式
- 3.3.4、模糊 like
- 四、動態SQL
- 4.1、if標簽
- 4.2、where標簽
- 4.3、foreach標簽
- 4.4、代碼片段
- 4.5、總結
- 五、MyBatis主組態檔
- 六、MyBatis擴展
- 基于PageHelper的分頁
一、框架概述
1.1、三層架構
三層架構包含的三層:
界面層:和用戶打交道的,接收用戶的請求引數,顯示處理結果的,對應的包是controller包,
業務邏輯層:接收了界面層傳遞的資料,計算邏輯,呼叫資料庫,獲取資料,對應的包是service包,
資料訪問層:就是訪問資料庫,執行對資料的查詢,修改,洗掉等等,對應的包是dao包,
三層的處理請求的互動:
用戶使用界面層–> 業務邏輯層—>資料訪問層(持久層)–>資料庫(mysql)
如圖:

為什么使用三層:
1、結構清晰、耦合度低,各層分工明確;
2、可維護性高、可擴展行高;
3、有利于各層邏輯的復用,
1.2、什么是框架
框架(Framework)是整個或部分系統的可重用設計,表現為一組抽象構件及構件實體間互動的方法;框架是可被應用開發者定制的應用骨架、模板,
簡單的說,框架其實是半成品軟體,就是一組組件,供你使用完成你自己的系統,框架是安全的,可復用的,不斷升級的軟體,
1.3、MyBatis框架
一個框架,早期叫做ibatis,代碼在GitHub,
MyBatis是MyBatis SQL Mapper Framework for Java(sql映射框架)
(1)sql mapper:sql映射
可以把資料庫表中的一行資料,映射為一個java物件,一行資料可以看作是一個java物件,操作這個物件就相當于操作表中的資料.
(2)Data Access Object(DAOs):資料訪問,對資料庫執行增刪改查,
1.3.1、使用JDBC的缺陷
1、代碼比較多,開發效率低
2、需要關注Connection,Statement、ResultSet物件創建和銷毀
3、對ResultSet查詢的結果,需要自己封裝為List
4、重復的代碼比較多
1.3.2、MyBatis解決的主要問題
減輕使用JDBC的復雜性,不用撰寫重復的創建Connection,Statement;不用撰寫關閉資源代碼,直接使用java物件,表示結果資料,
1.3.3、MyBatis提供的功能
1、 提供了創建Connection ,Statement, ResultSet的能力 ,不用開發人員創建這些物件了;
2. 提供了執行sql陳述句的能力, 不用你執行sql;
3. 提供了回圈sql, 把sql的結果轉為java物件, List集合的能力;
4. 提供了關閉資源的能力,不用你關閉Connection, Statement, ResultSet,
開發人員做的是:提供sql陳述句,
MyBatis是一個sql映射框架,提供的資料庫的操作能力,增強的JDBC,使用MyBatis讓開發人員集中精神寫sql就可以了,不必關心Connection ,Statement, ResultSet的創建,銷毀,sql的執行,
二、MyBatis使用傳統Dao開發方式
2.1、撰寫Student物體類
public class Student {
//定義屬性,目前要求是屬性名和列名一致
private int id;
private String name;
private String email;
private int age;
//構造方法、set、get、toString
2.2、撰寫StudentDao介面
public interface StudentDao {
//查詢student表中所有資料
List<Student> selectStudents();
//向student表中插入資料,引數student表示向資料庫表中插入的資料
//回傳值int表示執行insert操作后影響資料庫的行數
int insertStudents(Student student);
}
2.3、撰寫Dao介面Mapper映射檔案StudentDao.xml
<?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">
<mapper namespace="com.dao.StudentDao">
<!--
select:表示查詢操作
id:你要執行的sql語法的唯一標識,mybatis會使用這個id的值來找到要執行的sql陳述句
可以自定義,但是前要求使用介面中的方法名稱
resultType:表示結果型別的,是sql陳述句執行后得到ResultSet,遍歷這個ResultSet得到java物件的型別
值寫的是型別的全限定名稱
-->
<select id="selectStudents" resultType="com.domain.Student">
select id,name,email,age from student order by id
</select>
<insert id="insertStudents">
insert into student values(#{id},#{name},#{email},#{age})
</insert>
</mapper>
2.4、創建MyBatis主組態檔
<?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>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<!--環境配置:資料庫的連接資訊
default:必須和某個environment的id值一樣
告訴mybatis使用哪個資料庫的連接資訊,也就是訪問哪個資料庫
-->
<environments default="mydev">
<!--environment : 一個資料庫資訊的配置,環境
id:一個唯一值,自定義,表示環境的名稱
-->
<environment id="mydev">
<!--transactionManager:mybatis的事務型別
type:JDBC(表示使用jdbc中的Connection物件的commit,rollback做事務處理)-->
<transactionManager type="JDBC"/>
<!-- dataSource:表示資料源,連接資料庫的
type:表示資料源的型別,POOLED表示使用連接池-->
<dataSource type="POOLED">
<!--driver,url,username,password是固定的,不能自定義-->
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/ssm"/>
<property name="username" value="root"/>
<property name="password" value="6269131.x"/>
</dataSource>
</environment>
</environments>
<!--sql映射檔案的位置-->
<mappers>
<!--一個mapper標簽它指定一個檔案的位置,
從類路徑開始的路徑資訊, target/clasess(類路徑)-->
<mapper resource="com/dao/StudentDao.xml"/>
</mappers>
</configuration>
配置日志功能:在mybatis.xml檔案加入日志配置,可以在控制臺輸出執行是sql陳述句和引數
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
2.5、創建工具類(MyBatisUtils類)
public class MybatisUtil {
private static SqlSessionFactory factory = null;
static {
//1、定義mybatis主組態檔的名稱,從類路徑的根開始
String config = "mybatis.xml";
try {
InputStream in = Resources.getResourceAsStream(config);
//創建SqlSessionFactory物件,使用SqlSessionFactoryBuilder
factory = new SqlSessionFactoryBuilder().build(in);
} catch (IOException e) {
e.printStackTrace();
}
}
//獲取SqlSession物件
public static SqlSession getSqlSession() {
SqlSession sqlSession = null;
if(factory != null) {
sqlSession = factory.openSession();
}
return sqlSession;
}
}
2.6、創建Dao介面的實作類
public class StudentDaoImpl implements StudentDao {
public List<Student> selectStudents() {
//獲取SqlSession物件
SqlSession sqlSession = MybatisUtil.getSqlSession();
String sqlId = "com.dao.StudentDao.selectStudents";
List<Student> studentList = sqlSession.selectList(sqlId);
sqlSession.close();
return studentList;
}
public int insertStudents(Student student) {
SqlSession sqlSession = MybatisUtil.getSqlSession();
String sqlId = "com.dao.StudentDao.insertStudents";
int nums = sqlSession.insert(sqlId,student);
sqlSession.commit();
sqlSession.close();
return nums;
}
}
2.7、創建測驗類
public class TestMybatis {
@Test
public void selectTest() {
StudentDao dao = new StudentDaoImpl();
List<Student> students = dao.selectStudents();
for(Student stu : students) {
System.out.println(stu);
}
}
@Test
public void insertTest() {
StudentDao dao = new StudentDaoImpl();
Student student = new Student(1017,"張三","zhangsan@qq.com",40);
int nums = dao.insertStudents(student);
System.out.println(nums);
}
}
2.8、主要類介紹
(1)、Resources:mybatis中的一個類,負責讀取主組態檔,回傳不同型別的IO流物件,
InputStream in = Resources.getResourceAsStream(“mybatis.xml”);
(2)、SqlSessionFactoryBuilder : 創建SqlSessionFactory物件,
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder()
SqlSessionFactory factory = builder.build(in);
SqlSessionFactoryBuilder物件在創建完工廠物件后,即可被銷毀,
(3)、SqlSessionFactory介面
重量級物件, 程式創建一個物件耗時比較長,使用資源比較多,
在整個專案中,有一個就夠用了,
SqlSessionFactory:介面,介面實作類: DefaultSqlSessionFactory
SqlSessionFactory作用: 獲取SqlSession物件,SqlSession sqlSession = factory.openSession();
openSession()方法說明:
1. openSession() :無引數的, 獲取是非自動提交事務的SqlSession物件
2. openSession(boolean): openSession(true) 獲取自動提交事務的SqlSession.
openSession(false) 非自動提交事務的SqlSession物件
(4)、SqlSession介面:定義了操作資料的方法 例如 selectList() ,insert(),update(), delete(), commit(), rollback(),
SqlSession介面的實作類DefaultSqlSession,
SqlSession物件不是執行緒安全的,需要在方法內部使用,在執行sql陳述句前,使用openSession()獲取SqlSession物件,在執行完sql陳述句后,需要關閉它,執行SqlSession.close(),這樣才能保證它的使用是執行緒安全的,
三、MyBatis實作Dao的動態代理
3.1Dao的動態代理
使用SqlSession.getMapper(dao介面.class)獲取dao介面的物件,
這樣就不需要Dao介面的實作類
public class MybatisTest {
@Test
/**
* 使用Mybatis的動態代理機制,使用sqlSession.getMapper(dao介面)
* getMapper能獲取dao介面對應的實作類物件
*/
public void selectTest() {
SqlSession sqlSession = MybatisUtil.getSqlSession();
StudentDao dao = sqlSession.getMapper(StudentDao.class);
//com.sun.proxy.$Proxy9: jdk的動態代理
System.out.println(dao.getClass().getName());
//呼叫dao的方法執行資料庫的操作
List<Student> students = dao.selectStudents();
for(Student stu : students) {
System.out.println(stu);
}
}
@Test
public void insertTest() {
SqlSession sqlSession = MybatisUtil.getSqlSession();
StudentDao dao = sqlSession.getMapper(StudentDao.class);
Student student = new Student(1032,"李四","lisi@sina.com",21);
int nums = dao.insertStudents(student);
sqlSession.commit();
System.out.println(nums);
}
}
3.2、深入理解引數
MyBatis傳遞引數:從java代碼中把資料傳入到mapper檔案的sql陳述句中,
3.2.1、parameterType
parameterType:寫在mapper檔案中的一個屬性,表示dao介面中方法的引數的資料型別,
parameterType它的值是java資料型別的全限定名稱或者是mybatis定義的別名
eg:parameterType = “int”
parameterType = “java.lang.Integer”
注意:parameterType不是強制的,mybatis通過反射機制能夠發現介面引數的資料型別,所以可以沒有,一般我們也不寫,
eg:
例如StudentDao介面
public Student selectStudentById(Integer id)
<select id="selectStudentById" parameterType="java.lang.Integer" resultType="com.domain.Student">
select id,name,email,age from student where id = #{studentId}
</select>
3.2.2、一個簡單型別的引數
簡單型別:mybatis把java的基本資料型別和String都叫簡單資料型別,
在mapper檔案獲取簡單型別的一個引數的值,使用#{任意字符}
例如StudentDao介面
public Student selectStudentById(int id)
mapper:
<select id="selectStudentById" resultType="com.domain.Student">
select id,name,email,age from student where id = #{studentId}
</select>
3.2.3、多個引數,使用@Param命名引數
當Dao介面方法有多個引數,需要通過名稱使用引數,在方法形參前面加入@Param(“自定義引數名”),mapper檔案使用#{自定義引數名},
介面方法:
List selectMultiParam(@Param(“myName”) String name,@Param(“myAge”) int age);
mapper:
<select id="selectMultiParam" resultType="com.domain.Student">
select id,name,email,age from student where name = #{myName} or age = #{myAge}
</select>
3.2.4、多個引數-使用java物件
使用java物件傳遞引數,java的屬性值就是sql需要的引數值,每一個屬性就是一個引數,
語法格式:#{屬性名,javaType=java中資料型別名稱,jdbcType=資料庫資料型別名稱} 這是最為完整的方式,但是javaType、jdbcType的值mybatis通過反射機制可以獲取,一般不需要設定,常用格式:#{屬性名}
介面方法:
List selectMultiStudent(Student student);
mapper:
<select id="selectMultiStudent" resultType="com.domain.Student">
select id,name,email,age from student where
name = #{name} or age = #{age}
</select>
或者為:
<select id="selectMultiStudent" resultType="com.domain.Student">
select id,name,email,age from student where name = #{name,javaType=java.lang.String,jdbcType=VARCHAR}
or age = #{age,javaType=java.lang.Integer,jdbcType=INTEGER}
</select>
3.2.5、多個引數-按位置
引數位置從0開始,參考引數語法==#{arg位置}==,第一個引數是#{arg0},第二個引數是#{arg1}
注意:mybatis-3.3版本和之前的版使用#{0},#{1}方式,從mybatis-3.4開始使用#{arg0}方式,
介面方法:
List selectMultiPosition(String name,int age);
mapper:
<!--多個引數使用位置-->
<select id="selectMultiPosition" resultType="com.domain.Student">
select id,name,email,age from student where
name = #{arg0} or age = #{arg1}
</select>
3.2.6、多個引數-使用Map
Map集合可以存盤多個值,使用Map向mapper檔案一次傳入多個引數,Map集合使用String的key,Object型別的值存盤引數,mapper檔案使用==#{key}==參考引數值,
比如:
HashMap<String, Object> map = new HashMap<>();
map.put("name","zhangsan");
map.put("age",20);
介面方法:
List selectMultiMap(Map<String,Object> map);
mapper檔案:
<!--多個引數使用Map,使用語法:#{map的key}-->
<select id="selectMultiMap" resultType="com.domain.Student">
select id,name,email,age from student where
name = #{name} or age = #{age}
</select>
3.2.7、# 和 $
#:占位符,告訴mybatis使用實際的引數值代替,并使用PrepareStatement物件執行sql陳述句,#{…}代替sql陳述句的 “ ?”,這樣做更安全、通常也是首選做法,
例如:
select id,name, email,age from student where id=#{studentId}
#的結果:select id,name, email,age from student where id=?
$:字串替換,告訴mybatis使用 $ 包含的“字串”替換所在位置,使用Statement把sql陳述句和${}的內容連接起來,主要用在替換表名、列名,不同列排序等操作,
例如:
select id,name, email,age from student where id=${studentId}
$的結果:select id,name, email,age from student where id=1001
相當于:String sql=“select id,name, email,age from student where id=” + “1001”;
#和$的區別:
1、#使用?在sql陳述句中做占位的,使用PrepareStatement執行sql,效率高
2、#能夠避免sql注入,更安全
3、$不使用占位符,是字串連接的方法,使用Statement物件執行sql,效率低
4、$有sql注入的風險,缺乏安全性
5、$可以替換表名或者列名
3.3、MyBatis的輸出結果
3.3.1、resultType
resultType:執行sql得到ResultSet轉換的型別,使用型別的完全限定名或者別名,注意如果回傳的是集合,那應該設定為集合包含的型別,而不是集合本身,


1、簡單型別
介面方法:int countStudent();
mapper檔案:
<select id="countStudent" resultType="java.lang.Integer">
select count(*) from student
</select>
2、物件型別
介面方法:List<Student> selectStudent( int id);
mapper檔案:
<select id="selectStudent" resultType="com.domain.Student">
select id,name from student where id = #{id}
</select>
框架的處理:使用構造方法創建物件,呼叫setXXX給屬性賦值,
Student student = new Student();

注意:Dao介面方法的回傳值是集合型別,需要指定集合中的型別,不是集合本身,

3、Map
sql的查詢結果作為Map的key和value,推薦使用Map<Object,Object>.
注意:Map作為介面回傳值,sql陳述句的查詢結果最多只能有一條記錄,大于一條記錄是錯誤,
介面方法:Map<Object,Object> selectMapById(int id);
mapper檔案:
<!--回傳Map (用的少)
1、列名是Map的key,列值是Map的value
2、最多只能回傳一行記錄,多一行是錯誤的
-->
<select id="selectMapById" resultType="java.util.HashMap">
select id,name,email,age from student where id = #{id}
</select>
測驗方法:
@Test
public void testSelectMapById() {
SqlSession sqlSession = MybatisUtil.getSqlSession();
StudentDao dao = sqlSession.getMapper(StudentDao.class);
Map<Object, Object> map = dao.selectMapById(1017);
System.out.println(map);
}
3.3.2、resultMap
resultMap可以自定義sql的結果和java物件屬性的映射關系,更靈活的把列值賦值給指定屬性,
常用在列名和java物件屬性名不一樣的情況,
使用方式:
1、先定義resultMap,指定列名和屬性的對應關系,
2、在<select>標簽中把resultType替換為resultMap,
介面方法:List<MyStudent> selectMyStudent();
mapper檔案:
<!--列名和屬性名不一致的第一種方式:使用resultMap
1、先定義resultMap
2、在select標簽中參考1定義的resultMap
-->
<!--定義resultMap
id:自定義名稱,表示你定義的這個resultMap
type:javaleix的全限定名稱
-->
<resultMap id="MyStudentMap" type="com.domain.MyStudent">
<!--列名和java屬性的關系-->
<!--主鍵列,使用id標簽
column:列名
property:java物件的屬性名
-->
<id column="id" property="stuId"/>
<!--非主鍵欄位使用result-->
<result column="name" property="stuName"/>
<result column="email" property="stuEmail"/>
<result column="age" property="stuAge"/>
</resultMap>
<select id="selectMyStudent" resultMap="MyStudentMap">
select id,name,email,age from student
</select>
3.3.3、物體類屬性名和列名不同的處理方式
(1)、使用列別名和<resultType>
<!--
resultType的默認原則是: 同名的列值賦給同名的屬性,使用列別名(java屬性名)
-->
<select id="selectMyStudentDiffProperty" resultType="com.domain.MyStudent">
select id as stuId,name as stuName,email as stuEmali,age as stuAge from student
</select>
(2)、使用<resultMap> (推薦使用)
注意:resultMap和resultType不要一起用,二選一
3.3.4、模糊 like
使用like進行模糊查詢的第一種方式 : 在java代碼中指定like的內容,
<select id="selectLikeOne" resultType="com.domain.Student">
select id,name,email,age from student where name like #{name}
</select>
測驗程式:
@Test
public void testSelectLikeOne() {
SqlSession sqlSession = MybatisUtil.getSqlSession();
StudentDao dao = sqlSession.getMapper(StudentDao.class);
String name = "%楊%";
List<Student> students = dao.selectLikeOne(name);
for (Student stu : students) {
System.out.println("student = " + stu);
}
sqlSession.close();
}
使用like進行模糊查詢的第二種方式:在mapper檔案中拼接like的內容,
<select id="selectLikeTwo" resultType="com.domain.Student">
select id,name,email,age from student where name like "%" #{name} "%"
</select>
測驗程式:
@Test
public void testSelectLikeTwo() {
SqlSession sqlSession = MybatisUtil.getSqlSession();
StudentDao dao = sqlSession.getMapper(StudentDao.class);
String name = "楊";
List<Student> students = dao.selectLikeTwo(name);
for (Student stu : students) {
System.out.println("student = " + stu);
}
sqlSession.close();
}
四、動態SQL
在mapper的動態SQL中若出現大于號、小于號,大于等于號,小于等于號等符號,最好將其轉為物體符號,否則,XML可能會出現決議出錯問題,
特別是對于小于號(<),在XML
z紅是絕不能出現的,否則決議mapper檔案會出錯,
物體符號表:

4.1、if標簽
對于該標簽的執行,當test的值為true時,會將其包含的SQL片段拼接到其所在的SQL陳述句中,
語法:<if test=“條件”> sql陳述句的部分 </if>
mapper檔案:
<select id="selectStudentIf" resultType="com.domain.Student">
select id,name,email,age from student where true
<if test="name != null and name != ''">
name = #{name}
</if>
<if test="age > 0">
or age > #{age}
</if>
</select>
4.2、where標簽
<if/>標簽中存在一個麻煩的地方,需要在where后手工添加1=1的子句,因為,若where后的所有<if/>條件均為false,而where后若沒有1=1子句,則sql中就會剩下一個空的where,sql出錯,所以在where后需要添加永為真子句1=1,以防這種情況發生,但當資料量很大的時候,會嚴重影響查詢效率,
使用<where/>標簽,在有查詢條件時,可以自動添上where子句,沒有查詢條件時,不會添加where子句,注意:第一個<if/>標簽中的sql片段,可以不包含and,不過寫上也不會出錯,系統會將多余的and去掉,但其它<if/>中sql片斷的and,必須要求寫上,否則sql陳述句將拼接出錯,
<select id="selectStudentWhere" resultType="com.domain.Student">
select id,name,email,age from student
<where>
<if test="name != null and name != ''">
name = #{name}
</if>
<if test="age > 0">
or age = #{age}
</if>
</where>
</select>
4.3、foreach標簽
<foreach/>標簽用于實作對于陣列與集合的遍歷,
collection:表示要遍歷的集合型別,list,array等
open、close、separator為對遍歷內容的SQL拼接,
語法:

遍歷簡單型別:
mapper檔案:
<select id="selectStudentForOne" resultType="com.domain.Student">
select id,name,email,age from student where id in
<foreach collection="list" item="myid" open="(" close=")" separator=",">
#{myid}
</foreach>
</select>
測驗程式:
@Test
public void selectStudentForOne() {
SqlSession sqlSession = MybatisUtil.getSqlSession();
StudentDao dao = sqlSession.getMapper(StudentDao.class);
List<Integer> list = new ArrayList<>();
list.add(1032);
list.add(1017);
list.add(1001);
List<Student> students = dao.selectStudentForOne(list);
for(Student stu : students) {
System.out.println("student= " + stu);
}
sqlSession.close();
}
遍歷物件型別:
mapper檔案:
<select id="selectStudentForTwo" resultType="com.domain.Student">
select id,name,email,age from student where id in
<foreach collection="list" item="student" open="(" close=")" separator=",">
#{student.id}
</foreach>
</select>
測驗程式:
@Test
public void selectStudentForTwo() {
SqlSession sqlSession = MybatisUtil.getSqlSession();
StudentDao dao = sqlSession.getMapper(StudentDao.class);
List<Student> stuList = new ArrayList<>();
Student s1 = new Student();
s1.setId(1001);
stuList.add(s1);
s1 = new Student();
s1.setId(1017);
stuList.add(s1);
List<Student> students = dao.selectStudentForTwo(stuList);
for(Student stu : students) {
System.out.println("student= " + stu);
}
sqlSession.close();
}
4.4、代碼片段
<sql/>標簽用于定義SQL片段,以便其他SQL標簽復用,而其他標簽使用該SQL片段,需要使用<include/>子標簽,該<sql/>標簽可以定義SQL陳述句中的任何部分,所以子標簽可以放在動態SQL的任何位置,
mapper檔案:
<sql id="studentSql">
select id,name,email,age from student
</sql>
<select id="selectStudentForTwo" resultType="com.domain.Student">
<include refid="studentSql"/>
where id in
<foreach collection="list" item="student" open="(" close=")" separator=",">
#{student.id}
</foreach>
</select>
4.5、總結

五、MyBatis主組態檔

1、資料庫的屬性組態檔:把資料庫連接資訊放到一個單獨的檔案中,和mybatis主組態檔分開,目的是便于修改,保存,處理多個資料庫的資訊,
1)在resource目錄中定義一個屬性組態檔,xxx.properties,在屬性組態檔中,定義資料,格式是key=value
key:一般使用 . 做多級目錄的,
2)在mybatis的主組態檔,使用<property>指定檔案的位置,在需要使用值的地方,$(key)
2、mapper檔案,使用package指定路徑
<mappers>
<package name="com.dao"/>
</mappers>
name:xml檔案(mapper檔案)所在的包名,這個包中所有xml檔案一次都能加載給mybatis
使用package的要求:
1、mapper檔案名稱需要和介面名稱一樣,區分大小寫的一樣
2、mapper檔案和dao介面需要在同一目錄
六、MyBatis擴展
基于PageHelper的分頁
1)maven坐標
<!--pageHelper的依賴-->
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.0</version>
</dependency>
2)加入plugin配置:
<!--在<environments>之前加入-->
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor"/>
</plugins>
3)PageHelper物件
查詢陳述句之前呼叫PageHelper.startPage靜態方法,
在你需要進行分頁的Mybatis查詢方法前呼叫PageHelper.startPage靜態方法即可,緊跟在這個方法后的第一個MyBatis查詢方法會被進行分頁,
@Test
public void testSelectAllByPageHelper() {
SqlSession sqlSession = MybatisUtil.getSqlSession();
StudentDao dao = sqlSession.getMapper(StudentDao.class);
PageHelper.startPage(2,3);
List<Student> students = dao.selectAll();
for(Student stu : students) {
System.out.println("student= " + stu);
}
sqlSession.close();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/393128.html
標籤:java
上一篇:LeetCode-劍指 Offer 35. 復雜鏈表的復制-Java
下一篇:【設計模式】單例模式
