溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

MyBatis中DAO代理怎么使用

發(fā)布時間:2022-04-07 15:46:01 來源:億速云 閱讀:166 作者:iii 欄目:開發(fā)技術

這篇文章主要介紹了MyBatis中DAO代理怎么使用的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇MyBatis中DAO代理怎么使用文章都會有所收獲,下面我們一起來看看吧。

    DAO代理實現(xiàn)數(shù)據(jù)庫操作

    1、去掉Dao接口實現(xiàn)類

    MyBatis中DAO代理怎么使用

    2、getMapper獲取代理對象

    只需調用 SqlSession 的 getMapper()方法,即可獲取指定接口的實現(xiàn)類對 象。該方法的參數(shù)為指定 Dao 接口類的 class 值。

    SqlSession session = factory.openSession();
    StudentDao dao = session.getMapper(StudentDao.class);

    使用工具類

    StudentDao studentDao = 
    MyBatisUtil.getSqlSession().getMapper(StudentDao.class);

    getMapper()創(chuàng)建的對象,是代替我們自己創(chuàng)建的 StudentDaoImpl 類

    3、使用 Dao 代理對象方法執(zhí)行 sql 語句

    select方法進行查詢

    @Test
    public void testSelect() throws IOException {
     final List<Student> studentList = studentDao.selectStudents();
     studentList.forEach( stu -> System.out.println(stu));
    }

    insert方法進行插入

    @Test
    public void testInsert() throws IOException {
     Student student = new Student();
     student.setId(1006);
     student.setName("林浩");
     student.setEmail("linhao@163.com");
     student.setAge(26);
     int nums = studentDao.insertStudent(student);
     System.out.println("使用 Dao 添加數(shù)據(jù):"+nums);
    }

    4、深入理解參數(shù)

    從 java 代碼中把參數(shù)傳遞到 mapper.xml 文件。

    parameterType

    parameterType: 接口中方法參數(shù)的類型, 類型的完全限定名或別名。這個屬 性是可選的,因為 MyBatis 可以推斷出具體傳入語句的參數(shù),默認值為未設置 (unset)。接口中方法的參數(shù)從 java 代碼傳入到 mapper 文件的 sql 語句。

    • int 或 java.lang.Integer

    • hashmap 或 java.util.HashMap

    • list 或 java.util.ArrayList

    • student 或 com.bjpowernode.domain.Student

    <select>,<insert>,<update>,<delete>都可以使用 parameterType 指定類型。

    eg:

    <delete id="deleteStudent" parameterType="int">
     delete from student where id=#{studentId}
    </delete>
    等同于
    <delete id="deleteStudent" parameterType="java.lang.Integer">
     delete from student where id=#{studentId}
    </delete>

    一個簡單參數(shù)

    Dao 接口中方法的參數(shù)只有一個簡單類型(java 基本類型和 String),占位符 #{ 任意字符 },和方法的參數(shù)名無關。

    接口方法

    Student selectById(int id);

    mapper文件

    <select id="selectById" resultType="com.bjpowernode.domain.Student">
     select id,name,email,age from student where id=#{studentId}
    </select>

    #{studentId} , studentId 是自定義的變量名稱,和方法參數(shù)名無關。

    測試方法

    @Test
    public void testSelectById(){
     //一個參數(shù) 
     Student student = studentDao.selectById(1005);
     System.out.println("查詢 id 是 1005 的學生:"+student);
    }

    使用@Param

    當 Dao 接口方法多個參數(shù),需要通過名稱使用參數(shù)。 在方法形參前面加 入@Param(“自定義參數(shù)名”),mapper 文件使用#{自定義參數(shù)名}。

    例如定義 List<Student> selectStudent( @Param(“personName”) 

    String name ) { … } 

    mapper 文件 select * from student where name = 

    #{ personName}

    接口方法

    List<Student> selectMultiParam(@Param("personName") String name,
     @Param("personAge") int age);

    Mapper文件

    <select id="selectMultiParam" resultType="com.bjpowernode.domain.Student">
     select id,name,email,age from student where name=#{personName} or age 
    =#{personAge}
    </select>

    測試方法

    @Test
    public void testSelectMultiParam(){
     List<Student> stuList = studentDao.selectMultiParam("李力",20);
     stuList.forEach( stu -> System.out.println(stu));
    }

    使用對象

    使用 java 對象傳遞參數(shù), java 的屬性值就是 sql 需要的參數(shù)值。 每一個屬性就是一個參數(shù)。

    語法格式: #{ property,javaType=java 中數(shù)據(jù)類型名

    jdbcType=數(shù)據(jù)類型名稱 } javaType, jdbcType 的類型 MyBatis 可以檢測出來,一般不需要設置。常用格式 #{ property }

    MyBatis中DAO代理怎么使用

     創(chuàng)建保存參數(shù)值的對象 QueryParam

    package com.bjpowernode.vo; 
    public class QueryParam {
     private String queryName;
     private int queryAge;
     //set ,get 方法
    }

    接口方法

    List<Student> selectMultiObject(QueryParam queryParam);

    Mapper文件

    <select id="selectMultiObject" resultType="com.bjpowernode.domain.Student">
     select id,name,email,age from student where name=#{queryName} or age 
    =#{queryAge}
    </select>
    或
    <select id="selectMultiObject" resultType="com.bjpowernode.domain.Student">
     select id,name,email,age from student
     where name=#{queryName,javaType=string,jdbcType=VARCHAR}
     or age =#{queryAge,javaType=int,jdbcType=INTEGER}
    </select>

    測試方法

    @Test
    public void selectMultiObject(){
     QueryParam qp = new QueryParam();
     qp.setQueryName("李力");
     qp.setQueryAge(20);
     List<Student> stuList = studentDao.selectMultiObject(qp);
     stuList.forEach( stu -> System.out.println(stu));
    }

    關于“MyBatis中DAO代理怎么使用”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“MyBatis中DAO代理怎么使用”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業(yè)資訊頻道。

    向AI問一下細節(jié)

    免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

    AI