日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

MyBatis接口代理

發布時間:2025/3/15 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MyBatis接口代理 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

MyBatis接口代理:

  • 采用 Mybatis 的代理開發方式實現 DAO 層的開發,這種方式是目前的主流方式。
  • Mapper 接口開發方法只需要程序員編寫Mapper 接口(相當于Dao 接口),由Mybatis 框架根據接口定義創建接口的動態代理對象,代理對象的方法體同上邊Dao接口實現類方法。

Mapper接口開發需要遵循以下規范:

  • 映射配置文件中的namespace與mapper接口的類名相同
  • 映射配置文件中的增刪改查標簽的id屬性要和Mappe接口中的方法名相同
  • 映射配置文件中的增刪改查標簽的parameterType屬性要和Mappe接口中的方法參數相同
  • 映射配置文件中的增刪改查標簽的resultType屬性要和Mappe接口中方法的返回值相同
  • 接口代理原理:

    分析動態代理對象如何生成的?

    通過動態代理開發模式,我們只編寫一個接口,不寫實現類,我們通過 getMapper() 方法最終獲取到 org.apache.ibatis.binding.MapperProxy 代理對象,然后執行功能,而這個代理對象正是 MyBatis 使用了 JDK 的動態代理技術,幫助我們生成了代理實現類對象。從而可以進行相關持久化操作。

    分析方法是如何執行的?

    動態代理實現類對象在執行方法的時候最終調用了 mapperMethod.execute() 方法,這個方法中通過 switch 語句根據操作類型來判斷是新增、修改、刪除、查詢操作,最后一步回到了 MyBatis 最原生的 SqlSession 方式來執行增刪改查。

    接口代理實現演示:

  • 編寫StudentMapper接口
  • public interface StudentMapper {//查詢全部public abstract List<Student> selectAll();//根據id查詢public abstract Student selectById(Integer id);//新增數據public abstract Integer insert(Student stu);//修改數據public abstract Integer update(Student stu);//刪除數據public abstract Integer delete(Integer id); }
  • Service接口
  • public interface StudentService {//查詢全部public abstract List<Student> selectAll();//根據id查詢public abstract Student selectById(Integer id);//新增數據public abstract Integer insert(Student stu);//修改數據public abstract Integer update(Student stu);//刪除數據public abstract Integer delete(Integer id); }
  • 映射文件
  • <?xml version="1.0" encoding="UTF-8" ?> <!--MyBatis的DTD約束--> <!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!--mapper:核心根標簽namespace屬性:名稱空間 --> <mapper namespace="com.mybatis.mapper.StudentMapper"><!--select:查詢功能的標簽id屬性:唯一標識resultType屬性:指定結果映射對象類型parameterType屬性:指定參數映射對象類型--><select id="selectAll" resultType="student">SELECT * FROM student</select><select id="selectById" resultType="student" parameterType="int">SELECT * FROM student WHERE id = #{id}</select><!--對于增刪改返回的都是影響行數,所以resultType屬性可以不寫--><insert id="insert" parameterType="student">INSERT INTO student VALUES (#{id},#{name},#{age})</insert><update id="update" parameterType="student">UPDATE student SET name = #{name},age = #{age} WHERE id = #{id}</update><delete id="delete" parameterType="int">DELETE FROM student WHERE id = #{id}</delete> </mapper>
  • Service實現類
  • public class StudentMapperImpl implements StudentService {@Overridepublic List<Student> selectAll() {List<Student> list = null;SqlSession sqlSession = null;InputStream rs = null;try {// 加載核心配置文件rs = Resources.getResourceAsStream("MybatisConfig.xml");// 獲取工廠對象SqlSessionFactory build = new SqlSessionFactoryBuilder().build(rs);// 獲取SqlSession對象sqlSession = build.openSession(true);// 獲取接口實現類對象StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);// 通過實現類對象調用方法,接收結果list = mapper.selectAll();} catch (IOException e) {e.printStackTrace();} finally {// 釋放資源SSif (sqlSession != null) {sqlSession.close();}if (rs != null) {try {rs.close();} catch (IOException e) {e.printStackTrace();}}}// 返回結果return list;}@Overridepublic Student selectById(Integer id) {Student stu = null;SqlSession sqlSession = null;InputStream rs = null;try {// 加載核心配置文件rs = Resources.getResourceAsStream("MybatisConfig.xml");// 獲取工廠對象SqlSessionFactory build = new SqlSessionFactoryBuilder().build(rs);// 獲取SqlSession對象sqlSession = build.openSession(true);// 獲取接口實現類對象StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);// 通過實現類對象調用方法,接收結果stu = mapper.selectById(id);} catch (IOException e) {e.printStackTrace();} finally {// 釋放資源SSif (sqlSession != null) {sqlSession.close();}if (rs != null) {try {rs.close();} catch (IOException e) {e.printStackTrace();}}}return stu;}@Overridepublic Integer insert(Student stu) {Integer result = null;SqlSession sqlSession = null;InputStream rs = null;try {// 加載核心配置文件rs = Resources.getResourceAsStream("MybatisConfig.xml");// 獲取工廠對象SqlSessionFactory build = new SqlSessionFactoryBuilder().build(rs);// 獲取SqlSession對象sqlSession = build.openSession(true);// 獲取接口實現類對象StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);// 通過實現類對象調用方法,接收結果result = mapper.insert(stu);} catch (IOException e) {e.printStackTrace();} finally {// 釋放資源SSif (sqlSession != null) {sqlSession.close();}if (rs != null) {try {rs.close();} catch (IOException e) {e.printStackTrace();}}}return result;}@Overridepublic Integer update(Student stu) {Integer result = null;SqlSession sqlSession = null;InputStream rs = null;try {// 加載核心配置文件rs = Resources.getResourceAsStream("MybatisConfig.xml");// 獲取工廠對象SqlSessionFactory build = new SqlSessionFactoryBuilder().build(rs);// 獲取SqlSession對象sqlSession = build.openSession(true);// 獲取接口實現類對象StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);result = mapper.update(stu);} catch (IOException e) {e.printStackTrace();} finally {// 釋放資源SSif (sqlSession != null) {sqlSession.close();}if (rs != null) {try {rs.close();} catch (IOException e) {e.printStackTrace();}}}return result;}@Overridepublic Integer delete(Integer id) {Integer result = null;SqlSession sqlSession = null;InputStream rs = null;try {// 加載核心配置文件rs = Resources.getResourceAsStream("MybatisConfig.xml");// 獲取工廠對象SqlSessionFactory build = new SqlSessionFactoryBuilder().build(rs);// 獲取SqlSession對象sqlSession = build.openSession(true);// 獲取接口實現類對象StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);result = mapper.delete(id);} catch (IOException e) {e.printStackTrace();} finally {// 釋放資源SSif (sqlSession != null) {sqlSession.close();}if (rs != null) {try {rs.close();} catch (IOException e) {e.printStackTrace();}}}return result;} }
  • 測試類
  • public class StuTest {// 創建業務層對象private StudentService service = new StudentMapperImpl();//查詢全部功能測試@Testpublic void selectAll() {List<Student> students = service.selectAll();for (Student stu : students) {System.out.println(stu);}}//根據id查詢功能測試@Testpublic void selectById() {Student stu = service.selectById(3);System.out.println(stu);}//新增功能測試@Testpublic void insert() {Student stu = new Student(4,"趙六",26);Integer result = service.insert(stu);System.out.println(result);}//修改功能測試@Testpublic void update() {Student stu = new Student(4,"趙六",16);Integer result = service.update(stu);System.out.println(result);}//刪除功能測試@Testpublic void delete() {Integer result = service.delete(4);System.out.println(result);} }

    總結

    以上是生活随笔為你收集整理的MyBatis接口代理的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。