国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

MyBatis使用級聯(lián)操作解決lombok構(gòu)造方法識別失敗問題

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

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

MyBatis級聯(lián)操作,列舉最簡單的student-classes(學(xué)生與班級)的關(guān)系表:

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

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

接下來,創(chuàng)建關(guān)系表對應(yīng)的實體類:

@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);}

然后創(chuàng)建對應(yīng)的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必須是對應(yīng)接口的全類名

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

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

MyBatis執(zhí)行sql返回的結(jié)果集會和關(guān)系對象映射起來,注意列與字段的對應(yīng)關(guān)系。

然后將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()) { // 級聯(lián)查詢 StudentRepository studentRepository = sqlSession.getMapper(StudentRepository.class); System.out.println(studentRepository.findById(1L)); }}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Mybatis 數(shù)據(jù)庫
相關(guān)文章:
主站蜘蛛池模板: 91pao强力打造免费高清 | 国产精品亚洲片在线不卡 | 欧美一级特黄乱妇高清视频 | 日本欧美一区二区三区在线 | 亚洲免费一级片 | 成人中文字幕在线 | 男人看片网址 | 亚洲成a人伦理 | 精品欧美一区二区三区免费观看 | 特黄特黄一级高清免费大片 | av在线手机播放 | 在线播放一级片 | 毛片网站在线看 | 四虎午夜剧场 | 亚洲日本一区二区三区 | 欧美很黄视频在线观看 | 欧美日韩亚洲高清不卡一区二区三区 | 国产呦精品系列在线 | 国产成人mv在线观看入口视频 | 一级啊片 | 久久精品一品道久久精品9 久久精品一区 | a级毛片免费 | 69xx欧美| 欧美日韩在线视频播放 | 91亚洲精品在看在线观看高清 | 热99re久久精品这里都是免费 | 做爰www免费看视频 1024色淫免费视频 | 午夜日韩视频 | 男女性高爱潮免费的国产 | 国产看午夜精品理论片 | 日韩一区二区不卡 | 一级毛片免费在线播放 | 久久精品a亚洲国产v高清不卡 | 一级做a爰性色毛片免费 | 男人的天堂久久爱 | 亚洲天堂日韩在线 | 一本色道久久88亚洲综合 | 国产精品久久国产三级国不卡顿 | 在线视频欧美日韩 | 国产亚洲三级 | 一个人看的日本免费视频 |