日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

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

MyBatis 多表操作的實現(xiàn)

瀏覽:22日期:2023-10-22 18:16:34

1.1 一對一查詢

1.1.1 概述

  關(guān)系數(shù)據(jù)庫中第一個表中的單個行只可以與第二個表中的一個行相關(guān),且第二個表中的一個行也只可以與第一個表中的一個行相關(guān)。

MyBatis 多表操作的實現(xiàn)

1.1.2 創(chuàng)建實體類

public class Student { private Integer id; private String name; private Boolean age; private String sex; private StudentStatus studentStatus; // set and get}

public class StudentStatus { private Integer id; private String num; private String major; // set and get}

1.1.3 創(chuàng)建 DAO 接口

public class StudentStatus { private Integer id; private String num; private String major; // set and get}

1.1.4 結(jié)果映射

  resultMap 元素是 MyBatis 中最重要最強大的元素。它可以從 90% 的 JDBC ResultSets 數(shù)據(jù)提取代碼中解放出來,并在一些情形下允許進(jìn)行一些 JDBC 不支持的操作。實際上,在為一些比如連接的復(fù)雜語句編寫映射代碼的時候,一份 resultMap 能夠代替實現(xiàn)同等功能的長達(dá)數(shù)千行的代碼。resultMap 的設(shè)計思想是,對于簡單的語句根本不需要配置顯式的結(jié)果映射,而對于復(fù)雜一點的語句只需要描述它們的關(guān)系就行了。之前已經(jīng)使用過簡單映射語句了,但并沒有顯式指定 resultMap。只是簡單的使用 resultType 將所有的列映射到對象的屬性上,需要注意的是列名與屬性名一致才能映射,解決列名不匹配還是需要使用 resultMap。

<resultMap type='User'><result property='id' column='user_id' /><result property='username' column='user_name'/><result property='password' column='hashed_password'/></resultMap><select resultMap='userResultMap'>select user_id, user_name, hashed_password from some_table where id = #{id}</select>

1.1.5 配置 mapper

<?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.software.mybatis.dao.StudentDao'> <resultMap type='student'> <result property='studentStatus.id' column='st_id'/> <result property='studentStatus.major' column='major'/> <result property='studentStatus.num' column='num'/> </resultMap> <select resultMap='resMap'> select * from student s, student_status st where s.st_id = st.st_id </select></mapper>

上面這種配置會將自動將列名一致的映射到 type 指定的實體類中,該實體類中屬性類型為對象的則需要單獨拿出來映射。還可以使用 association 進(jìn)行復(fù)雜的映射,我們發(fā)現(xiàn)未配置的屬性無法進(jìn)行映射。產(chǎn)生這個問題的原因是 resultMap 的自動映射未打開,使用 autoMapping 設(shè)置這個屬性為 true/false,MyBatis 將會為本結(jié)果映射開啟/關(guān)閉自動映射。

<mapper namespace='com.software.mybatis.dao.StudentDao'> <resultMap type='com.software.mybatis.entity.Student'> <result property='id' column='id'/> <result property='name' column='name'/> <result property='sex' column='sex'/> <result property='age' column='age'/> <association property='studentStatus' javaType='com.software.mybatis.entity.StudentStatus'> <result property='id' column='st_id'/> <result property='major' column='major'/> <result property='num' column='num'/> </association> </resultMap> <select resultMap='resMap'> select * from student s, student_status st where s.st_id = st.st_id </select></mapper>

<?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.software.mybatis.dao.StudentDao'> <resultMap type='student' autoMapping='true'> <association property='studentStatus' resultMap='stMap' /> </resultMap> <resultMap type='StudentStatus' autoMapping='true'/> <select resultMap='resMap'> select * from student s, student_status st where s.st_id = st.st_id </select></mapper>

1.1.6 核心配置

<!DOCTYPE configuration PUBLIC '-//mybatis.org//DTD Config 3.0//EN' 'http://mybatis.org/dtd/mybatis-3-config.dtd'><configuration> <settings> <setting name='logImpl' value='STDOUT_LOGGING'/> </settings> <typeAliases> <package name='com.software.mybatis.entity'/> </typeAliases> <environments default='development'> <environment id='development'> <transactionManager type='JDBC'/> <dataSource type='POOLED'><property name='driver' value='com.mysql.jdbc.Driver'/><property name='url' value='jdbc:mysql://localhost:3306/db'/><property name='username' value='root'/><property name='password' value='root'/> </dataSource> </environment> </environments> <mappers> <mapper resource='student-mapper.xml'/> </mappers></configuration>

1.1.7 測試

/** * Created with IntelliJ IDEA. * * @author Demo_Null * @date 2020/9/3 * @description 測試類 */public class MybatisDemo { @Test public void TestA() throws IOException { // 加載核心配置文件 InputStream resourceAsStream = Resources.getResourceAsStream('mybatis.xml'); // 獲得 sqlSession 工廠對象 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); // 獲得 sqlSession 對象 SqlSession sqlSession = sqlSessionFactory.openSession(); List<Student> list = sqlSession.selectList('com.software.mybatis.dao.StudentDao.findAll'); System.out.println(list); }}

MyBatis 多表操作的實現(xiàn)

1.2 一對多查詢

1.2.1 概述

  一對多關(guān)系是關(guān)系數(shù)據(jù)庫中兩個表之間的一種關(guān)系,該關(guān)系中第一個表中的單個行可以與第二個表中的一個或多個行相關(guān),但第二個表中的一個行只可以與第一個表中的一個行相關(guān)。

MyBatis 多表操作的實現(xiàn)

1.2.2 創(chuàng)建實體類

public class Student { private Integer sId; private String sName; private Long sAge; private String sSex; private Integer cId;// set and get}

public class Class { private Integer cId; private String cName; private String cAddr; private List<Student> students; // set and get}

1.1.3 創(chuàng)建 DAO 接口

/** * Created with IntelliJ IDEA. * * @author Demo_Null * @date 2020/9/3 * @description DAO 接口 */public interface ClassDao { public List<Class> findAll();}

1.1.4 配置 mapper

<?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.software.mybatis.dao.ClassDao'> <resultMap type='Class'> <result property='cId' column='c_id'/> <result property='cName' column='c_name'/> <result property='cAddr' column='c_addr'/> <collection property='students' ofType='Student'> <result property='sId' column='s_id' /> <result property='sName' column='s_name'/> <result property='sAge' column='s_age'/> <result property='sSex' column='s_sex'/> <result property='cId' column='c_id'/> </collection> </resultMap> <select resultMap='resMap'> select * from student s, class c where s.c_id = c.c_id </select></mapper>

1.1.5 測試

/** * Created with IntelliJ IDEA. * * @author Demo_Null * @date 2020/9/3 * @description 測試類 */public class MybatisDemo { @Test public void TestA() throws IOException { // 加載核心配置文件 InputStream resourceAsStream = Resources.getResourceAsStream('mybatis.xml'); // 獲得 sqlSession 工廠對象 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); // 獲得 sqlSession 對象 SqlSession sqlSession = sqlSessionFactory.openSession(); List<Class> list = sqlSession.selectList('com.software.mybatis.dao.ClassDao.findAll'); for (Class aClass : list) { System.out.println(aClass); } }}

MyBatis 多表操作的實現(xiàn)

1.3 多對多查詢

1.3.1 概述

  多對多關(guān)系是關(guān)系數(shù)據(jù)庫中兩個表之間的一種關(guān)系, 該關(guān)系中第一個表中的一個行可以與第二個表中的一個或多個行相關(guān)。第二個表中的一個行也可以與第一個表中的一個或多個行相關(guān)。該關(guān)系一般會借助第三方表實現(xiàn)。

MyBatis 多表操作的實現(xiàn)

1.3.2 創(chuàng)建實體類

public class Course { private Integer cId; private String cName; private List<Student> students;// set and get}

public class Student { private Integer sId; private String sName; private Long sAge; private String sSex; private List<Course> courses;// set and get}

1.3.3 創(chuàng)建 DAO 接口

/** * Created with IntelliJ IDEA. * * @author Demo_Null * @date 2020/9/3 * @description course DAO 接口 */public interface CourseDao { public List<Course> findAll();}

/** * Created with IntelliJ IDEA. * * @author Demo_Null * @date 2020/9/3 * @description student DAO 接口 */public interface StudentDao { public List<Student> findAll();}

1.3.4 配置 mapper

☞ student-mapper.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.software.mybatis.dao.StudentDao'> <resultMap type='Student'> <result property='sId' column='s_id' /> <result property='sName' column='s_name'/> <result property='sAge' column='s_age'/> <result property='sSex' column='s_sex'/> <collection property='courses' ofType='Course'> <result property='cId' column='c_id'/> <result property='cName' column='c_name'/> </collection> </resultMap> <select resultMap='resMap'> select * from course c, student s, s_c sc where c.c_id = sc.c_id and s.s_id = sc.s_id </select></mapper>

☞ course-mapper.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.software.mybatis.dao.CourseDao'> <resultMap type='Course'> <result property='cId' column='c_id'/> <result property='cName' column='c_name'/> <collection property='students' ofType='Student'> <result property='sId' column='s_id' /> <result property='sName' column='s_name'/> <result property='sAge' column='s_age'/> <result property='sSex' column='s_sex'/> </collection> </resultMap> <select resultMap='resMap'> select * from course c, student s, s_c sc where c.c_id = sc.c_id and s.s_id = sc.s_id </select></mapper>

1.3.5 測試

/** * Created with IntelliJ IDEA. * * @author Demo_Null * @date 2020/9/3 * @description 測試類 */public class MybatisDemo { @Test public void TestA() throws IOException { // 加載核心配置文件 InputStream resourceAsStream = Resources.getResourceAsStream('mybatis.xml'); // 獲得 sqlSession 工廠對象 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); // 獲得 sqlSession 對象 SqlSession sqlSession = sqlSessionFactory.openSession(); List<Course> courseList = sqlSession.selectList('com.software.mybatis.dao.CourseDao.findAll'); List<Student> studentList = sqlSession.selectList('com.software.mybatis.dao.StudentDao.findAll'); System.out.println('### 課程 ###'); for (Course course : courseList) { System.out.println(course); } System.out.println('### 學(xué)生 ###'); for (Student student : studentList) { System.out.println(student); } }}

MyBatis 多表操作的實現(xiàn)

到此這篇關(guān)于MyBatis 多表操作的實現(xiàn)的文章就介紹到這了,更多相關(guān)MyBatis 多表操作內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Mybatis 數(shù)據(jù)庫
相關(guān)文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
精品久久91| 国产精品国码视频| 欧洲一级精品| 尤物网精品视频| 在线精品一区二区| 国产欧美精品久久| 精品国产欧美日韩| 91精品啪在线观看国产18 | 91精品国产调教在线观看 | 日韩不卡一区二区三区| 国产伦精品一区二区三区在线播放| 国产精品二区影院| 在线观看精品| 少妇精品久久久| 久久精品一本| 激情欧美一区二区三区| 婷婷五月色综合香五月| 美女久久精品| 午夜欧美精品久久久久久久| 日本va欧美va精品| 88xx成人免费观看视频库| 免费成人在线视频观看| 久久爱www.| 久久精品1区| 日韩精品免费视频人成| 三级在线看中文字幕完整版| 亚洲一区二区av| 毛片在线网站| 亚洲精品影院在线观看| 中文字幕人成乱码在线观看| 亚洲人成精品久久久| 久久精品一区| 一二三区精品| 欧美成人a交片免费看| 中文字幕日韩高清在线 | 国产白浆在线免费观看| 水蜜桃久久夜色精品一区的特点 | 亚洲精品国产偷自在线观看| 欧美亚洲网站| 狠狠干综合网| 久久精品五月| 日韩区一区二| 99视频精品全国免费| 国产精品一区二区av日韩在线| 久久久人人人| 久久精品国产亚洲一区二区三区| 巨乳诱惑日韩免费av| 国产一区二区三区精品在线观看| 亚洲免费毛片| 99久久久久国产精品| 国产精品免费精品自在线观看| 黄色av一区| 黄在线观看免费网站ktv| 欧美日韩一视频区二区| 99在线观看免费视频精品观看| 精品一区二区三区中文字幕| 综合色就爱涩涩涩综合婷婷| 日韩三区在线| 国产在线一区不卡| 久久国内精品自在自线400部| av成人国产| 国产综合婷婷| 色偷偷偷在线视频播放| 久久精品国产999大香线蕉| 亚洲tv在线| 夜夜嗨av一区二区三区网站四季av| 久久麻豆视频| 欧美精品观看| 亚洲精品综合| 久久国产福利| 亚洲国产一区二区在线观看| 色综合五月天| 捆绑调教美女网站视频一区 | 尹人成人综合网| 久久国产影院| 日韩欧美网址| 电影天堂国产精品| 精品网站999| 精品欧美日韩精品| 久久不卡国产精品一区二区| 国产美女久久| 麻豆91在线播放| 久久免费精品| 国产成人精品亚洲线观看| 国产精品极品在线观看| 国产精品免费不| 久久伊人亚洲| 91av亚洲| 国产一区亚洲| 亚洲欧美日本日韩| 中文无码久久精品| 日韩精品免费视频一区二区三区| 亚洲欧美网站| 日韩精品成人| 国产精品三级| 精品午夜视频| 日韩成人三级| 亚洲精品网址| 亚洲另类av| 国产精品第十页| 亚洲免费福利| 亚洲精华国产欧美| 日本精品国产| 精品一区二区三区的国产在线观看| 久久字幕精品一区| 理论片午夜视频在线观看| 色婷婷狠狠五月综合天色拍| 色婷婷精品视频| 香蕉精品999视频一区二区| 中文精品电影| 国产亚洲高清在线观看| 另类小说一区二区三区| 日本欧美不卡| 一区二区电影| 免费在线欧美黄色| 伊伊综合在线| 一区二区视频欧美| 日韩在线成人| 国产成人在线中文字幕| 1024精品久久久久久久久| 亚洲精品极品| 精品国产欧美日韩一区二区三区| 中文字幕在线视频网站| 欧美成人亚洲| 91大神在线观看线路一区| 国产69精品久久| 欧美日韩四区| 国产精品v日韩精品v欧美精品网站| 日韩欧美在线中字| 亚洲午夜国产成人| 91欧美国产| 亚洲1区在线观看| 成人污污视频| 蜜臀av性久久久久蜜臀aⅴ四虎 | 日韩精品电影一区亚洲| 久久精品国产99国产| 99久久夜色精品国产亚洲狼 | 每日更新成人在线视频| 国产福利一区二区三区在线播放| 91精品国产调教在线观看| 日韩一区二区三区高清在线观看| 精品久久91| 中文视频一区| 久久久久久自在自线| 美女被久久久| 国产黄大片在线观看| 蜜桃视频一区二区| 日韩免费福利视频| 国产欧美日韩免费观看| 亚洲性色av| 欧美日韩中出| 黄色av一区| 伊人久久视频| 国产欧美二区| 久久国产精品久久久久久电车| 久久只有精品| 日韩精品视频在线看| 99精品视频精品精品视频| 国产亚洲精品精品国产亚洲综合 | 精品丝袜久久| 日韩精品亚洲专区| 91精品99| 亚洲啊v在线| 久久精品三级| 国产亚洲观看| 午夜久久av | 偷拍欧美精品| 动漫av一区| 国产日韩一区二区三免费高清| 91精品高清| av综合电影网站| 欧美xxxx性| 91精品日本| 中文字幕日韩欧美精品高清在线| 日韩一区二区三区免费播放| 国产精品任我爽爆在线播放| 日本不卡高清视频| 在线观看视频免费一区二区三区| 久久国产中文字幕| 欧美男人天堂| 国产一区二区三区探花| 国产精品一区二区免费福利视频| 亚洲我射av| 天堂av在线一区| 免费视频亚洲| 美女久久久久| 亚洲午夜视频| 欧美日韩精品免费观看视欧美高清免费大片| 麻豆精品新av中文字幕| 国产三级精品三级在线观看国产| 亚洲一区导航| 中文在线日韩| 日本伊人午夜精品| 日韩精品一区二区三区中文在线 | 在线精品一区| 亚洲三级av| 少妇精品久久久一区二区| 在线看片日韩| 日韩高清一区在线| 91午夜精品|