MyBatis使用級(jí)聯(lián)操作解決lombok構(gòu)造方法識(shí)別失敗問題
先解決一下idea無法識(shí)別lombok構(gòu)造方法的問題,解決方案是在idea的插件中下載并安裝lombok插件。
MyBatis級(jí)聯(lián)操作,列舉最簡單的student-classes(學(xué)生與班級(jí))的關(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 '班級(jí)id' );
create table if not exists classes ( id int primary key auto_increment, name varchar(20) not null comment '班級(jí)名' );
接下來,創(chuàng)建關(guān)系表對(duì)應(yīng)的實(shí)體類:
@Data public class Student { private long id; private String name; private Classes classes; }
@Data public class Classes { private long id; private String name; private List<Student> students; }
在repository包下新建StudentRepository接口:
public interface StudentRepository { public Student findById(long id); }
然后創(chuàng)建對(duì)應(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 id="studentMap" 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 id="findById" 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>
注意這里有幾個(gè)限制:
1.命名空間,xml文件的namespace必須是對(duì)應(yīng)接口的全類名
2.Statement標(biāo)簽的id必須與接口方法相同,其中parameterType為參數(shù),resultType為返回類型,復(fù)雜類型用resultMap
3.復(fù)雜類型resultMap中多對(duì)一用association,一堆多用集合collection
MyBatis執(zhí)行sql返回的結(jié)果集會(huì)和關(guān)系對(duì)象映射起來,注意列與字段的對(duì)應(yīng)關(guān)系。
然后將mapper引入:
<mappers> <mapper resource="com/wts/repository/StudentRepository.xml"></mapper> </mappers>
編寫測試方法:
@Test public void test03() { InputStream inputStream = AppTest.class.getClassLoader().getResourceAsStream("config.xml"); SqlSessionFactory sqlSessionFactory = (new SqlSessionFactoryBuilder()).build(inputStream); try (SqlSession sqlSession = sqlSessionFactory.openSession()) { // 級(jí)聯(lián)查詢 StudentRepository studentRepository = sqlSession.getMapper(StudentRepository.class); System.out.println(studentRepository.findById(1L)); } }
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
jenkins如何部署應(yīng)用到多個(gè)環(huán)境
本文介紹了如何基于流水線的方式將應(yīng)用程序部署到多個(gè)環(huán)境,包括測試環(huán)境和生產(chǎn)環(huán)境,通過創(chuàng)建項(xiàng)目、設(shè)置參數(shù)、配置流水線、設(shè)置環(huán)境變量、配置Maven工具、構(gòu)建階段、部署測試環(huán)境和生產(chǎn)環(huán)境、以及清理階段,實(shí)現(xiàn)了自動(dòng)化部署流程2024-11-11Java設(shè)置請(qǐng)求響應(yīng)時(shí)間的多種實(shí)現(xiàn)方式
在前后端分離的開發(fā)模式中,前端請(qǐng)求后端獲取數(shù)據(jù)時(shí),合理設(shè)置響應(yīng)時(shí)間(超時(shí)時(shí)間)是提升系統(tǒng)性能和用戶體驗(yàn)的關(guān)鍵,本文將深入探討如何在Java中設(shè)置請(qǐng)求的響應(yīng)時(shí)間,需要的朋友可以參考下2025-01-01JDK動(dòng)態(tài)代理原理:只能代理接口,不能代理類問題
這篇文章主要介紹了JDK動(dòng)態(tài)代理原理:只能代理接口,不能代理類問題。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11如何修改HttpServletRequest中header中的信息
這篇文章主要介紹了如何修改HttpServletRequest中header中的信息,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02spring整合redis緩存并以注解(@Cacheable、@CachePut、@CacheEvict)形式使用
本篇文章主要介紹了spring整合redis緩存并以注解(@Cacheable、@CachePut、@CacheEvict)形式使用,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-04-04手把手帶你實(shí)現(xiàn)第一個(gè)Mybatis程序
這篇文章主要介紹了mybatis實(shí)現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2021-07-07java中synchronized Lock(本地同步)鎖的8種情況
本文主要介紹了java中synchronized Lock(本地同步)鎖的8種情況,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09SpringBoot項(xiàng)目中如何動(dòng)態(tài)切換數(shù)據(jù)源、數(shù)據(jù)庫
本文主要介紹了SpringBoot項(xiàng)目中如何動(dòng)態(tài)切換數(shù)據(jù)源、數(shù)據(jù)庫,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-02-02