您好,登錄后才能下訂單哦!
這篇文章主要講解了“什么是Mybatis結(jié)果集映射一對多”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“什么是Mybatis結(jié)果集映射一對多”吧!
搭建數(shù)據(jù)庫環(huán)境
student 表
DROP TABLE IF EXISTS `student_2`; CREATE TABLE `student_2` ( `id` int(10) NOT NULL, `name` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `tid` int(10) NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE, INDEX `fktid`(`tid`) USING BTREE, CONSTRAINT `fktid` FOREIGN KEY (`tid`) REFERENCES `teacher` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; INSERT INTO `student_2` VALUES (1, '小明', 1); INSERT INTO `student_2` VALUES (2, '邱ss', 2); INSERT INTO `student_2` VALUES (3, '邱大哥', 3); INSERT INTO `student_2` VALUES (4, '楊大哥', 1); INSERT INTO `student_2` VALUES (5, '楊ss', 2);
teacher
DROP TABLE IF EXISTS `teacher`; CREATE TABLE `teacher` ( `id` int(10) NOT NULL AUTO_INCREMENT, `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = latin1 COLLATE = latin1_swedish_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Records of teacher -- ---------------------------- INSERT INTO `teacher` VALUES (1, '小王老師'); INSERT INTO `teacher` VALUES (2, '小李老師'); INSERT INTO `teacher` VALUES (3, '小黑老師');
idea 搭建maven 項(xiàng)目 (mybatis-demo)
項(xiàng)目結(jié)構(gòu)
導(dǎo)入依賴
<dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.48</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.20</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.6</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </dependencies>
mysql 配置文件
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/ssm-study?useSSL=false jdbc.username=root jdbc.password=123456
創(chuàng)建pojo 類
學(xué)生
/** * @Author: crush * @Date: 2021-06-17 18:23 * version 1.0 */ public class Student { /** * 學(xué)生id */ private Integer id; /** * xueshneg xingming */ private String name; /** * 老師id */ private Integer tid; }
老師
/** * @Author: crush * @Date: 2021-06-17 18:23 * version 1.0 */ public class Teacher { /** * 老師id */ private Integer id; /** * 老師的姓名 */ private String name; /** * 每個老師是不是有很多學(xué)生 */ private List<Student> students; }
寫一個mybatis 的工具類
/** * @author crush */ public class MybatisUtil { private static SqlSessionFactory sqlSessionFactory; static { try { String resource="mybatis-config.xml"; InputStream inputStream= Resources.getResourceAsStream(resource); sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { e.printStackTrace(); } } public static SqlSession getSession(){ return sqlSessionFactory.openSession(true); } }
寫一個TeacherMapper
import com.crush.pojo.Teacher; import org.apache.ibatis.annotations.Param; import java.util.List; public interface TeacherMapper { // 獲取老師 List<Teacher> getTeacher(); //獲取指定老師下的所有學(xué)生及老師的信息 Teacher getTeacher2(@Param("tid") Integer id); //獲取指定老師下的所有學(xué)生及老師的信息 Teacher getTeacher3(@Param("tid") Integer id); }
寫一個TeacherMapper.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.crush.dao.TeacherMapper"> <select id="getTeacher" resultType="Teacher"> select * from teacher </select> <select id="getTeacher2" resultMap="TeacherStudent"> select s.id sid,s.name sname,t.name tname, t.id tid from student_2 s,teacher t where s.tid=t.id and t.id=#{tid} </select> <resultMap id="TeacherStudent" type="Teacher"> <result property="id" column="tid"/> <result property="name" column="tname"/> <!-- 復(fù)雜的屬性,我們需要單獨(dú)處理 對象 association 集合collection javaType ="" 是指屬性的類型 集合中的泛型的信息 我們使用ofType 獲取 --> <collection property="students" ofType="Student"> <result property="id" column="sid"/> <result property="name" column="sname"/> <result property="tid" column="tid"/> </collection> </resultMap> <!--========================= 結(jié)果集映射=============================--> <select id="getTeacher3" resultMap="TeacherStudent3"> select * from teacher where id=#{tid} </select> <resultMap id="TeacherStudent3" type="Teacher"> <collection property="students" javaType="ArrayList" ofType="Student" select="getStudentByTeacherId" column="id" /> </resultMap> <select id="getStudentByTeacherId" resultType="Student"> select * from student_2 where tid=#{tid} </select> </mapper>
mybatis-config.xml 文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!--屬性--> <properties resource="dbconfig.properties"/> <!--設(shè)置--> <settings> <!-- STDOUT_LOGGING 打印到控制臺 --> <setting name="logImpl" value="STDOUT_LOGGING"/> <!-- 開啟駝峰 --> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> <!--別名--> <typeAliases> <!--這是 自己 取別名--> <typeAlias alias="Student" type="com.crush.pojo.Student"/> <typeAlias alias="Teacher" type="com.crush.pojo.Teacher"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </dataSource> </environment> </environments> <mappers> <mapper resource="mapper/StudentMapper.xml"/> <mapper resource="mapper/TeacherMapper.xml"/> </mappers> </configuration>
測試:
/** * @Author: crush * @Date: 2021-06-17 18:22 * version 1.0 */ public class MyTest { @Test public void getTeacher(){ SqlSession session = MybatisUtil.getSession(); TeacherMapper mapper = session.getMapper(TeacherMapper.class); List<Teacher> teacher = mapper.getTeacher(); System.out.println(teacher); session.close(); } @Test public void getTeacher2(){ SqlSession session = MybatisUtil.getSession(); TeacherMapper mapper = session.getMapper(TeacherMapper.class); Teacher teacher2 = mapper.getTeacher2(1); System.out.println(teacher2); session.close(); } @Test public void getTeacher3(){ SqlSession session = MybatisUtil.getSession(); TeacherMapper mapper = session.getMapper(TeacherMapper.class); Teacher teacher2 = mapper.getTeacher3(1); System.out.println(teacher2); session.close(); } }
感謝各位的閱讀,以上就是“什么是Mybatis結(jié)果集映射一對多”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對什么是Mybatis結(jié)果集映射一對多這一問題有了更深刻的體會,具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。