一、

1.Mapper

2.Service

3.Domain

 package com.mybatis3.domain;

 import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List; /**
* @author Siva
*
*/
public class Course implements Serializable
{
private static final long serialVersionUID = 1L; private Integer courseId;
private String name;
private String description;
private Date startDate;
private Date endDate;
private Tutor tutor;
private List<Student> students; @Override
public String toString() {
return "Course [courseId=" + courseId + ", name=" + name + ", description="
+ description + ", startDate=" + startDate + ", endDate="
+ endDate + ", tutor=" + tutor + ", students=" + students + "]";
}
public Integer getCourseId()
{
return courseId;
}
public void setCourseId(Integer id)
{
this.courseId = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getDescription()
{
return description;
}
public void setDescription(String description)
{
this.description = description;
}
public Date getStartDate()
{
return startDate;
}
public void setStartDate(Date startDate)
{
this.startDate = startDate;
}
public Date getEndDate()
{
return endDate;
}
public void setEndDate(Date endDate)
{
this.endDate = endDate;
}
public List<Student> getStudents()
{
if(students == null){
students = new ArrayList<Student>(0);
}
return students;
}
public void setStudents(List<Student> students)
{
this.students = students;
}
public Tutor getTutor() {
return tutor;
}
public void setTutor(Tutor tutor) {
this.tutor = tutor;
} }
 package com.mybatis3.domain;

 import java.io.Serializable;
import java.util.List; /**
* @author Siva
*
*/
public class Tutor implements Serializable
{
private static final long serialVersionUID = 1L; private Integer tutorId;
private String name;
private String email;
private Address address;
private List<Course> courses; @Override
public String toString() {
return "Tutor [tutorId=" + tutorId + ", name=" + name + ", email=" + email
+ ", address=" + address + ", courses=" + courses + "]";
}
public Tutor()
{
}
public Tutor(Integer id)
{
this.tutorId = id;
}
public Integer getTutorId()
{
return tutorId;
}
public void setTutorId(Integer id)
{
this.tutorId = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
public Address getAddress()
{
return address;
}
public void setAddress(Address address)
{
this.address = address;
}
public List<Course> getCourses() {
return courses;
}
public void setCourses(List<Course> courses) {
this.courses = courses;
} }

4.辅助类

5.配置及资源文件

(1)AddressMapper.xml

 <?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.mybatis3.mappers.AddressMapper"> <resultMap type="Address" id="AddressResult">
<id property="addrId" column="addr_id"/>
<result property="street" column="street"/>
<result property="city" column="city"/>
<result property="state" column="state"/>
<result property="zip" column="zip"/>
<result property="country" column="country"/>
</resultMap> <select id="selectAddressById" parameterType="int" resultMap="AddressResult">
select * from addresses where addr_id=#{addrId}
</select> </mapper>

(2)CourseMapper.xml

 <?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.mybatis3.mappers.CourseMapper"> <cache eviction="FIFO" flushInterval="60000" size="512" readOnly="false"/> <resultMap type="Course" id="CourseResult">
<id column="course_id" property="courseId"/>
<result column="name" property="name"/>
<result column="description" property="description"/>
<result column="start_date" property="startDate"/>
<result column="end_date" property="endDate"/>
</resultMap> <select id="selectCoursesByTutor" parameterType="int" resultMap="CourseResult">
select * from courses where tutor_id=#{tutorId}
</select> <select id="searchCourses" parameterType="hashmap" resultMap="CourseResult" useCache="false">
SELECT * FROM COURSES
WHERE TUTOR_ID= #{tutorId}
<if test="courseName != null">
AND name like #{courseName}
</if>
<if test="startDate != null">
AND start_date &gt;= #{startDate}
</if>
<if test="endDate != null">
AND end_date &lt;= #{endDate}
</if> </select> <select id="searchCoursesByTutors" parameterType="hashmap" resultMap="CourseResult">
SELECT * FROM COURSES
<if test="tutorIds != null">
<where>
tutor_id IN
<foreach item="tutorId" collection="tutorIds"
open="(" separator="," close=")">
#{tutorId}
</foreach>
</where>
</if>
</select> </mapper>

(3)StudentMapper.xml

