在mapper.xml中写法:
<mapper namespace="springboot.mapper.TeacherMapper" > <resultMap id="BaseResultMap" type="springboot.model.Teacher" > <id column="tid" property="tid" jdbcType="INTEGER" /> <result column="tname" property="tname" jdbcType="VARCHAR" /> <result column="tphone" property="tphone" jdbcType="VARCHAR" /> <result column="taddr" property="taddr" jdbcType="VARCHAR" /> <result column="cid" property="cid" jdbcType="INTEGER" /> <!-- 一对一的关联关系 column="cid"两个表的关联点 --> <association property="course" column="cid" resultMap="springboot.mapper.CourseMapper.BaseResultMap"></association> </resultMap> <sql id="Base_Column_List" > tid, tname, tphone, taddr, cid </sql> <!--一对一的关联sql语句,cid作为2个表的外键--> <select id="findTeacher" resultMap="BaseResultMap" parameterType="java.lang.Integer"> SELECT t.* ,c.* FROM teacher t,course c WHERE t.cid=c.cid and t.tid = #{tid,jdbcType=INTEGER} </select>dao层写法:
public interface TeacherMapper { //老师跟课程一对一的关联 Teacher findTeacher(Integer id); }model层:
public class Teacher { private Integer tid; private String tname; private String tphone; private String taddr; private Integer cid; //新增的课程属性 private Course course; public Course getCourse() { return course; } public void setCourse(Course course) { this.course = course; } public Integer getTid() { return tid; } public void setTid(Integer tid) { this.tid = tid; } public String getTname() { return tname; } public void setTname(String tname) { this.tname = tname; } public String getTphone() { return tphone; } public void setTphone(String tphone) { this.tphone = tphone; } public String getTaddr() { return taddr; } public void setTaddr(String taddr) { this.taddr = taddr; } public Integer getCid() { return cid; } public void setCid(Integer cid) { this.cid = cid; } @Override public String toString() { return "Teacher{" + "tid=" + tid + ", tname='" + tname + '\'' + ", tphone='" + tphone + '\'' + ", taddr='" + taddr + '\'' + ", cid=" + cid + '}'; } }springboot测试类:
@SpringBootApplication @MapperScan("springboot.mapper") public class Application { public static void main( String[] args ) { ApplicationContext context = SpringApplication.run(Application.class, args); TeacherMapper teacherMapper=context.getBean(TeacherMapper.class); System.out.println("老师和课程一对一联系:"); Teacher teacher1=teacherMapper.findTeacher(2); System.out.println(teacher1.getTname()+"------"+teacher1.getCourse().toString()); }测试效果:
老师和课程一对一联系: DEBUG [main] - Creating a new SqlSession DEBUG [main] - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4b3a45f1] was not registered for synchronization because synchronization is not active DEBUG [main] - Fetching JDBC Connection from DataSource DEBUG [main] - JDBC Connection [ProxyConnection[PooledConnection[com.mysql.jdbc.JDBC4Connection@78a287ed]]] will not be managed by Spring DEBUG [main] - ==> Preparing: SELECT t.* ,c.* FROM teacher t,course c WHERE t.cid=c.cid and t.tid = ? DEBUG [main] - ==> Parameters: 2(Integer) DEBUG [main] - <== Total: 1 DEBUG [main] - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4b3a45f1] DEBUG [main] - Returning JDBC Connection to DataSource 老王------Course{cid=2, cname='C语言', ctime=56}我的座右铭:不会,我可以学;落后,我可以追赶;跌倒,我可以站起来;我一定行。
