JavaPersistenceWithMyBatis3笔记-第3章SQL Mappers Using XMLs-001
一、
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 >= #{startDate}
</if>
<if test="endDate != null">
AND end_date <= #{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的更多相关文章
- JavaPersistenceWithMyBatis3笔记-第4章SQL Mappers Using Annotations-001
一. 1.Mapper /** * */ package com.mybatis3.mappers; import org.apache.ibatis.annotations.Select; impo ...
- JavaPersistenceWithMyBatis3笔记-第5章Configuring MyBatis in a Spring applications-001
一. 1.Mapper /** * */ package com.mybatis3.mappers; import java.util.List; import org.apache.ibatis.a ...
- JavaPersistenceWithMyBatis3笔记-第2章Bootstrapping MyBatis-001XMl形式和Java形式
一. 1.Mapper 同上 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper ...
- JavaPersistenceWithMyBatis3笔记-第1章-001
一.介绍 1.项目结构 2.数据库结构 二.代码 1.Mapper package com.mybatis3.mappers; import java.util.List; import com.my ...
- SQL Server2012 T-SQL基础教程--读书笔记(1-4章)
SQL Server2012 T-SQL基础教程--读书笔记(1-4章) SqlServer T-SQL 示例数据库:点我 Chapter 01 T-SQL 查询和编程背景 1.3 创建表和定义数据的 ...
- SQL Server2012 T-SQL基础教程--读书笔记(5-7章)
SQL Server2012 T-SQL基础教程--读书笔记(5-7章) SqlServer T-SQL 示例数据库:点我 Chapter 05 表表达式 5.1 派生表 5.1.1 分配列别名 5. ...
- java JDK8 学习笔记——第16章 整合数据库
第十六章 整合数据库 16.1 JDBC入门 16.1.1 JDBC简介 1.JDBC是java联机数据库的标准规范.它定义了一组标准类与接口,标准API中的接口会有数据库厂商操作,称为JDBC驱动程 ...
- Programming Entity Framework-dbContext 学习笔记第五章
### Programming Entity Framework-dbContext 学习笔记 第五章 将图表添加到Context中的方式及容易出现的错误 方法 结果 警告 Add Root 图标中的 ...
- Stealth视频教程学习笔记(第二章)
Stealth视频教程学习笔记(第二章) 本文是对Unity官方视频教程Stealth的学习笔记.在此之前,本人整理了Stealth视频的英文字幕,并放到了优酷上.本文将分别对各个视频进行学习总结,提 ...
随机推荐
- jquery判断密码是否一致?
密码 请输入密码 重新输入密码 请输入新密码 <input type="text" id="btn0"> 密码 <span class=&qu ...
- Java 接口与继承 道至简第六章发表阅读笔记
一.继承条件下的构造方法调用 class Grandparent { public Grandparent() { System.out.println("GrandParent Creat ...
- Nodejs-RESTFul架构
请求方法 一般会严格要求请求方法及其释义,下面给出常用的请求方法 如果请求头中存在 X-HTTP-Method-Override 或参数中存在 _method(拥有更高权重),且值为 GET, POS ...
- linux进程学习-创建新进程
init进程将系统启动后,init将成为此后所有进程的祖先,此后的进程都是直接或间接从init进程“复制”而来.完成该“复制”功能的函数有fork()和clone()等. 一个进程(父进程)调用for ...
- 树莓派(Arduino)仿真软件 —— Fritzing
Fritzing 官网:Fritzing Fritzing 下载地址:Fritzing Download windows 下降 zip 文件解压后,免安装双击 exe 即可运行:
- Android源代码因删除所有git仓库导致的编译错误
/******************************************************************************** * Android源代码因删除所有g ...
- RedHat 6.8 内核编译
/*************************************************************************** * RedHat 6.8 内核编译 * 说明: ...
- session 丢失问题
1. 存到memcached中, 十分简单, 在使用session之前, 加入下面两行代码 int_set('session.save_handler', 'memcache'); int_set(' ...
- LeetCode 362. Design Hit Counter
原题链接在这里:https://leetcode.com/problems/design-hit-counter/description/ 题目: Design a hit counter which ...
- 【转】深入剖析Java中的装箱和拆箱
深入剖析Java中的装箱和拆箱 自动装箱和拆箱问题是Java中一个老生常谈的问题了,今天我们就来一些看一下装箱和拆箱中的若干问题.本文先讲述装箱和拆箱最基本的东西,再来看一下面试笔试中经常遇到的与装箱 ...