 <?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.mybatis3.mappers.StudentMapper"> <resultMap type="Student" id="StudentResult">
<id property="studId" column="stud_id"/>
<result property="name" column="name" />
<result property="email" column="email"/>
<result property="phone" column="phone"/>
</resultMap> <resultMap type="Student" id="StudentWithAddressExtResult" extends="StudentResult">
<result property="address.addrId" column="addr_id"/>
<result property="address.street" column="street"/>
<result property="address.city" column="city"/>
<result property="address.state" column="state"/>
<result property="address.zip" column="zip"/>
<result property="address.country" column="country"/>
</resultMap> <resultMap type="Student" id="StudentWithAddressNestedSelect">
<id property="studId" column="stud_id"/>
<result property="name" column="name"/>
<result property="email" column="email"/>
<association property="address" column="addr_id" select="com.mybatis3.mappers.AddressMapper.selectAddressById"/>
</resultMap> <resultMap type="Student" id="StudentWithAddressNestedResultMap">
<id property="studId" column="stud_id"/>
<result property="name" column="name"/>
<result property="email" column="email"/>
<association property="address" javaType="Address">
<id property="addrId" column="addr_id"/>
<result property="street" column="street"/>
<result property="city" column="city"/>
<result property="state" column="state"/>
<result property="zip" column="zip"/>
<result property="country" column="country"/>
</association>
</resultMap> <select id="findAllStudents" resultMap="StudentResult">
select * from Students
</select> <select id="findStudentById" parameterType="int" resultMap="StudentWithAddressNestedSelect">
select * from Students where stud_id=#{studId}
</select> <select id="selectStudentWithAddress" parameterType="int" resultMap="StudentWithAddressNestedResultMap">
select stud_id, name, email,phone, a.addr_id, street, city, state, zip, country
FROM students s left outer join addresses a on s.addr_id=a.addr_id
where stud_id=#{studId}
</select> <insert id="insertStudent" parameterType="Student" useGeneratedKeys="true" keyProperty="studId">
insert into students(name,email,addr_id, phone)
values(#{name},#{email},#{address.addrId},#{phone})
</insert> <insert id="insertStudentWithMap" parameterType="hashmap" useGeneratedKeys="true" keyProperty="studId">
insert into students(name,email,addr_id,phone)
values(#{name},#{email},#{address.addrId},#{phone})
</insert> <update id="updateStudent" parameterType="Student">
update students
<!-- set
name=#{name},
email=#{email},
phone=#{phone}
where stud_id=#{studId} --> <set>
<if test="name != null">name=#{name},</if>
<if test="email != null">email=#{email},</if>
<if test="phone != null">phone=#{phone},</if>
</set>
where stud_id=#{studId}
</update> <delete id="deleteStudent" parameterType="int">
delete from students where stud_id=#{studId}
</delete> </mapper>

(4)TutorMapper.xml

 <?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.mybatis3.mappers.TutorMapper"> <resultMap type="Tutor" id="TutorWithCoursesNestedResult">
<id column="tutor_id" property="tutorId"/>
<result column="tutor_name" property="name"/>
<result column="email" property="email"/>
<association property="address" resultMap="com.mybatis3.mappers.AddressMapper.AddressResult"/>
<collection property="courses" resultMap="com.mybatis3.mappers.CourseMapper.CourseResult" />
</resultMap> <resultMap type="Tutor" id="TutorWithCoursesNestedSelect">
<id column="tutor_id" property="tutorId"/>
<result column="tutor_name" property="name"/>
<result column="email" property="email"/>
<association property="address" resultMap="com.mybatis3.mappers.AddressMapper.AddressResult"/>
<collection property="courses" column="tutor_id" select="com.mybatis3.mappers.CourseMapper.selectCoursesByTutor"/>
</resultMap> <select id="selectTutorById" parameterType="int" resultMap="TutorWithCoursesNestedResult">
SELECT t.tutor_id, t.name as tutor_name, email, a.addr_id, street, city, state, zip, country,
course_id, c.name, description, start_date, end_date
FROM tutors t left outer join addresses a on t.addr_id=a.addr_id
left outer join courses c on t.tutor_id=c.tutor_id
where t.tutor_id=#{tutorId}
</select> <select id="selectTutorWithCourses" parameterType="int" resultMap="TutorWithCoursesNestedSelect">
SELECT t.tutor_id, t.name as tutor_name, email, a.addr_id, street, city, state, zip, country
FROM tutors t left outer join addresses a on t.addr_id=a.addr_id
where t.tutor_id=#{tutorId}
</select> </mapper>

(5)mybatis-config.xml

 <?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="application.properties" /> <typeAliases>
<package name="com.mybatis3.domain" />
</typeAliases> <typeHandlers>
<typeHandler handler="com.mybatis3.typehandlers.PhoneTypeHandler" />
</typeHandlers> <environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</dataSource>
</environment> </environments> <mappers>
<package name="com.mybatis3.mappers"/>
</mappers> </configuration>

6.测试文件

最新文章

  1. &lt;%@ page trimDirectiveWhitespaces=&quot;true&quot; %&gt;
  2. shell--2.shell数组
  3. 互联网+下PDA移动智能手持POS超市收银开单软件
  4. Linux学习笔记(5)Linux常用命令之文件搜索命令
  5. URL Quoting
  6. log4net日志记录
  7. 使用NSURLSession请求需要AD认证的HTTPS服务器
  8. (转)python文件操作 seek(),tell()
  9. from 表单提交
  10. Swift: 基本操作符
  11. C#VS面向对象基础(二)
  12. lua迭代器和仿制药for
  13. SLAM入门之视觉里程计(5):单应矩阵
  14. 证明二叉查找树所有节点的平均深度为O(logN)
  15. servlet篇 之 访问形式
  16. Arcgis for qml - 鼠标拖拽移动
  17. 手机端 https://doc.vux.li/zh-CN/components/badge.html
  18. 为什么要选择OMP之合规性
  19. 【题解】 [HNOI2002]营业额统计 (Splay)
  20. Spring 远程服务

热门文章

  1. New Concept English three (51)
  2. python 生成唯一字符串UUID与MD5
  3. c++引用和指针的实现
  4. php-fpm 和 mysql 之间的关系
  5. thinkphp3.0中ajax的发送
  6. python 获取本机ip地址的方法(Unix 平台)
  7. python 类的定义和继承
  8. JS计算字符串的长度
  9. Oracl使用总结二
  10. [转]200 OK (from cache) 与 304 Not Modified------没有这个规则(ETag是否移除)!!!from cache和304,请查看顶部的流程图!