av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

MyBatis使用級聯操作解決lombok構造方法識別失敗問題

瀏覽:6日期:2023-10-23 15:58:07

先解決一下idea無法識別lombok構造方法的問題,解決方案是在idea的插件中下載并安裝lombok插件。

MyBatis級聯操作,列舉最簡單的student-classes(學生與班級)的關系表:

create table if not exists student ( id int primary key auto_increment, name varchar(20) not null comment ’學生姓名’, cid int not null comment ’班級id’);

create table if not exists classes ( id int primary key auto_increment, name varchar(20) not null comment ’班級名’);

接下來,創建關系表對應的實體類:

@Datapublic class Student { private long id; private String name; private Classes classes;}

@Datapublic class Classes { private long id; private String name; private List<Student> students;}

在repository包下新建StudentRepository接口:

public interface StudentRepository { public Student findById(long id);}

然后創建對應的mapper文件StudentRepository.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.wts.repository.StudentRepository'> <resultMap type='com.wts.entity.Student'> <id column='id' property='id'></id> <result column='name' property='name'></result> <association property='classes' javaType='com.wts.entity.Classes'> <id column='cid' property='id'></id> <result column='cname' property='name'></result> </association> </resultMap> <select parameterType='long' resultMap='studentMap'> select s.id,s.name,c.id as cid,c.name as cname from student s,classes c where s.id = #{id} and s.cid = c.id </select></mapper>

注意這里有幾個限制:

1.命名空間,xml文件的namespace必須是對應接口的全類名

2.Statement標簽的id必須與接口方法相同,其中parameterType為參數,resultType為返回類型,復雜類型用resultMap

3.復雜類型resultMap中多對一用association,一堆多用集合collection

MyBatis執行sql返回的結果集會和關系對象映射起來,注意列與字段的對應關系。

然后將mapper引入:

<mappers> <mapper resource='com/wts/repository/StudentRepository.xml'></mapper></mappers>

編寫測試方法:

@Testpublic void test03() { InputStream inputStream = AppTest.class.getClassLoader().getResourceAsStream('config.xml'); SqlSessionFactory sqlSessionFactory = (new SqlSessionFactoryBuilder()).build(inputStream); try (SqlSession sqlSession = sqlSessionFactory.openSession()) { // 級聯查詢 StudentRepository studentRepository = sqlSession.getMapper(StudentRepository.class); System.out.println(studentRepository.findById(1L)); }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Mybatis 數據庫
相關文章:
主站蜘蛛池模板: 福利av在线 | 天天成人综合网 | 久久国品片| 91精品国产一区二区三区 | 国产一区二区三区 | 中文字幕免费 | 天堂av在线影院 | 日韩av高清 | 国产精品久久久久久久免费观看 | 久久久久亚洲精品 | 国产高清一区二区三区 | 国产美女在线免费观看 | 国产精品免费一区二区三区四区 | 99re视频在线 | 久久久久久免费观看 | 久久天天躁狠狠躁夜夜躁2014 | 亚洲精品一区国语对白 | 91视频免费 | 午夜午夜精品一区二区三区文 | 国产成人99久久亚洲综合精品 | 欧美一级久久 | 久久99精品久久久久久国产越南 | 亚洲天堂久久新 | 在线国产精品一区 | 精品国产黄a∨片高清在线 成人区精品一区二区婷婷 日本一区二区视频 | 日韩中文字幕免费在线观看 | 九九九视频在线 | 波多野结衣一区二区三区 | 国产高潮好爽受不了了夜色 | 午夜免费视频 | 精品国产18久久久久久二百 | 国产一二区视频 | 日本久久精品视频 | 激情六月丁香婷婷 | 美女一区二区在线观看 | 亚洲国产精品一区二区三区 | 自拍偷拍第一页 | 人妖videosex高潮另类 | 国产色片在线 | 久久久久久国产精品 | 美女天天操 |