package com.dao;
import java.util.List;
import com.model.User;
public interface IuserDao {
List<User> selectAll();
List<User> UserAndDet();
List<User> oneTomany();
}
<?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.dao.IuserDao">
<!-- resultType 面向属性编程 -->
<!--学生表和学生明细表关联查询 -->
<select id="selectAll" resultType="com.model.User" >
select * from student,student_detail where student.sId = student_detail.sd_sId
</select>
<!-- resultMap 也是返回值类型 是一个组合类型/resultType是返回一个确定的javaBean -->
<resultMap type="user" id="UserAndDet">
<id column="sId" property="sId"></id><!--定义主键 -->
<result column="sName" property="sName"/>
<result column="sSex" property="sSex"/>
<!-- 设置一对一 -->
<association property="detail" javaType="User_detail">
<id column="sd_id" property="sd_id"></id>
<result column="sd_address" property="sd_address"/>
<result column="sd_tel" property="sd_tel"/>
<result column="sd_sId" property="sd_sId"/>
</association>
</resultMap>
<!-- <select id="UserAndDet" resultMap="UserAndDet">
select * from student,student_detail where student.sId = student_detail.sd_sId
</select> -->
<!-- 主表信息 -->
<resultMap type="user" id="oneTomanyTest">
<id column="sId" property="sId"></id><!--定义主键 -->
<result column="sName" property="sName"/>
<result column="sSex" property="sSex"/>
<!-- 从表信息 -->
<collection property="SelectCourse" ofType="com.model.SelectCourse" column="sId" select ="lateselectCourse">
<id column="se_id" property="se_id"/>
<result column="se_name" property="se_name"/>
<result column="se_sId" property="se_sId"/>
</collection>
</resultMap>
<select id="oneTomany" resultMap="oneTomanyTest">
select * from selectcourse,student where student.sId=selectcourse.se_sId
</select>
<!--延迟加载 -->
<select id="lateselectCourse" resultType="SelectCourse" parameterType="int">
select * from selectcourse where se_sId = #{id}
</select>
</mapper>
package com.model;
public class SelectCourse {
@Override
public String toString() {
return "SelectCourse [se_id=" + se_id + ", se_name=" + se_name + ", se_sId=" + se_sId + "]";
}
private int se_id;
private String se_name;
private int se_sId;
public int getSe_id() {
return se_id;
}
public void setSe_id(int se_id) {
this.se_id = se_id;
}
public String getSe_name() {
return se_name;
}
public void setSe_name(String se_name) {
this.se_name = se_name;
}
public int getSe_sId() {
return se_sId;
}
public void setSe_sId(int se_sId) {
this.se_sId = se_sId;
}
}
package com.model;
public class User_detail {
private int sd_id;
private String sd_address;
private String sd_tel;
private int sd_sId;
public int getSd_id() {
return sd_id;
}
public void setSd_id(int sd_id) {
this.sd_id = sd_id;
}
public String getSd_address() {
return sd_address;
}
public void setSd_address(String sd_address) {
this.sd_address = sd_address;
}
public String getSd_tel() {
return sd_tel;
}
public void setSd_tel(String sd_tel) {
this.sd_tel = sd_tel;
}
public int getSd_sId() {
return sd_sId;
}
public void setSd_sId(int sd_sId) {
this.sd_sId = sd_sId;
}
@Override
public String toString() {
return "User_detail [sd_id=" + sd_id + ", sd_address=" + sd_address + ", sd_tel=" + sd_tel + "]";
}
}
package com.model;
import java.util.List;
public class User {
private int sId;
private String sName;
private String sSex;
private User_detail detail;
private List<SelectCourse> SelectCourse;
public int getsId() {
return sId;
}
public void setsId(int sId) {
this.sId = sId;
}
public String getsName() {
return sName;
}
public void setsName(String sName) {
this.sName = sName;
}
public String getsSex() {
return sSex;
}
public void setsSex(String sSex) {
this.sSex = sSex;
}
public User_detail getDetail() {
return detail;
}
public void setDetail(User_detail detail) {
this.detail = detail;
}
public List<SelectCourse> getSelectCourse() {
return SelectCourse;
}
public void setSelectCourse(List<SelectCourse> selectCourse) {
SelectCourse = selectCourse;
}
@Override
public String toString() {
return "User [sId=" + sId + ", sName=" + sName + ", sSex=" + sSex + ", SelectCourse=" + SelectCourse + "]";
}
}
<?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="jdbcInfo.properties"/>
<settings>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
<typeAliases><!--给model类设置别名 -->
<package name="com.model"/>
</typeAliases>
<environments default="development">
<environment id="development">
<!--配置JDBC事务控制,由mybatis进行管理 -->
<transactionManager type="JDBC"></transactionManager>
<!--配置数据源,采用mybatis连接池 -->
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driverClass}" />
<property name="url" value="${jdbc.jdbcUrl}" />
<property name="username" value="${jdbc.user}" />
<property name="password" value="${jdbc.password}" />
</dataSource>
</environment>
</environments>
<mappers>
<!-- 扫描包 -->
<package name="com.dao"/>
</mappers>
</configuration>