标签:String gender id st Mybatis 用法 public name
订阅专栏这篇文章我们将来学习一些 association用法
表结构
- DROP TABLE IF EXISTS `student`; CREATE TABLE `student` (
- `id` int(11) NOT NULL auto_increment,
- `name` varchar(10) NOT NULL,
- `gender` char(1) NOT NULL,
- `major` varchar(20) NOT NULL,
- `grade` char(4) NOT NULL,
- `supervisor_id` int(11) NOT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of student -- ---------------------------- INSERT INTO `student` VALUES ('1', 'zhangsan', '男', '计算机科学与技术', '2011', '1'); -- ---------------------------- -- Table structure for teacher -- ---------------------------- DROP TABLE IF EXISTS `teacher`; CREATE TABLE `teacher` (
- `id` int(11) NOT NULL auto_increment,
- `name` varchar(10) NOT NULL,
- `gender` char(1) NOT NULL,
- `research_area` varchar(20) NOT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of teacher -- ---------------------------- INSERT INTO `teacher` VALUES ('1', 'laowu', '男', '软件工程');
对应的java bean
- public class Student {
- private int id;
- private String name; // 姓名
- private String gender; // 性别
- private String major; // 专业
- private String grade; // 年级
- private Teacher supervisor; //指导教师
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getGender() {
- return gender;
- }
- public void setGender(String gender) {
- this.gender = gender;
- }
- public String getMajor() {
- return major;
- }
- public void setMajor(String major) {
- this.major = major;
- }
- public String getGrade() {
- return grade;
- }
- public void setGrade(String grade) {
- this.grade = grade;
- }
- public Teacher getSupervisor() {
- return supervisor;
- }
- public void setSupervisor(Teacher supervisor) {
- this.supervisor = supervisor;
- }
- }
- public class Teacher {
- private int id;
- private String name; // 教师姓名
- private String gender; // 教师性别
- private String researchArea; // 研究领域
- private List<Student> supStudents; // 指导学生
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getGender() {
- return gender;
- }
- public void setGender(String gender) {
- this.gender = gender;
- }
- public String getResearchArea() {
- return researchArea;
- }
- public void setResearchArea(String researchArea) {
- this.researchArea = researchArea;
- }
- public List<Student> getSupStudents() {
- return supStudents;
- }
- public void setSupStudents(List<Student> supStudents) {
- this.supStudents = supStudents;
- }
- }
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="net.zaodk.mybatis.mapper.StudentOperationMapper">
- <!-- 定义java Bean的属性与数据库的列之间的映射 -->
- <resultMap type="Student" id="studentResultMap">
- <id column="id" property="id" />
- <result column="name" property="name" />
- <result column="gender" property="gender" />
- <result column="major" property="major" />
- <result column="grade" property="grade"/>
- <!-- association处理has-a关系 -->
- <!--注意:这种方式javaType必须指定,表示supervisor的类型是Teacher,否则会报错 -->
- <association property="supervisor" javaType="Teacher">
- <!-- 教师自身的属性与数据库字段的映射 -->
- <id property="id" column="t_id"/>
- <result property="name" column="t_name"/>
- <result property="gender" column="t_gender"/>
- <result property="researchArea" column="research_area"/>
- </association>
- </resultMap>
- <!-- SQL语句中以"#{}"的形式引用参数 -->
- <select id="getById" parameterType="int" resultMap="studentResultMap">
- SELECT st.id,st.name,st.gender,st.major,st.grade,t.id t_id,t.name t_name,
- t.gender t_gender,t.research_area
- FROM student st, teacher t
- WHERE st.supervisor_id = t.id
- AND st.id=#{id}
- </select> </mapper>
测试
- public void testAssociation(){
- SqlSession session = sqlSessionFactory.openSession();
- StudentOperationMapper studentOperationMapper = session.getMapper(StudentOperationMapper.class);
- Student student = studentOperationMapper.getById(1);
- System.out.println(student.getName()+"的指导老师是:"+student.getSupervisor().getName());
- }
myBatis association的两种形式
嵌套的resultMap
这种方法的本质就是把教师实体映射从association元素中提取出来,用一个resultMap元素表示。然后association元素再引用这个resultMap元素。
- <?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="net.zaodk.mybatis.mapper.StudentOperationMapper">
- <!-- 定义java Bean的属性与数据库的列之间的映射 -->
- <resultMap type="Teacher" id="teacherResultMap">
- <id property="id" column="t_id"/>
- <result property="name" column="t_name"/>
- <result property="gender" column="t_gender"/>
- <result property="researchArea" column="research_area"/>
- </resultMap>
- <resultMap type="Student" id="studentResultMap">
- <id column="id" property="id" />
- <result column="name" property="name" />
- <result column="gender" property="gender" />
- <result column="major" property="major" />
- <result column="grade" property="grade"/>
- <!-- 引用teacherResultMap -->
- <association property="supervisor" resultMap="teacherResultMap"/>
- </resultMap>
- <!-- SQL语句中以"#{}"的形式引用参数 -->
- <select id="getById" parameterType="int" resultMap="studentResultMap">
- SELECT st.id,st.name,st.gender,st.major,st.grade,t.id t_id,t.name t_name,
- t.gender t_gender,t.research_area
- FROM student st, teacher t
- WHERE st.supervisor_id = t.id
- AND st.id=#{id}
- </select> </mapper>
嵌套的select语句
这种方式是使用一条单独的select语句来加载关联的实体(本例中就是教师实体),然后在association元素中引用此select语句(注:此方法会产生N+1问题,尽量不要用嵌套的select语句)
- <?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="net.zaodk.mybatis.mapper.StudentOperationMapper">
- <!-- 定义java Bean的属性与数据库的列之间的映射 -->
- <resultMap type="Teacher" id="supervisorResultMap">
- <id property="id" column="t_id"/>
- <result property="name" column="t_name"/>
- <result property="gender" column="t_gender"/>
- <result property="researchArea" column="research_area"/>
- </resultMap>
- <resultMap type="Student" id="studentResultMap">
- <id column="id" property="id" />
- <result column="name" property="name" />
- <result column="gender" property="gender" />
- <result column="major" property="major" />
- <result column="grade" property="grade"/>
- <!-- 引用teacherResultMap -->
- <association property="supervisor" column="supervisor_id" select="selectSupervisor"/>
- </resultMap>
- <!-- SQL语句中以"#{}"的形式引用参数 -->
- <select id="getById" parameterType="int" resultMap="studentResultMap">
- select id,name,gender,major,grade,supervisor_id from student where id =#{id}
- </select>
- <select id="selectSupervisor" parameterType="int" resultMap="supervisorResultMap">
- select id,name,gender,research_area
- from teacher where id = #{id}
- </select> </mapper
标签:String,gender,id,st,Mybatis,用法,public,name 来源: https://www.cnblogs.com/feiwenstyle/p/16290425.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。