一、

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.测试文件

JavaPersistenceWithMyBatis3笔记-第3章SQL Mappers Using XMLs-001的更多相关文章

  1. JavaPersistenceWithMyBatis3笔记-第4章SQL Mappers Using Annotations-001

    一. 1.Mapper /** * */ package com.mybatis3.mappers; import org.apache.ibatis.annotations.Select; impo ...

  2. JavaPersistenceWithMyBatis3笔记-第5章Configuring MyBatis in a Spring applications-001

    一. 1.Mapper /** * */ package com.mybatis3.mappers; import java.util.List; import org.apache.ibatis.a ...

  3. JavaPersistenceWithMyBatis3笔记-第2章Bootstrapping MyBatis-001XMl形式和Java形式

    一. 1.Mapper 同上 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper ...

  4. JavaPersistenceWithMyBatis3笔记-第1章-001

    一.介绍 1.项目结构 2.数据库结构 二.代码 1.Mapper package com.mybatis3.mappers; import java.util.List; import com.my ...

  5. SQL Server2012 T-SQL基础教程--读书笔记(1-4章)

    SQL Server2012 T-SQL基础教程--读书笔记(1-4章) SqlServer T-SQL 示例数据库:点我 Chapter 01 T-SQL 查询和编程背景 1.3 创建表和定义数据的 ...

  6. SQL Server2012 T-SQL基础教程--读书笔记(5-7章)

    SQL Server2012 T-SQL基础教程--读书笔记(5-7章) SqlServer T-SQL 示例数据库:点我 Chapter 05 表表达式 5.1 派生表 5.1.1 分配列别名 5. ...

  7. java JDK8 学习笔记——第16章 整合数据库

    第十六章 整合数据库 16.1 JDBC入门 16.1.1 JDBC简介 1.JDBC是java联机数据库的标准规范.它定义了一组标准类与接口,标准API中的接口会有数据库厂商操作,称为JDBC驱动程 ...

  8. Programming Entity Framework-dbContext 学习笔记第五章

    ### Programming Entity Framework-dbContext 学习笔记 第五章 将图表添加到Context中的方式及容易出现的错误 方法 结果 警告 Add Root 图标中的 ...

  9. Stealth视频教程学习笔记(第二章)

    Stealth视频教程学习笔记(第二章) 本文是对Unity官方视频教程Stealth的学习笔记.在此之前,本人整理了Stealth视频的英文字幕,并放到了优酷上.本文将分别对各个视频进行学习总结,提 ...

随机推荐

  1. kylin_学习_02_kylin使用教程

    一. 二.参考资料 1.kylin从入门到实战:实际案例

  2. HihoCoder1465 重复旋律8(后缀自动机)

    描述 小Hi平时的一大兴趣爱好就是演奏钢琴.我们知道一段音乐旋律可以被表示为一段数构成的数列. 小Hi发现旋律可以循环,每次把一段旋律里面最前面一个音换到最后面就成为了原旋律的“循环相似旋律”,还可以 ...

  3. H国的身份证号码(搜索)

    个人心得:巧妙利用数字进行维护就好了,深搜还是有点心得的: #1558 : H国的身份证号码I 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 H国的身份证号码是一个N位 ...

  4. STL容器共性机制和使用场景

    一.STL容器共性机制 STL容器所提供的都是值(value)寓意,而非引用(reference)寓意,也就是说当我们给容器中插入元素的时候,容器内部实施了拷贝动作,将我们要插入的元素再另行拷贝一份放 ...

  5. C#检查网络是否可以连接互联网

    添加引用: using System.Runtime.InteropServices; using System.Net.NetworkInformation; [DllImport("wi ...

  6. 使用Jsonp实现跨域请求

    来自百度百科的一段话: JSONP(JSON with Padding)是JSON的一种“使用模式”,可用于解决主流浏览器的跨域数据访问的问题.由于同源策略,一般来说位于 server1.exampl ...

  7. 利用PowerDesigner设计数据库

    PowerDesigner非常强大, 可以利用它完成数据库的设计. 1.下载地址:http://pan.baidu.com/s/1DsLrg 2.表设计: 建立概念数据模型(Conceptual Da ...

  8. Azure上Linux VM误配防火墙的恢复方法

    在实际运维中,防火墙把自己挡在机器外面的情况会时有发生.如何快速的恢复对运维人员是很重要的. 本文将介绍如何用Azure Extension实现不通过ssh对VM进行操作的方法. 之前写过一遍Blog ...

  9. bash的使用

    转自:http://blog.csdn.net/y2888886/article/details/50535033 在上篇博文的基础上做如下修改 注意一些常见命令中间就要加 “ ” ,否则很多命令无法 ...

  10. 2017-09-17 python 学习笔记

    Mock 库 介绍: http://blog.csdn.net/chdhust/article/details/50663729   说明mock能做什么. 可以考虑在调试方法时使用 Mock 库