表结构
DROP TABLE IF EXISTS `t_lecture`; CREATE TABLE `t_lecture` ( `id` int(20) NOT NULL AUTO_INCREMENT COMMENT '编号', `lecture_name` varchar(60) NOT NULL COMMENT '课程名称', `note` varchar(1024) DEFAULT NULL COMMENT '备注', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; INSERT INTO `t_lecture` (`id`, `lecture_name`, `note`) VALUES (1001,'高等数学','math'), (1002,'C语言程序设计','program language'); DROP TABLE IF EXISTS `t_student`; CREATE TABLE `t_student` ( `id` int(20) NOT NULL AUTO_INCREMENT COMMENT '编号', `cnname` varchar(60) NOT NULL COMMENT '学生姓名', `sex` tinyint(4) NOT NULL COMMENT '性别', `selfcard_no` int(20) NOT NULL COMMENT '学生证号', `note` varchar(1024) DEFAULT NULL COMMENT '备注', `effective` tinyint(4) NOT NULL COMMENT '是否有效', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; INSERT INTO `t_student` (`id`, `cnname`, `sex`, `selfcard_no`, `note`, `effective`) VALUES (2017053001,'张磊',1,2017053001,'张磊的测试',1), (2017053002,'王梅',2,2017053002,'王梅的测试',1); DROP TABLE IF EXISTS `t_student_health_female`; CREATE TABLE `t_student_health_female` ( `id` int(20) NOT NULL AUTO_INCREMENT COMMENT '编号', `student_id` int(20) NOT NULL COMMENT '学生编号', `check_date` varchar(60) NOT NULL COMMENT '检查日期', `heart` varchar(60) NOT NULL COMMENT '心', `liver` varchar(60) NOT NULL COMMENT '肝', `spleen` varchar(60) NOT NULL COMMENT '脾', `lung` varchar(60) NOT NULL COMMENT '肺', `kidney` varchar(60) NOT NULL COMMENT '肾', `uterus` varchar(60) NOT NULL COMMENT '子宫', `note` varchar(1024) DEFAULT NULL COMMENT '备注', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; INSERT INTO `t_student_health_female` (`id`, `student_id`, `check_date`, `heart`, `liver`, `spleen`, `lung`, `kidney`, `uterus`, `note`) VALUES (1,2017053002,'2017-04-01','bad','bad','bad','bad','bad','good','total bad'); DROP TABLE IF EXISTS `t_student_health_male`; CREATE TABLE `t_student_health_male` ( `id` int(20) NOT NULL AUTO_INCREMENT COMMENT '编号', `student_id` int(20) NOT NULL COMMENT '学生编号', `check_date` varchar(60) NOT NULL COMMENT '检查日期', `heart` varchar(60) NOT NULL COMMENT '心', `liver` varchar(60) NOT NULL COMMENT '肝', `spleen` varchar(60) NOT NULL COMMENT '脾', `lung` varchar(60) NOT NULL COMMENT '肺', `kidney` varchar(60) NOT NULL COMMENT '肾', `prostate` varchar(60) NOT NULL COMMENT '前列腺', `note` varchar(1024) DEFAULT NULL COMMENT '备注', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; INSERT INTO `t_student_health_male` (`id`, `student_id`, `check_date`, `heart`, `liver`, `spleen`, `lung`, `kidney`, `prostate`, `note`) VALUES (1,2017053001,'2017-04-01','good','good','good','good','good','normal','total good'); DROP TABLE IF EXISTS `t_student_lecture`; CREATE TABLE `t_student_lecture` ( `id` int(20) NOT NULL AUTO_INCREMENT COMMENT '编号', `student_id` int(20) NOT NULL COMMENT '学生编号', `lecture_id` int(20) NOT NULL COMMENT '课程编号', `grade` decimal(16,2) NOT NULL COMMENT '评分', `note` varchar(1024) DEFAULT NULL COMMENT '备注', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; INSERT INTO `t_student_lecture` (`id`, `student_id`, `lecture_id`, `grade`, `note`) VALUES (1,2017053001,1001,98.00,'good'), (2,2017053001,1002,61.00,'bad'); DROP TABLE IF EXISTS `t_student_selfcard`; CREATE TABLE `t_student_selfcard` ( `id` int(20) NOT NULL AUTO_INCREMENT COMMENT '编号', `student_id` int(20) NOT NULL COMMENT '学生编号', `native` varchar(60) NOT NULL COMMENT '籍贯', `issue_date` date NOT NULL COMMENT '发证日期', `end_date` date NOT NULL COMMENT '结束发证日期', `note` varchar(1024) DEFAULT NULL COMMENT '备注', `student_effective` tinyint(4) NOT NULL COMMENT '学生是否有效', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; INSERT INTO `t_student_selfcard` (`id`, `student_id`, `native`, `issue_date`, `end_date`, `note`, `student_effective`) VALUES (1,2017053001,'江苏','2017-01-01','2017-12-31','张三的学生证测试',1);
CREATE DEFINER=`root`@`localhost` PROCEDURE `getStudentAndStudentSelfcard`(in studentId int)
begin
select id, cnname, sex, note, effective
from t_student where id = studentId;
select id, student_id, native, issue_date, end_date, note, student_effective
from t_student_selfcard where student_id = studentId;
end
实体定义
public enum SexEnum { MALE(1, "男"), FEMALE(2, "女"); private int id; private String name; private SexEnum(int id, String name) { this.id = id; this.name = name; } 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 static SexEnum getSex(int id) { if (id == 1) { return MALE; } else if (id == 2) { return FEMALE; } return null; } }
public class StudentBean { private Integer id; private String cnname; private SexEnum sex; private Integer selfcardNo; private String note; private Boolean effective; private StudentSelfcardBean studentSelfcard; private List<StudentLectureBean> studentLectureList;
public StudentBean(Integer id, String cnname) {
this.id = id;
this.cnname = cnname;
}
public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getCnname() { return cnname; } public void setCnname(String cnname) { this.cnname = cnname; } public SexEnum getSex() { return sex; } public void setSex(SexEnum sex) { this.sex = sex; } public Integer getSelfcardNo() { return selfcardNo; } public void setSelfcardNo(Integer selfcardNo) { this.selfcardNo = selfcardNo; } public String getNote() { return note; } public void setNote(String note) { this.note = note; } public Boolean getEffective() { return effective; } public void setEffective(Boolean effective) { this.effective = effective; } public StudentSelfcardBean getStudentSelfcard() { return studentSelfcard; } public void setStudentSelfcard(StudentSelfcardBean studentSelfcard) { this.studentSelfcard = studentSelfcard; } public List<StudentLectureBean> getStudentLectureList() { return studentLectureList; } public void setStudentLectureList(List<StudentLectureBean> studentLectureList) { this.studentLectureList = studentLectureList; } @Override public String toString() { return "StudentBean [id=" + id + ", cnname=" + cnname + ", sex=" + sex + ", selfcardNo=" + selfcardNo + ", note=" + note + ", effective=" + effective + ", studentSelfcard=" + studentSelfcard + ", studentLectureList=" + studentLectureList + "]"; } } public class LectureBean { private Integer id; private String lectureName; private String note; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getLectureName() { return lectureName; } public void setLectureName(String lectureName) { this.lectureName = lectureName; } public String getNote() { return note; } public void setNote(String note) { this.note = note; } @Override public String toString() { return "LectureBean [id=" + id + ", lectureName=" + lectureName + ", note=" + note + "]"; } } public class StudentSelfcardBean { private Integer id; private Integer studentId; private String native_; private Date issueDate; private Date endDate; private String note; private Integer studentEffective; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getStudentId() { return studentId; } public void setStudentId(Integer studentId) { this.studentId = studentId; } public String getNative_() { return native_; } public void setNative_(String native_) { this.native_ = native_; } public Date getIssueDate() { return issueDate; } public void setIssueDate(Date issueDate) { this.issueDate = issueDate; } public Date getEndDate() { return endDate; } public void setEndDate(Date endDate) { this.endDate = endDate; } public String getNote() { return note; } public void setNote(String note) { this.note = note; } public Integer getStudentEffective() { return studentEffective; } public void setStudentEffective(Integer studentEffective) { this.studentEffective = studentEffective; } @Override public String toString() { return "StudentSelfcardBean [id=" + id + ", studentId=" + studentId + ", native_=" + native_ + ", issueDate=" + issueDate + ", endDate=" + endDate + ", note=" + note + ", studentEffective=" + studentEffective + "]"; } } public class StudentLectureBean { private int id; private Integer studentId; private LectureBean lecture; private BigDecimal grade; private String note; public int getId() { return id; } public void setId(int id) { this.id = id; } public Integer getStudentId() { return studentId; } public void setStudentId(Integer studentId) { this.studentId = studentId; } public LectureBean getLecture() { return lecture; } public void setLecture(LectureBean lecture) { this.lecture = lecture; } public BigDecimal getGrade() { return grade; } public void setGrade(BigDecimal grade) { this.grade = grade; } public String getNote() { return note; } public void setNote(String note) { this.note = note; } @Override public String toString() { return "StudentLectureBean [id=" + id + ", studentId=" + studentId + ", lecture=" + lecture + ", grade=" + grade + ", note=" + note + "]"; } } public class StudentHealthBean { private Integer id; private Integer studentId; private String checkDate; private String heart; private String liver; private String spleen; private String lung; private String kidney; private String note; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getStudentId() { return studentId; } public void setStudentId(Integer studentId) { this.studentId = studentId; } public String getCheckDate() { return checkDate; } public void setCheckDate(String checkDate) { this.checkDate = checkDate; } public String getHeart() { return heart; } public void setHeart(String heart) { this.heart = heart; } public String getLiver() { return liver; } public void setLiver(String liver) { this.liver = liver; } public String getSpleen() { return spleen; } public void setSpleen(String spleen) { this.spleen = spleen; } public String getLung() { return lung; } public void setLung(String lung) { this.lung = lung; } public String getKidney() { return kidney; } public void setKidney(String kidney) { this.kidney = kidney; } public String getNote() { return note; } public void setNote(String note) { this.note = note; } @Override public String toString() { return "StudentHealthBean [id=" + id + ", studentId=" + studentId + ", checkDate=" + checkDate + ", heart=" + heart + ", liver=" + liver + ", spleen=" + spleen + ", lung=" + lung + ", kidney=" + kidney + ", note=" + note + "]"; } } public class StudentHealthFemaleBean extends StudentHealthBean { private String uterus; public String getUterus() { return uterus; } public void setUterus(String uterus) { this.uterus = uterus; } @Override public String toString() { return "StudentHealthFemaleBean [uterus=" + uterus + "]" + super.toString(); } } public class StudentHealthMaleBean extends StudentHealthBean { private String prostate; public String getProstate() { return prostate; } public void setProstate(String prostate) { this.prostate = prostate; } @Override public String toString() { return "StudentHealthMaleBean [prostate=" + prostate + "]" + super.toString(); } } public class FemaleStudentBean extends StudentBean {
public FemaleStudentBean(Integer id, String cnname) {
super(id, cnname);
}
private List<StudentHealthFemaleBean> studentHealthFemaleList = null; public List<StudentHealthFemaleBean> getStudentHealthFemaleList() { return studentHealthFemaleList; } public void setStudentHealthFemaleList(List<StudentHealthFemaleBean> studentHealthFemaleList) { this.studentHealthFemaleList = studentHealthFemaleList; } @Override public String toString() { return "FemaleStudentBean [studentHealthFemaleList=" + studentHealthFemaleList + "]" + super.toString(); } } public class MaleStudentBean extends StudentBean {
public MaleStudentBean(Integer id, String cnname) {
super(id, cnname);
}
private List<StudentHealthMaleBean> studentHealthMaleList = null; public List<StudentHealthMaleBean> getStudentHealthMaleList() { return studentHealthMaleList; } public void setStudentHealthMaleList(List<StudentHealthMaleBean> studentHealthMaleList) { this.studentHealthMaleList = studentHealthMaleList; } @Override public String toString() { return "MaleStudentBean [studentHealthMaleList=" + studentHealthMaleList + "]" + super.toString(); } }
typehandler定义
public class SexTypeHandler implements TypeHandler<SexEnum> { @Override public void setParameter(PreparedStatement ps, int i, SexEnum parameter, JdbcType jdbcType) throws SQLException { ps.setInt(i, parameter.getId()); } @Override public SexEnum getResult(ResultSet rs, String columnName) throws SQLException { int id = rs.getInt(columnName); return SexEnum.getSex(id); } @Override public SexEnum getResult(ResultSet rs, int columnIndex) throws SQLException { int id = rs.getInt(columnIndex); return SexEnum.getSex(id); } @Override public SexEnum getResult(CallableStatement cs, int columnIndex) throws SQLException { int id = cs.getInt(columnIndex); return SexEnum.getSex(id); } }
mapper层接口
public interface StudentMapper { public StudentBean getStudent(int id); public List<StudentBean> findAllStudent();
public StudentBean getStudentAndStudentSelfcard(int studentId); } public interface StudentSelfcardMapper { public StudentSelfcardBean findStudentSelfcardByStudentId(int studentId, Boolean studentEffective); } public interface LectureMapper { public LectureBean getLecture(int id); } public interface StudentLectureMapper { public List<StudentLectureBean> findStudentLectureByStudentId(int id); } public interface StudentHealthFemaleMapper { public List<StudentHealthFemaleBean> findStudentHealthFemaleByStuId(int stuId); } public interface StudentHealthMaleMapper { public List<StudentHealthMaleBean> findStudentHealthMaleByStuId(int stuId); }
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="com.kafoming.mybatis.test.mapper.StudentMapper"> <resultMap id="studentMap" type="com.kafoming.mybatis.test.model.StudentBean"> <id property="id" column="id" /> <result property="cnname" column="cnname" /> <result property="sex" column="sex" jdbcType="INTEGER" javaType="com.kafoming.mybatis.test.enums.SexEnum" typeHandler="com.kafoming.mybatis.test.typehandler.SexTypeHandler"/> <result property="note" column="note" /> <result property="effective" column="effective" /> <!-- notNullColumn没起作用 ??? --> <association property="studentSelfcard" column="{studentId=id, studentEffective=effective}" select="com.kafoming.mybatis.test.mapper.StudentSelfcardMapper.findStudentSelfcardByStudentId" notNullColumn="note" fetchType="lazy" /> <!-- 此处的javaType可以省略 --> <collection property="studentLectureList" column="id" javaType="ArrayList" ofType="com.kafoming.mybatis.test.model.StudentLectureBean" select="com.kafoming.mybatis.test.mapper.StudentLectureMapper.findStudentLectureByStudentId" fetchType="eager" /> <discriminator javaType="int" column="sex"> <case value="1" resultMap="maleStudentMap" /> <case value="2" resultMap="femaleStudentMap" /> </discriminator> </resultMap> <!-- resultMap中可以使用逗号分隔 --> <select id="getStudent" parameterType="int" resultMap="studentMap"> select id, cnname, sex, note, effective from t_student where id = #{id} </select> <resultMap id="maleStudentMap" type="com.kafoming.mybatis.test.model.MaleStudentBean" extends="studentMap"> <constructor> <idArg column="id" javaType="int"/> <arg column="cnname" javaType="String"/> </constructor> <collection property="studentHealthMaleList" column="id" select="com.kafoming.mybatis.test.mapper.StudentHealthMaleMapper.findStudentHealthMaleByStuId" fetchType="lazy" /> </resultMap> <resultMap id="femaleStudentMap" type="com.kafoming.mybatis.test.model.FemaleStudentBean" extends="studentMap"> <constructor> <idArg column="id" javaType="int"/> <arg column="cnname" javaType="String"/> </constructor> <collection property="studentHealthFemaleList" column="id" select="com.kafoming.mybatis.test.mapper.StudentHealthFemaleMapper.findStudentHealthFemaleByStuId" fetchType="lazy" /> </resultMap> <select id="findAllStudent" resultMap="studentMap2"> select s.id, s.cnname, s.sex, s.note as snote, if (sex = 1, shm.id, shf.id) as hid, if (sex = 1, shm.check_date, shf.check_date) as check_date, if (sex = 1, shm.heart, shf.heart) as heart, if (sex = 1, shm.liver, shf.liver) as liver, if (sex = 1, shm.spleen, shf.spleen) as spleen, if (sex = 1, shm.lung, shf.lung) as lung, if (sex = 1, shm.kidney, shf.kidney) as kidney, if (sex = 1, shm.note, shf.note) as shnote, shm.prostate, shf.uterus, ss.id as ss_ssid, ss.student_id as ss_student_id, ss.native as ss_native_, ss.issue_date as ss_issue_date, ss.end_date as ss_end_date, ss.note as ss_ssnote, sl.id as slid, sl.lecture_id, sl.grade, sl.note as slnote, l.lecture_name, l.note as lnote from t_student s left join t_student_health_male shm on s.id = shm.student_id left join t_student_health_female shf on s.id = shf.student_id left join t_student_selfcard ss on s.id = ss.student_id left join t_student_lecture sl on s.id = sl.student_id left join t_lecture l on sl.lecture_id = l.id </select> <resultMap id="studentSelfcard2" type="com.kafoming.mybatis.test.model.StudentSelfcardBean"> <id property="id" column="ssid" /> <result property="studentId" column="student_id" /> <result property="native_" column="native_" /> <result property="issueDate" column="issue_date" /> <result property="endDate" column="end_date" /> <result property="note" column="ssnote" /> </resultMap> <!-- 这里association中的column都可以省略 --> <resultMap id="studentMap2" type="com.kafoming.mybatis.test.model.StudentBean"> <id property="id" column="id" /> <result property="cnname" column="cnname" /> <result property="sex" column="sex" jdbcType="INTEGER" javaType="com.kafoming.mybatis.test.enums.SexEnum" typeHandler="com.kafoming.mybatis.test.typehandler.SexTypeHandler"/> <result property="note" column="note" /> <!-- column不可使用多个字段 ??? --> <!-- 映射到resultMap中的字段都必须带上前缀ss_ --> <association property="studentSelfcard" resultMap="studentSelfcard2" columnPrefix="ss_" notNullColumn="ssnote" /> <collection property="studentLectureList" ofType="com.kafoming.mybatis.test.model.StudentLectureBean"> <result property="id" column="slid" /> <result property="studentId" column="id" /> <result property="grade" column="grade" /> <result property="note" column="slnote" /> <association property="lecture" javaType="com.kafoming.mybatis.test.model.LectureBean"> <result property="id" column="lecture_id" /> <result property="lectureName" column="lecture_name" /> <result property="note" column="lnote" /> </association> </collection> <!-- column是 MyBatis 查找比较值的地方。 JavaType 是需要被用来保证等价测试的合适类型(尽管字符串在很多情形下都会有用) --> <discriminator javaType="int" column="sex"> <case value="1" resultMap="maleStudentMap2" /> <case value="2" resultMap="femaleStudentMap2" /> </discriminator> </resultMap> <resultMap id="maleStudentMap2" type="com.kafoming.mybatis.test.model.MaleStudentBean" extends="studentMap2"> <constructor> <idArg column="id" javaType="int"/> <arg column="cnname" javaType="String"/> </constructor> <collection property="studentHealthMaleList" ofType="com.kafoming.mybatis.test.model.StudentHealthMaleBean"> <id property="id" column="hid" javaType="int" /> <result property="checkDate" column="check_date" /> <result property="heart" column="heart" /> <result property="liver" column="liver" /> <result property="spleen" column="spleen" /> <result property="lung" column="lung" /> <result property="kidney" column="kidney" /> <result property="prostate" column="prostate" /> <result property="note" column="shnote" /> </collection> </resultMap> <resultMap id="femaleStudentMap2" type="com.kafoming.mybatis.test.model.FemaleStudentBean" extends="studentMap2"> <constructor> <idArg column="id" javaType="int"/> <arg column="cnname" javaType="String"/> </constructor> <collection property="studentHealthFemaleList" ofType="com.kafoming.mybatis.test.model.StudentHealthFemaleBean"> <id property="id" column="hid" javaType="int" /> <result property="checkDate" column="check_date" /> <result property="heart" column="heart" /> <result property="liver" column="liver" /> <result property="spleen" column="spleen" /> <result property="lung" column="lung" /> <result property="kidney" column="kidney" /> <result property="uterus" column="uterus" /> <result property="note" column="shnote" /> </collection> </resultMap> <!-- 多结果集 --> <select id="getStudentAndStudentSelfcard" parameterType="int" resultSets="students,studentSelfcards" resultMap="studentMap3" statementType="CALLABLE"> { call getStudentAndStudentSelfcard(#{studentId, jdbcType=INTEGER, mode=IN}) } </select> <resultMap id="studentMap3" type="com.kafoming.mybatis.test.model.StudentBean"> <constructor> <idArg column="id" javaType="int"/> <arg column="cnname" javaType="String"/> </constructor> <id property="id" column="id" /> <result property="cnname" column="cnname" /> <result property="sex" column="sex" jdbcType="INTEGER" javaType="com.kafoming.mybatis.test.enums.SexEnum" typeHandler="com.kafoming.mybatis.test.typehandler.SexTypeHandler"/> <result property="note" column="note" /> <result property="effective" column="effective" /> <association property="studentSelfcard" javaType="com.kafoming.mybatis.test.model.StudentSelfcardBean" resultSet="studentSelfcards" column="id" foreignColumn="student_id"><!-- foreignColumn包含多个字段如何表示 --> <id property="id" column="id" /> <result property="studentId" column="student_id" /> <result property="native_" column="native" /> <result property="issueDate" column="issue_date" /> <result property="endDate" column="end_date" /> <result property="note" column="note" /> <result property="studentEffective" column="student_effective" /> </association> </resultMap> </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="com.kafoming.mybatis.test.mapper.LectureMapper"> <select id="getLecture" parameterType="int" resultType="com.kafoming.mybatis.test.model.LectureBean"> select id, lecture_name as lectureName, note from t_lecture where id = #{id} </select> </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="com.kafoming.mybatis.test.mapper.StudentSelfcardMapper"> <resultMap id="studentSelfcardMap" type="com.kafoming.mybatis.test.model.StudentSelfcardBean"> <id property="id" column="id" /> <result property="studentId" column="student_id" /> <result property="native_" column="native" /> <result property="issueDate" column="issue_date" /> <result property="endDate" column="end_date" /> <result property="note" column="note" /> <result property="studentEffective" column="student_effective" /> </resultMap> <select id="findStudentSelfcardByStudentId" parameterType="map" resultMap="studentSelfcardMap"> select id, student_id, native, issue_date, end_date, note, student_effective from t_student_selfcard where student_id = #{studentId} and student_effective = #{studentEffective} </select> </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="com.kafoming.mybatis.test.mapper.StudentLectureMapper"> <resultMap id="studentLectureMap" type="com.kafoming.mybatis.test.model.StudentLectureBean"> <id property="id" column="id" /> <result property="studentId" column="student_id" /> <result property="grade" column="grade" /> <result property="note" column="note" /> <association property="lecture" column="lecture_id" select="com.kafoming.mybatis.test.mapper.LectureMapper.getLecture" fetchType="lazy" /> </resultMap> <select id="findStudentLectureByStudentId" parameterType="int" resultMap="studentLectureMap"> select id, student_id, lecture_id, grade, note from t_student_lecture where student_id = #{id} </select> </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="com.kafoming.mybatis.test.mapper.StudentHealthFemaleMapper"> <resultMap id="studentHealthFemaleMap" type="com.kafoming.mybatis.test.model.StudentHealthFemaleBean" extends="com.kafoming.mybatis.test.mapper.StudentHealthMaleMapper.studentHealthMap"> <result property="uterus" column="uterus" /> </resultMap> <select id="findStudentHealthFemaleByStuId" parameterType="int" resultMap="studentHealthFemaleMap"> select id, student_id, check_date, heart, liver, spleen, lung, kidney, uterus, note from t_student_health_female where student_id = #{stuId} </select> </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="com.kafoming.mybatis.test.mapper.StudentHealthMaleMapper"> <resultMap id="studentHealthMap" type="com.kafoming.mybatis.test.model.StudentHealthBean"> <id property="id" column="id" /> <result property="studentId" column="student_id" /> <result property="checkDate" column="check_date" /> <result property="heart" column="heart" /> <result property="liver" column="liver" /> <result property="spleen" column="spleen" /> <result property="lung" column="lung" /> <result property="kidney" column="kidney" /> <result property="note" column="note" /> </resultMap> <resultMap id="studentHealthMaleMap" type="com.kafoming.mybatis.test.model.StudentHealthMaleBean" extends="studentHealthMap"> <result property="prostate" column="prostate" /> </resultMap> <select id="findStudentHealthMaleByStuId" parameterType="int" resultMap="studentHealthMaleMap"> select id, student_id, check_date, heart, liver, spleen, lung, kidney, prostate, note from t_student_health_male where student_id = #{stuId} </select> </mapper>
mybaits配置文件
<?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> <settings> <setting name="lazyLoadingEnabled" value="true" /> <setting name="aggressiveLazyLoading" value="false" /> </settings> <typeHandlers> <package name="com.kafoming.mybatis.test" /> </typeHandlers> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://${db.host}:${db.port}/${db.name}?useUnicode=true&characterEncoding=utf-8" /> <property name="username" value="root" /> <property name="password" value="123456" /> </dataSource> </environment> </environments> <databaseIdProvider type="DB_VENDOR"> <property name="MySQL" value="mysql" /> </databaseIdProvider> <mappers> <!-- <mapper resource="com/kafoming/mybatis/test/mapper/EmployeesMapper.xml" /> --> <mapper resource="com/kafoming/mybatis/test/mapper/StudentMapper.xml" /> <mapper resource="com/kafoming/mybatis/test/mapper/StudentSelfcardMapper.xml" /> <mapper resource="com/kafoming/mybatis/test/mapper/LectureMapper.xml" /> <mapper resource="com/kafoming/mybatis/test/mapper/StudentLectureMapper.xml" /> <mapper resource="com/kafoming/mybatis/test/mapper/StudentHealthMaleMapper.xml" /> <mapper resource="com/kafoming/mybatis/test/mapper/StudentHealthFemaleMapper.xml" /> </mappers> </configuration>
日志配置文件
#============================================================================ # rootLogger #============================================================================ log4j.rootLogger=DEBUG,CONSOLE log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.CONSOLE.target=System.out log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern=%d %-5p [%t] [%F:%L] [%X{traceId}] - %m%n
主程序
public class StudentDemo { public static void main(String[] args) throws IOException { /* * 1.加载mybatis的配置文件,初始化mybatis,创建出SqlSessionFactory,是创建SqlSession的工厂 * 这里只是为了演示的需要,SqlSessionFactory临时创建出来,在实际的使用中,SqlSessionFactory只需要创建一次,当作单例来使用 */ InputStream inputStream = Resources.getResourceAsStream("mybatisConfig.xml"); SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); String environment = "development"; Properties properties = new Properties(); properties.put("db.host", "localhost"); properties.put("db.port", "3306"); properties.put("db.name", "mybatis_test"); SqlSessionFactory factory = builder.build(inputStream, environment, properties); // 2. 从SqlSession工厂 SqlSessionFactory中创建一个SqlSession,进行数据库操作 SqlSession sqlSession = factory.openSession(); StudentMapper stuMapper = sqlSession.getMapper(StudentMapper.class); // StudentBean stu = stuMapper.getStudent(2017053001); // System.out.println(stu); // stu = stuMapper.getStudent(2017053002); // System.out.println(stu); List<StudentBean> stus = stuMapper.findAllStudent(); for (StudentBean stu : stus) { System.out.println(stu); } } }