最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

MyBatis詳細講解DAO代理的使用

 更新時間:2022年04月07日 12:38:25   作者:托馬斯-酷濤  
MyBatis允許只聲明一個dao接口,而無需寫dao實現(xiàn)類的方式實現(xiàn)數據庫操作。前提是必須保證Mapper文件中的<mapper>標簽的namespace屬性值必須要和dao接口的類路徑一致,MyBatis容器會自動通過動態(tài)代理生成接口的實現(xiàn)類

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

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

2、getMapper獲取代理對象

只需調用 SqlSession 的 getMapper()方法,即可獲取指定接口的實現(xiàn)類對 象。該方法的參數為指定 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 添加數據:"+nums);
}

4、深入理解參數

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

parameterType

parameterType: 接口中方法參數的類型, 類型的完全限定名或別名。這個屬 性是可選的,因為 MyBatis 可以推斷出具體傳入語句的參數,默認值為未設置 (unset)。接口中方法的參數從 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>

一個簡單參數

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

接口方法

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 是自定義的變量名稱,和方法參數名無關。

測試方法

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

使用@Param

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

例如定義 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 對象傳遞參數, java 的屬性值就是 sql 需要的參數值。 每一個屬性就是一個參數。

語法格式: #{ property,javaType=java 中數據類型名

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

?創(chuàng)建保存參數值的對象 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代理內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 使用synchronized實現(xiàn)一個Lock代碼詳解

    使用synchronized實現(xiàn)一個Lock代碼詳解

    這篇文章主要介紹了使用synchronized實現(xiàn)一個Lock代碼詳解,具有一定借鑒價值,需要的朋友可以參考下。
    2017-12-12
  • Java調用IK分詞器進行分詞方式,封裝工具類

    Java調用IK分詞器進行分詞方式,封裝工具類

    這篇文章主要介紹了Java調用IK分詞器進行分詞方式,封裝工具類,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • Java 在Word中創(chuàng)建郵件合并模板并合并文本和圖片的操作方法

    Java 在Word中創(chuàng)建郵件合并模板并合并文本和圖片的操作方法

    通過Java程序展示如何來實現(xiàn)創(chuàng)建模板,并通過郵件合并功能來合并文本數據和圖片數據的方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2021-07-07
  • Spring?Boot應用中如何動態(tài)指定數據庫實現(xiàn)不同用戶不同數據庫的問題

    Spring?Boot應用中如何動態(tài)指定數據庫實現(xiàn)不同用戶不同數據庫的問題

    讓我們創(chuàng)建一個 Spring Boot 項目首先設置一個具有必要依賴項的新 Spring Boot項目,在項目配置中包括 Spring Web、Spring Data JPA 和關于數據庫的依賴項,接下來介紹Spring?Boot應用中如何動態(tài)指定數據庫,實現(xiàn)不同用戶不同數據庫的場景?,需要的朋友可以參考下
    2024-04-04
  • java怎樣動態(tài)獲取泛型參數的類型

    java怎樣動態(tài)獲取泛型參數的類型

    在Java中,泛型信息在編譯時會被擦除,但可以通過特定API獲取運行時的泛型參數類型,主要API包括Class的getGenericSuperclass()和getGenericInterfaces()方法,以及ParameterizedType的getActualTypeArguments()方法
    2024-09-09
  • 詳解spring boot集成ehcache 2.x 用于hibernate二級緩存

    詳解spring boot集成ehcache 2.x 用于hibernate二級緩存

    本篇文章主要介紹了詳解spring boot集成ehcache 2.x 用于hibernate二級緩存,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • MyBatis Generator去掉生成的注解

    MyBatis Generator去掉生成的注解

    這篇文章主要介紹了MyBatis Generator去掉生成的注解的相關資料,非常不錯具有一定的參考借鑒價值,需要的朋友可以參考下
    2016-11-11
  • Apache?Arrow?Parquet存儲與使用

    Apache?Arrow?Parquet存儲與使用

    這篇文章主要為大家介紹了Apache?Arrow?Parquet存儲與使用原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-08-08
  • SpringBoot中的事務回滾規(guī)則詳解

    SpringBoot中的事務回滾規(guī)則詳解

    這篇文章主要介紹了SpringBoot中的事務回滾規(guī)則詳解,事務是指一系列的操作,這些操作要么全部成功,要么全部失敗。在Spring Boot中,我們可以使用事務管理器來管理事務,在使用事務管理器的時候,一個非常重要的概念就是事務回滾,需要的朋友可以參考下
    2023-07-07
  • Spring Boot自定義favicon實現(xiàn)方法實例解析

    Spring Boot自定義favicon實現(xiàn)方法實例解析

    這篇文章主要介紹了Spring Boot自定義favicon實現(xiàn)方法實例解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-08-08

最新評論

赣榆县| 梁平县| 封开县| 大港区| 东乌珠穆沁旗| 交城县| 通海县| 信丰县| 白银市| 南安市| 古交市| 信阳市| 黑水县| 锦州市| 邹平县| 永善县| 黄冈市| 白水县| 淮南市| 金塔县| 东乌| 东台市| 临漳县| 海淀区| 巴林右旗| 孟津县| 仙桃市| 金山区| 公主岭市| 滕州市| 高邮市| 四子王旗| 博客| 弥勒县| 清远市| 邹城市| 长兴县| 虎林市| 镇宁| 宁强县| 阳信县|