在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="tid"两个表的关联点 --> <collection property="user" column="tid" resultMap="springboot.mapper.UserMapper.BaseResultMap" ></collection> </resultMap> <sql id="Base_Column_List" > tid, tname, tphone, taddr, cid </sql> <!--老师跟学生一对多关联--> <select id="findUser" resultMap="BaseResultMap" parameterType="java.lang.Integer"> SELECT t.* ,u.* FROM teacher t,user u WHERE t.tid=u.tid and t.tid = #{tid,jdbcType=INTEGER} </select> <!--end-->dao层写法:
public interface TeacherMapper { //老师跟学生一对多关系 Teacher findUser(Integer id); }model层:
public class Teacher { private Integer tid; private String tname; private String tphone; private String taddr; private Integer cid; //新增学生列表,一个老师教多个学生 private List<User> user; public List<User> getUser() { return user; } public void setUser(List<User> user) { this.user = user; } 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 teacher2=teacherMapper.findUser(1); System.out.println("teacher:"+teacher2.toString()); List<User>list=teacher2.getUser(); for (User user:list){ System.out.println("user:"+user.toString()); } }测试效果:
老师和学生一对多联系: DEBUG [main] - Creating a new SqlSession DEBUG [main] - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7e38a7fe] 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.* ,u.* FROM teacher t,user u WHERE t.tid=u.tid and t.tid = ? DEBUG [main] - ==> Parameters: 1(Integer) DEBUG [main] - <== Total: 4 DEBUG [main] - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7e38a7fe] DEBUG [main] - Returning JDBC Connection to DataSource teacher:Teacher{tid=1, tname='老张', tphone='1234', taddr='广州', cid=1} user:User{id=1, uname='小黑', upwd='123', unum='123', sex=0, age=18, addr='广东', tid=1} user:User{id=2, uname='小白', upwd='234', unum='234', sex=1, age=21, addr='广州', tid=1} user:User{id=6, uname='桌号', upwd='123', unum='463', sex=1, age=12, addr='null', tid=1} user:User{id=18, uname='黑杰克', upwd='123', unum='123', sex=1, age=18, addr='进一步', tid=1}我的座右铭:不会,我可以学;落后,我可以追赶;跌倒,我可以站起来;我一定行。
