一、

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. mysql数据库优化。(强力推荐)

    本文转自:https://m.aliyun.com/yunqi/articles/38809 一个成熟的数据库架构并不是一开始设计就具备高可用.高伸缩等特性的,它是随着用户量的增加,基础架构才逐渐完善 ...

  2. asp.net core mcroservices 架构之 分布式日志(一)

    一 简介 无论是微服务还是其他任何分布式系统,都需要一个统一处理日志的系统,这个系统 必须有收集,索引,分析查询的功能.asp .net core自己的日志是同步方式的,正如文档所言: 所以必须自己提 ...

  3. TP中登录验证

    loginpro 1.建立控制器 loginController.calss.php <?php namespace Admin\Controller; header('Content-type ...

  4. oracle如何insert into 多个values

    稍微熟悉Oracle的都知道,如果我们想一条SQL语句向表中插入多个值的话,如果如下语句 INSERT INTO 某表 VALUES(各个值),VALUES(各个值),.....; 这样会报错的,因为 ...

  5. Java 时间工具类

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  1.Calendar 转化 String  ...

  6. SQL Sever 学习系列之三

    SQL Sever 学习系列之三 SQL Server 学习系列之一(薪酬方案+基础) SQL Server 学习系列之二(日期格式问题)         五.经理今天刚谈到with的用法(with的 ...

  7. !heap 和 _HEAP_ENTRY

    WinDBG提供了!heap命令帮助我们查找heap,同时我们也可以通过dt和MS SYMBOL来了解memory layout. 假设我们有下面一个小程序. int _tmain(int argc, ...

  8. 异常:SQL Error: 1064, SQLState: 42000

    在MySQL中,有很多字符被MySQL保留了.如果你用来做列名或者表名就会出现问题. 我这里出现的问题是采用了order作为表明,这是一个保留字,所以出现问题.

  9. Ubuntu 16.04 LTS制作本地源

    平时apt-get install安装软件时,下载的deb文件都会存放在/var/cache/apt/archives/下,没有网络时就需要将这些deb制作成本地源.另外,如果在本机架一个简单的网络服 ...

  10. Django基础(五)

    Django admin 自带的验证: from django.contrib.auth.decorators import login_required from django.contrib.au ...