一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術(shù)|正則表達(dá)式|

服務(wù)器之家 - 編程語言 - JAVA教程 - MyBatis使用級聯(lián)操作解決lombok構(gòu)造方法識別失敗問題

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

2020-07-08 14:33viewts JAVA教程

這篇文章主要介紹了MyBatis使用級聯(lián)操作解決lombok構(gòu)造方法識別失敗問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

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

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

?
1
2
3
4
5
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'
);
?
1
2
3
4
create table if not exists classes (
  id int primary key auto_increment,
  name varchar(20) not null comment '班級名'
);

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

?
1
2
3
4
5
6
@Data
public class Student {
  private long id;
  private String name;
  private Classes classes;
}
?
1
2
3
4
5
6
@Data
public class Classes {
  private long id;
  private String name;
  private List<Student> students;
}

在repository包下新建StudentRepository接口:

?
1
2
3
public interface StudentRepository {
  public Student findById(long id);
}

然后創(chuàng)建對應(yīng)的mapper文件StudentRepository.xml:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?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>

注意這里有幾個限制:

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引入:

?
1
2
3
<mappers>
  <mapper resource="com/wts/repository/StudentRepository.xml"></mapper>
</mappers>

編寫測試方法:

?
1
2
3
4
5
6
7
8
9
10
@Test
public 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ù)器之家。

原文鏈接:https://www.cnblogs.com/viewts/p/13213352.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 色噜噜亚洲男人的天堂www | 免费观看伦理片 | 深夜啪啪网站 | 国产成人理在线观看视频 | 国产日产在线观看 | 扒开老师挠尿口到崩溃刑罚 | 日本高清免费不卡在线播放 | 男女视频在线观看 | 免费lulu网站| 成人永久免费 | 嫩草在线观看视频 | 秋霞黄色 | 四虎免费入口 | 午夜理论电影在线观看亚洲 | 果冻传媒天美传媒在线小视频播放 | 男人资源站 | 日日免费视频 | 精品国产视频 | 欧美亚洲天堂网 | 呜呜别塞了啊抽插 | 天堂网在线.www天堂在线视频 | 色婷在线| 教室里的激情电影 | 大学生情侣在线 | 国产suv精品一区二区四区三区 | 亚洲人成影院午夜网站 | beeg最新| 国产资源免费观看 | 国产成人夜色91 | 亚洲大片免费观看 | 美女被狂揉下部羞羞动漫 | 国产成人刺激视频在线观看 | 18性夜影院午夜寂寞影院免费 | 国色天香社区在线视频播放 | 四虎播放器 | 精品国产日韩亚洲一区在线 | 小兰被扒开内裤露出p | free性videoxxⅹ印度 | 四虎影视紧急入口地址大全 | 男生的j桶女人屁免费视频 男生操男生 | 国产成人啪精品午夜在线观看 |