mybatis代码记录

it2025-03-16  10

1.多对一的处理

<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.xk.dao.StudentMapper"> <!--方式一:嵌套查询 按照查询进行嵌套处理就像SQL中的子查询--> <select id="getStudent" resultMap="ST"> select * from student </select> <resultMap id="ST" type="Student"> <id property="id" column="id"/> <result property="name" column="name"/> <association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/> </resultMap> <select id="getTeacher" resultType="Teacher"> select * from teacher where id=#{idHereYouCanXJBWrite} </select> <!--方式二:联表查询--> <!--映射到pojo的属性名 按照结果进行嵌套处理就像SQL中的联表查询--> <resultMap id="ST2" type="Student"> <result property="id" column="sid"/> <result property="name" column="sname"/> <association property="teacher" column="tid" javaType="Teacher"> <result property="id" column="tid"/> <result property="name" column="tname"/> </association> </resultMap> <select id="getStudent2" resultMap="ST2"> select s.id sid,s.name sname,t.id tid,t.name tname from student s,teacher t where s.tid=t.id; </select> </mapper>
最新回复(0)