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

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - MyBatis 多表操作的實現

MyBatis 多表操作的實現

2020-09-05 00:15Demo_Null Java教程

這篇文章主要介紹了MyBatis 多表操作的實現,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

1.1 一對一查詢

1.1.1 概述

關系數據庫中第一個表中的單個行只可以與第二個表中的一個行相關,且第二個表中的一個行也只可以與第一個表中的一個行相關。

MyBatis 多表操作的實現

1.1.2 創建實體類

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 創建 DAO 接口

public class StudentStatus {
  private Integer id;
  private String num;
  private String major;

  // set and get
}

1.1.4 結果映射

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

<resultMap id="userResultMap" type="User">
	<result property="id" column="user_id" />
	<result property="username" column="user_name"/>
	<result property="password" column="hashed_password"/>
</resultMap>
<select id="selectUsers" 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 id="resMap" type="student">
    <result property="studentStatus.id" column="st_id"/>
    <result property="studentStatus.major" column="major"/>
    <result property="studentStatus.num" column="num"/>
  </resultMap>
  <select id="findAll" resultMap="resMap">
    select * from student s, student_status st where s.st_id = st.st_id
  </select>
</mapper>

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

<mapper namespace="com.software.mybatis.dao.StudentDao">
  <resultMap id="resMap" 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 id="findAll" 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 id="resMap" type="student" autoMapping="true">
    <association property="studentStatus" resultMap="stMap" />
  </resultMap>
  <resultMap id="stMap" type="StudentStatus" autoMapping="true"/>
  <select id="findAll" 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 多表操作的實現

1.2 一對多查詢

1.2.1 概述

一對多關系是關系數據庫中兩個表之間的一種關系,該關系中第一個表中的單個行可以與第二個表中的一個或多個行相關,但第二個表中的一個行只可以與第一個表中的一個行相關。

MyBatis 多表操作的實現

1.2.2 創建實體類

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 創建 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 id="resMap" 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 id="findAll" 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 多表操作的實現

1.3 多對多查詢

1.3.1 概述

多對多關系是關系數據庫中兩個表之間的一種關系, 該關系中第一個表中的一個行可以與第二個表中的一個或多個行相關。第二個表中的一個行也可以與第一個表中的一個或多個行相關。該關系一般會借助第三方表實現。

MyBatis 多表操作的實現

1.3.2 創建實體類

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 創建 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 id="resMap" 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 id="findAll" 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 id="resMap" 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 id="findAll" 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("### 學生 ###");
    for (Student student : studentList) {
      System.out.println(student);
    }
  }
}

MyBatis 多表操作的實現

到此這篇關于MyBatis 多表操作的實現的文章就介紹到這了,更多相關MyBatis 多表操作內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!

原文鏈接:https://blog.csdn.net/Demo_Null/article/details/108378164

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 天天干夜夜拍 | 好男人在线观看免费高清2019韩剧 | 青青青手机在线视频 | 亚洲欧美在线观看一区二区 | 美女扒开胸罩露出奶 | 天选之王漫画顾长歌免费阅读 | 亚洲欧美日韩另类精品一区二区三区 | 色依依视频视频在线观看 | 97爱sese | 护士伦理片 | 欧美日本道免费一区二区三区 | 欧美福利在线观看 | 国产精品久久免费观看 | 天天操天天射天天爽 | 久久精品成人免费看 | 亚洲成熟人网站 | 国内永久第一免费福利视频 | 国产主播福利在线观看 | 青草视频免费 | 欧美日韩在线观看区一二 | 国产精品久久毛片完整版 | 日韩亚洲人成在线综合 | 日韩日韩日韩手机看片自拍 | 久久精品黄AA片一区二区三区 | 99久久免费国产特黄 | 欧美怡红院视频一区二区三区 | 国产在线看片网站 | 久久中文字幕亚洲精品最新 | 欧美日韩视频在线一区二区 | 黄漫在线播放 | 国产综合成色在线视频 | 521色香蕉网在线观看免费 | 痴mu动漫成年动漫在线观看 | 楚乔传第二部全60集免费观看 | 国产思妍小仙女一二区 | 给我免费观看的视频在线播放 | 嫩模被黑人粗大挺进 | 国产色综合久久五月色婷婷中文 | avove全部视频在线观看 | 91在线永久 | 久久这里只有精品视频e |