主頁 > 後端開發 > MyBatis筆記

MyBatis筆記

2021-12-26 08:24:23 後端開發

文章目錄

  • 一、框架概述
    • 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

下一篇:【設計模式】單例模式

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 【C++】Microsoft C++、C 和匯編程式檔案

    ......

    uj5u.com 2020-09-10 00:57:23 more
  • 例外宣告

    相比于斷言適用于排除邏輯上不可能存在的狀態,例外通常是用于邏輯上可能發生的錯誤。 例外宣告 Item 1:當函式不可能拋出例外或不能接受拋出例外時,使用noexcept 理由 如果不打算拋出例外的話,程式就會認為無法處理這種錯誤,并且應當盡早終止,如此可以有效地阻止例外的傳播與擴散。 示例 //不可 ......

    uj5u.com 2020-09-10 00:57:27 more
  • Codeforces 1400E Clear the Multiset(貪心 + 分治)

    鏈接:https://codeforces.com/problemset/problem/1400/E 來源:Codeforces 思路:給你一個陣列,現在你可以進行兩種操作,操作1:將一段沒有 0 的區間進行減一的操作,操作2:將 i 位置上的元素歸零。最終問:將這個陣列的全部元素歸零后操作的最少 ......

    uj5u.com 2020-09-10 00:57:30 more
  • UVA11610 【Reverse Prime】

    本人看到此題沒有翻譯,就附帶了一個自己的翻譯版本 思考 這一題,它的第一個要求是找出所有 $7$ 位反向質數及其質因數的個數。 我們應該需要質數篩篩選1~$10^{7}$的所有數,這里就不慢慢介紹了。但是,重讀題,我們突然發現反向質數都是 $7$ 位,而將它反過來后的數字卻是 $6$ 位數,這就說明 ......

    uj5u.com 2020-09-10 00:57:36 more
  • 統計區間素數數量

    1 #pragma GCC optimize(2) 2 #include <bits/stdc++.h> 3 using namespace std; 4 bool isprime[1000000010]; 5 vector<int> prime; 6 inline int getlist(int ......

    uj5u.com 2020-09-10 00:57:47 more
  • C/C++編程筆記:C++中的 const 變數詳解,教你正確認識const用法

    1、C中的const 1、區域const變數存放在堆疊區中,會分配記憶體(也就是說可以通過地址間接修改變數的值)。測驗代碼如下: 運行結果: 2、全域const變數存放在只讀資料段(不能通過地址修改,會發生寫入錯誤), 默認為外部聯編,可以給其他源檔案使用(需要用extern關鍵字修飾) 運行結果: ......

    uj5u.com 2020-09-10 00:58:04 more
  • 【C++犯錯記錄】VS2019 MFC添加資源不懂如何修改資源宏ID

    1. 首先在資源視圖中,添加資源 2. 點擊新添加的資源,復制自動生成的ID 3. 在解決方案資源管理器中找到Resource.h檔案,編輯,使用整個專案搜索和替換的方式快速替換 宏宣告 4. Ctrl+Shift+F 全域搜索,點擊查找全部,然后逐個替換 5. 為什么使用搜索替換而不使用屬性視窗直 ......

    uj5u.com 2020-09-10 00:59:11 more
  • 【C++犯錯記錄】VS2019 MFC不懂的批量添加資源

    1. 打開資源頭檔案Resource.h,在其中預先定義好宏 ID(不清楚其實ID值應該設定多少,可以先新建一個相同的資源項,再在這個資源的ID值的基礎上遞增即可) 2. 在資源視圖中選中專案資源,按F7編輯資源檔案,按 ID 型別 相對路徑的形式添加 資源。(別忘了先把檔案拷貝到專案中的res檔案 ......

    uj5u.com 2020-09-10 01:00:19 more
  • C/C++編程筆記:關于C++的參考型別,專供新手入門使用

    今天要講的是C++中我最喜歡的一個用法——參考,也叫別名。 參考就是給一個變數名取一個變數名,方便我們間接地使用這個變數。我們可以給一個變數創建N個參考,這N + 1個變數共享了同一塊記憶體區域。(參考型別的變數會占用記憶體空間,占用的記憶體空間的大小和指標型別的大小是相同的。雖然參考是一個物件的別名,但 ......

    uj5u.com 2020-09-10 01:00:22 more
  • 【C/C++編程筆記】從頭開始學習C ++:初學者完整指南

    眾所周知,C ++的學習曲線陡峭,但是花時間學習這種語言將為您的職業帶來奇跡,并使您與其他開發人員區分開。您會更輕松地學習新語言,形成真正的解決問題的技能,并在編程的基礎上打下堅實的基礎。 C ++將幫助您養成良好的編程習慣(即清晰一致的編碼風格,在撰寫代碼時注釋代碼,并限制類內部的可見性),并且由 ......

    uj5u.com 2020-09-10 01:00:41 more
最新发布
  • Rust中的智能指標:Box<T> Rc<T> Arc<T> Cell<T> RefCell<T> Weak

    Rust中的智能指標是什么 智能指標(smart pointers)是一類資料結構,是擁有資料所有權和額外功能的指標。是指標的進一步發展 指標(pointer)是一個包含記憶體地址的變數的通用概念。這個地址參考,或 ” 指向”(points at)一些其 他資料 。參考以 & 符號為標志并借用了他們所 ......

    uj5u.com 2023-04-20 07:24:10 more
  • Java的值傳遞和參考傳遞

    值傳遞不會改變本身,參考傳遞(如果傳遞的值需要實體化到堆里)如果發生修改了會改變本身。 1.基本資料型別都是值傳遞 package com.example.basic; public class Test { public static void main(String[] args) { int ......

    uj5u.com 2023-04-20 07:24:04 more
  • [2]SpinalHDL教程——Scala簡單入門

    第一個 Scala 程式 shell里面輸入 $ scala scala> 1 + 1 res0: Int = 2 scala> println("Hello World!") Hello World! 檔案形式 object HelloWorld { /* 這是我的第一個 Scala 程式 * 以 ......

    uj5u.com 2023-04-20 07:23:58 more
  • 理解函式指標和回呼函式

    理解 函式指標 指向函式的指標。比如: 理解函式指標的偽代碼 void (*p)(int type, char *data); // 定義一個函式指標p void func(int type, char *data); // 宣告一個函式func p = func; // 將指標p指向函式func ......

    uj5u.com 2023-04-20 07:23:52 more
  • Django筆記二十五之資料庫函式之日期函式

    本文首發于公眾號:Hunter后端 原文鏈接:Django筆記二十五之資料庫函式之日期函式 日期函式主要介紹兩個大類,Extract() 和 Trunc() Extract() 函式作用是提取日期,比如我們可以提取一個日期欄位的年份,月份,日等資料 Trunc() 的作用則是截取,比如 2022-0 ......

    uj5u.com 2023-04-20 07:23:45 more
  • 一天吃透JVM面試八股文

    什么是JVM? JVM,全稱Java Virtual Machine(Java虛擬機),是通過在實際的計算機上仿真模擬各種計算機功能來實作的。由一套位元組碼指令集、一組暫存器、一個堆疊、一個垃圾回收堆和一個存盤方法域等組成。JVM屏蔽了與作業系統平臺相關的資訊,使得Java程式只需要生成在Java虛擬機 ......

    uj5u.com 2023-04-20 07:23:31 more
  • 使用Java接入小程式訂閱訊息!

    更新完微信服務號的模板訊息之后,我又趕緊把微信小程式的訂閱訊息給實作了!之前我一直以為微信小程式也是要企業才能申請,沒想到小程式個人就能申請。 訊息推送平臺🔥推送下發【郵件】【短信】【微信服務號】【微信小程式】【企業微信】【釘釘】等訊息型別。 https://gitee.com/zhongfuch ......

    uj5u.com 2023-04-20 07:22:59 more
  • java -- 緩沖流、轉換流、序列化流

    緩沖流 緩沖流, 也叫高效流, 按照資料型別分類: 位元組緩沖流:BufferedInputStream,BufferedOutputStream 字符緩沖流:BufferedReader,BufferedWriter 緩沖流的基本原理,是在創建流物件時,會創建一個內置的默認大小的緩沖區陣列,通過緩沖 ......

    uj5u.com 2023-04-20 07:22:49 more
  • Java-SpringBoot-Range請求頭設定實作視頻分段傳輸

    老實說,人太懶了,現在基本都不喜歡寫筆記了,但是網上有關Range請求頭的文章都太水了 下面是抄的一段StackOverflow的代碼...自己大修改過的,寫的注釋挺全的,應該直接看得懂,就不解釋了 寫的不好...只是希望能給視頻網站開發的新手一點點幫助吧. 業務場景:視頻分段傳輸、視頻多段傳輸(理 ......

    uj5u.com 2023-04-20 07:22:42 more
  • Windows 10開發教程_編程入門自學教程_菜鳥教程-免費教程分享

    教程簡介 Windows 10開發入門教程 - 從簡單的步驟了解Windows 10開發,從基本到高級概念,包括簡介,UWP,第一個應用程式,商店,XAML控制元件,資料系結,XAML性能,自適應設計,自適應UI,自適應代碼,檔案管理,SQLite資料庫,應用程式到應用程式通信,應用程式本地化,應用程式 ......

    uj5u.com 2023-04-20 07:22:35 more