MyBatis使用級(jí)聯(lián)操作解決lombok構(gòu)造方法識(shí)別失敗問題
先解決一下idea無(wú)法識(shí)別lombok構(gòu)造方法的問題,解決方案是在idea的插件中下載并安裝lombok插件。
MyBatis級(jí)聯(lián)操作,列舉最簡(jiǎ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í)名' );
接下來(lái),創(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ì)象映射起來(lái),注意列與字段的對(duì)應(yīng)關(guān)系。
然后將mapper引入:
<mappers> <mapper resource="com/wts/repository/StudentRepository.xml"></mapper> </mappers>
編寫測(cè)試方法:
@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));
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Data Jpa 復(fù)合主鍵的實(shí)現(xiàn)
這篇文章主要介紹了Spring Data Jpa 復(fù)合主鍵的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
IDEA快速搭建spring?boot項(xiàng)目教程(Spring?initializr)
這篇文章主要介紹了IDEA快速搭建spring?boot項(xiàng)目教程(Spring?initializr),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
SpringBoot常用讀取配置文件的3種方法小結(jié)
本文主要介紹了SpringBoot常用讀取配置文件的3種方法小結(jié),主要包括@Value讀取配置文件,@ConfigurationProperties 讀取配置文件和讀取配置文件中的List,具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10
Java AES加密解密的簡(jiǎn)單實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇Java AES加密解密的簡(jiǎn)單實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2017-06-06
Java遞歸查找層級(jí)文件夾下特定內(nèi)容的文件的方法
這篇文章主要介紹了Java遞歸查找層級(jí)文件夾下特定內(nèi)容的文件,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06

