序
三种对象关系映射ObjectRelationshipMapping
- JDBC(数据库操作)
student | t_student |
---|---|
studentId | t_id |
- Mybatis(半自动的ORM)
Teacher | XML/@ teacher–SQL |
---|---|
teacherId | teacher_id |
- Hibernate(完全ORM)
Teacher | teacher |
---|---|
teacher.setName()------- | ------>update… |
导语
本文是讨论:基于Mybatis对数据库多表的增删查改
Mybatis多表参考:链接1 | 链接2 | 链接3
mapper一对多解析参考:链接1 | 链接2
-
一对一
Person IdCard
-
一对多
学生(多) 班级(一)
类中:
查询:
左连接:(注意,在一端尽量用左外连接查询)
-
多对多
employee(多)--------------------------------project(多)
EmpDao.xml中的配置:类比ClazzDao.xml
ProjectDao.xml中的配置:类比ClazzDao.xml
EmpDoProjectRecordDao.xml中的配置:类比StudentDao.xml(不同的在于,EmpDoProjectRecordDao.xml中的中应该有两个)
正文
(1)一对一
- 表:
- mapper:
- 数据库模拟-查询idcard_id为1:
- 详解:实体类+Dao+Dao.xml
IdCard.java:
package com.cungudafa.spingmvc01.bean.one2one;
public class IdCard {
private Integer idCardId;
private String idCardNumber;
/**
* 身份证属于某个人
*/
private Person person;
public Integer getIdCardId() {
return idCardId;
}
public void setIdCardId(Integer idCardId) {
this.idCardId = idCardId;
}
public String getIdCardNumber() {
return idCardNumber;
}
public void setIdCardNumber(String idCardNumber) {
this.idCardNumber = idCardNumber;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}
Person.java:
package com.cungudafa.spingmvc01.bean.one2one;
public class Person {
private Integer personId;
private String personName;
public Integer getPersonId() {
return personId;
}
public void setPersonId(Integer personId) {
this.personId = personId;
}
public String getPersonName() {
return personName;
}
public void setPersonName(String personName) {
this.personName = personName;
}
}
IdCardDao.java:
package com.cungudafa.spingmvc01.dao.one2one;
import com.cungudafa.spingmvc01.bean.one2one.IdCard;
public interface IdCardDao {
public IdCard getIdCardById(IdCard idCard);
}
IdCardDao.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.cungudafa.spingmvc01.dao.one2one.IdCardDao">
<resultMap type="IdCard" id="imap">
<id column="idcard_id" property="idCardId"/>
<result column="idcard_number" property="idCardNumber"/>
<association property="person" javaType="Person">
<id column="person_id" property="personId"/>
<result column="person_name" property="personName"/>
</association>
</resultMap>
<select id="getIdCardById" parameterType="IdCard" resultMap="imap">
select * from idcard t1,person t2
where t1.person_id = t2.person_id and t1.idcard_id = #{idCardId}
</select>
</mapper>
- junit测试:
IdCardDaoTest.java:
package com.cungudafa.spingmvc01.dao.one2one;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.cungudafa.spingmvc01.bean.one2one.IdCard;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:config/application-*.xml")
public class IdCardDaoTest {
@Autowired
private IdCardDao dao;
@Test
public void test1(){
IdCard idCard = new IdCard();
idCard.setIdCardId(1);//查询idcard为1的人的姓名
idCard = dao.getIdCardById(idCard);
System.out.println("身份证号 = "+idCard.getIdCardNumber());
System.out.println("idCard.getPerson().getPersonId() = " + idCard.getPerson().getPersonId());;
System.out.println("该身份证属于 = " + idCard.getPerson().getPersonName());
}
}
(2)一对多
- 表:
- mapper:
- 数据库模拟-查询clazz_id为1601:
左连接查询:全部学生和相应的班级信息:
- 详解:实体类+Dao+Dao.xml
Student.java:
package com.cungudafa.spingmvc01.bean.one2many;
public class Student {
private Integer studentId;
private String studentName;
/**
* 一个学生属于一个班级
*/
private Clazz clazz;
public Integer getStudentId() {
return studentId;
}
public void setStudentId(Integer studentId) {
this.studentId = studentId;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public Clazz getClazz() {
return clazz;
}
public void setClazz(Clazz clazz) {
this.clazz = clazz;
}
}
Clazz.java:
package com.cungudafa.spingmvc01.bean.one2many;
import java.util.Set;
public class Clazz {
private Integer clazzId;
private String clazzName;
/**
* 一个班级下有多个学生
*/
private Set<Student> students;
public Integer getClazzId() {
return clazzId;
}
public void setClazzId(Integer clazzId) {
this.clazzId = clazzId;
}
public String getClazzName() {
return clazzName;
}
public void setClazzName(String clazzName) {
this.clazzName = clazzName;
}
public Set<Student> getStudents() {
return students;
}
public void setStudents(Set<Student> students) {
this.students = students;
}
}
StudentDao.java:
package com.cungudafa.spingmvc01.dao.one2many;
import java.util.List;
import com.cungudafa.spingmvc01.bean.one2many.Student;
public interface StudentDao {
public List<Student> getStudentList();
}
ClassDao.java:
package com.cungudafa.spingmvc01.dao.one2many;
import java.util.List;
import com.cungudafa.spingmvc01.bean.one2many.Clazz;
public interface ClazzDao {
public Clazz getClazzById(Clazz clazz);
public List<Clazz> getClazzes();
}
StudentDao.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.cungudafa.spingmvc01.dao.one2many.StudentDao">
<resultMap type="Student" id="smap">
<id column="student_id" property="studentId"/>
<result column="student_name" property="studentName"/>
<association property="clazz" javaType="Clazz">
<id column="clazz_id" property="clazzId"/>
<result column="clazz_name" property="clazzName"/>
</association>
</resultMap>
<select id="getStudentList" parameterType="Student" resultMap="smap">
select * from student t1,clazz t2 where t1.clazz_id = t2.clazz_id
</select>
</mapper>
ClazzDao.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.cungudafa.spingmvc01.dao.one2many.ClazzDao">
<resultMap type="Clazz" id="cmap">
<id column="clazz_id" property="clazzId"/>
<result column="clazz_name" property="clazzName"/>
<collection property="students" ofType="Student">
<id column="student_id" property="studentId"/>
<result column="student_name" property="studentName"/>
</collection>
</resultMap>
<select id="getClazzById" parameterType="Clazz" resultMap="cmap">
select * from clazz t1,student t2
where t1.clazz_id = t2.clazz_id and t1.clazz_id = #{clazzId}
</select>
<select id="getClazzes" resultMap="cmap">
select * from clazz t1
left outer join student t2
on t1.clazz_id = t2.clazz_id
</select>
</mapper>
- junit测试:
StudentDaoTest.java:(一学生对应一班级,还算一对一,略过)
package com.cungudafa.spingmvc01.dao.one2many;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.cungudafa.spingmvc01.bean.one2many.Student;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:config/application-*.xml")
public class StudentDaoTest {
@Autowired
private StudentDao dao;
@Test
public void test1(){
List<Student> students = dao.getStudentList();
for(Student s : students){
System.out.println("s.name = " + s.getStudentName());
System.out.println("s.clazz.name = " + s.getClazz().getClazzName());
}
}
}
StudentDaoTest.java:(一班级对应多个学生)
package com.cungudafa.spingmvc01.dao.one2many;
import java.util.List;
import java.util.Set;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.cungudafa.spingmvc01.bean.one2many.Clazz;
import com.cungudafa.spingmvc01.bean.one2many.Student;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:config/application-*.xml")
public class ClazzDaoTest {
@Autowired
private ClazzDao dao;
@Test
public void test1(){
Clazz clazz = new Clazz();
clazz.setClazzId(1601);//按班级号查找全部学生姓名(1对多)
clazz = dao.getClazzById(clazz);
System.out.println("clazz.name = " + clazz.getClazzName());
Set<Student> students = clazz.getStudents();
for(Student s : students){
System.out.println("s.name = " + s.getStudentName());
}
}
@Test
public void test2(){
List<Clazz> clazzes = dao.getClazzes();//按班级号依次查找
for(Clazz clazz : clazzes){
System.out.println("----------------");
System.out.println("clazz.name = " + clazz.getClazzName());
Set<Student> students = clazz.getStudents();
for(Student s : students){
System.out.println("s.name = " + s.getStudentName());
}
System.out.println("----------------");
}
}
}
(3)多对多
(比较复杂,全部源码在文末)
图中分析按顾客预订公寓模型分析,代码逻辑是员工与项目模型的多对多关系
- Junit测试:
package com.cungudafa.spingmvc01.dao;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.cungudafa.spingmvc01.bean.Department;
import com.cungudafa.spingmvc01.bean.DepartmentExample;
import com.cungudafa.spingmvc01.bean.DepartmentExample.Criteria;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:config/application-*.xml")
public class DepartmentMapperTest {
@Autowired
private DepartmentMapper dao;
@Test
public void test1(){//增
Department d = new Department();
d.setDepartmentName("行政部");
dao.insert(d);//插入一个行政部
}
@Test
public void test2(){//改
Department d = dao.selectByPrimaryKey(1);//把department_id为1的部门:行政部改为-》销售部
d.setDepartmentName("销售部");
dao.updateByPrimaryKey(d);
}
@Test
public void test3(){//删
dao.deleteByPrimaryKey(3);
}
@Test
public void test4(){//查
Department d = dao.selectByPrimaryKey(1);//id为1的部门
System.out.println("查询部门号为1的d.name = " + d.getDepartmentName());
}
@Test
public void test5(){//模糊查询所有部
DepartmentExample example = new DepartmentExample();
Criteria c = example.createCriteria();
// c.andDepartmentNameLike("%部%");
c.andDepartmentIdLessThan(5).andDepartmentNameLike("%部%");
List<Department> ds = dao.selectByExample(example);
for(Department d : ds){
System.out.println("查询d.name = " + d.getDepartmentName());
}
}
@Test
public void test6(){
DepartmentExample example = new DepartmentExample();
Criteria c = example.createCriteria();
c.andDepartmentIdGreaterThanOrEqualTo(4);//删除4?
dao.deleteByExample(example);
}
}
更新、查询:
多对多的DAO.xml比较复杂:
由 Mybatis反向自动生成工具自动生成 (这部分在另一文详述,本文为实例说明)
全部源码:
Employee.java:
package com.cungudafa.spingmvc01.bean;
public class Employee {
private Integer employeeId;
private String employeeName;
private Integer departmentId;
public Integer getEmployeeId() {
return employeeId;
}
public void setEmployeeId(Integer employeeId) {
this.employeeId = employeeId;
}
public String getEmployeeName() {
return employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName == null ? null : employeeName.trim();
}
public Integer getDepartmentId() {
return departmentId;
}
public void setDepartmentId(Integer departmentId) {
this.departmentId = departmentId;
}
}
Department.java:
package com.cungudafa.spingmvc01.bean;
public class Department {
private Integer departmentId;
private String departmentName;
public Integer getDepartmentId() {
return departmentId;
}
public void setDepartmentId(Integer departmentId) {
this.departmentId = departmentId;
}
public String getDepartmentName() {
return departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName == null ? null : departmentName.trim();
}
}
EmployeeExample.java:
package com.cungudafa.spingmvc01.bean;
import java.util.ArrayList;
import java.util.List;
public class EmployeeExample {
protected String orderByClause;
protected boolean distinct;
protected List<Criteria> oredCriteria;
public EmployeeExample() {
oredCriteria = new ArrayList<Criteria>();
}
public void setOrderByClause(String orderByClause) {
this.orderByClause = orderByClause;
}
public String getOrderByClause() {
return orderByClause;
}
public void setDistinct(boolean distinct) {
this.distinct = distinct;
}
public boolean isDistinct() {
return distinct;
}
public List<Criteria> getOredCriteria() {
return oredCriteria;
}
public void or(Criteria criteria) {
oredCriteria.add(criteria);
}
public Criteria or() {
Criteria criteria = createCriteriaInternal();
oredCriteria.add(criteria);
return criteria;
}
public Criteria createCriteria() {
Criteria criteria = createCriteriaInternal();
if (oredCriteria.size() == 0) {
oredCriteria.add(criteria);
}
return criteria;
}
protected Criteria createCriteriaInternal() {
Criteria criteria = new Criteria();
return criteria;
}
public void clear() {
oredCriteria.clear();
orderByClause = null;
distinct = false;
}
protected abstract static class GeneratedCriteria {
protected List<Criterion> criteria;
protected GeneratedCriteria() {
super();
criteria = new ArrayList<Criterion>();
}
public boolean isValid() {
return criteria.size() > 0;
}
public List<Criterion> getAllCriteria() {
return criteria;
}
public List<Criterion> getCriteria() {
return criteria;
}
protected void addCriterion(String condition) {
if (condition == null) {
throw new RuntimeException("Value for condition cannot be null");
}
criteria.add(new Criterion(condition));
}
protected void addCriterion(String condition, Object value, String property) {
if (value == null) {
throw new RuntimeException("Value for " + property + " cannot be null");
}
criteria.add(new Criterion(condition, value));
}
protected void addCriterion(String condition, Object value1, Object value2, String property) {
if (value1 == null || value2 == null) {
throw new RuntimeException("Between values for " + property + " cannot be null");
}
criteria.add(new Criterion(condition, value1, value2));
}
public Criteria andEmployeeIdIsNull() {
addCriterion("employee_id is null");
return (Criteria) this;
}
public Criteria andEmployeeIdIsNotNull() {
addCriterion("employee_id is not null");
return (Criteria) this;
}
public Criteria andEmployeeIdEqualTo(Integer value) {
addCriterion("employee_id =", value, "employeeId");
return (Criteria) this;
}
public Criteria andEmployeeIdNotEqualTo(Integer value) {
addCriterion("employee_id <>", value, "employeeId");
return (Criteria) this;
}
public Criteria andEmployeeIdGreaterThan(Integer value) {
addCriterion("employee_id >", value, "employeeId");
return (Criteria) this;
}
public Criteria andEmployeeIdGreaterThanOrEqualTo(Integer value) {
addCriterion("employee_id >=", value, "employeeId");
return (Criteria) this;
}
public Criteria andEmployeeIdLessThan(Integer value) {
addCriterion("employee_id <", value, "employeeId");
return (Criteria) this;
}
public Criteria andEmployeeIdLessThanOrEqualTo(Integer value) {
addCriterion("employee_id <=", value, "employeeId");
return (Criteria) this;
}
public Criteria andEmployeeIdIn(List<Integer> values) {
addCriterion("employee_id in", values, "employeeId");
return (Criteria) this;
}
public Criteria andEmployeeIdNotIn(List<Integer> values) {
addCriterion("employee_id not in", values, "employeeId");
return (Criteria) this;
}
public Criteria andEmployeeIdBetween(Integer value1, Integer value2) {
addCriterion("employee_id between", value1, value2, "employeeId");
return (Criteria) this;
}
public Criteria andEmployeeIdNotBetween(Integer value1, Integer value2) {
addCriterion("employee_id not between", value1, value2, "employeeId");
return (Criteria) this;
}
public Criteria andEmployeeNameIsNull() {
addCriterion("employee_name is null");
return (Criteria) this;
}
public Criteria andEmployeeNameIsNotNull() {
addCriterion("employee_name is not null");
return (Criteria) this;
}
public Criteria andEmployeeNameEqualTo(String value) {
addCriterion("employee_name =", value, "employeeName");
return (Criteria) this;
}
public Criteria andEmployeeNameNotEqualTo(String value) {
addCriterion("employee_name <>", value, "employeeName");
return (Criteria) this;
}
public Criteria andEmployeeNameGreaterThan(String value) {
addCriterion("employee_name >", value, "employeeName");
return (Criteria) this;
}
public Criteria andEmployeeNameGreaterThanOrEqualTo(String value) {
addCriterion("employee_name >=", value, "employeeName");
return (Criteria) this;
}
public Criteria andEmployeeNameLessThan(String value) {
addCriterion("employee_name <", value, "employeeName");
return (Criteria) this;
}
public Criteria andEmployeeNameLessThanOrEqualTo(String value) {
addCriterion("employee_name <=", value, "employeeName");
return (Criteria) this;
}
public Criteria andEmployeeNameLike(String value) {
addCriterion("employee_name like", value, "employeeName");
return (Criteria) this;
}
public Criteria andEmployeeNameNotLike(String value) {
addCriterion("employee_name not like", value, "employeeName");
return (Criteria) this;
}
public Criteria andEmployeeNameIn(List<String> values) {
addCriterion("employee_name in", values, "employeeName");
return (Criteria) this;
}
public Criteria andEmployeeNameNotIn(List<String> values) {
addCriterion("employee_name not in", values, "employeeName");
return (Criteria) this;
}
public Criteria andEmployeeNameBetween(String value1, String value2) {
addCriterion("employee_name between", value1, value2, "employeeName");
return (Criteria) this;
}
public Criteria andEmployeeNameNotBetween(String value1, String value2) {
addCriterion("employee_name not between", value1, value2, "employeeName");
return (Criteria) this;
}
public Criteria andDepartmentIdIsNull() {
addCriterion("department_id is null");
return (Criteria) this;
}
public Criteria andDepartmentIdIsNotNull() {
addCriterion("department_id is not null");
return (Criteria) this;
}
public Criteria andDepartmentIdEqualTo(Integer value) {
addCriterion("department_id =", value, "departmentId");
return (Criteria) this;
}
public Criteria andDepartmentIdNotEqualTo(Integer value) {
addCriterion("department_id <>", value, "departmentId");
return (Criteria) this;
}
public Criteria andDepartmentIdGreaterThan(Integer value) {
addCriterion("department_id >", value, "departmentId");
return (Criteria) this;
}
public Criteria andDepartmentIdGreaterThanOrEqualTo(Integer value) {
addCriterion("department_id >=", value, "departmentId");
return (Criteria) this;
}
public Criteria andDepartmentIdLessThan(Integer value) {
addCriterion("department_id <", value, "departmentId");
return (Criteria) this;
}
public Criteria andDepartmentIdLessThanOrEqualTo(Integer value) {
addCriterion("department_id <=", value, "departmentId");
return (Criteria) this;
}
public Criteria andDepartmentIdIn(List<Integer> values) {
addCriterion("department_id in", values, "departmentId");
return (Criteria) this;
}
public Criteria andDepartmentIdNotIn(List<Integer> values) {
addCriterion("department_id not in", values, "departmentId");
return (Criteria) this;
}
public Criteria andDepartmentIdBetween(Integer value1, Integer value2) {
addCriterion("department_id between", value1, value2, "departmentId");
return (Criteria) this;
}
public Criteria andDepartmentIdNotBetween(Integer value1, Integer value2) {
addCriterion("department_id not between", value1, value2, "departmentId");
return (Criteria) this;
}
}
public static class Criteria extends GeneratedCriteria {
protected Criteria() {
super();
}
}
public static class Criterion {
private String condition;
private Object value;
private Object secondValue;
private boolean noValue;
private boolean singleValue;
private boolean betweenValue;
private boolean listValue;
private String typeHandler;
public String getCondition() {
return condition;
}
public Object getValue() {
return value;
}
public Object getSecondValue() {
return secondValue;
}
public boolean isNoValue() {
return noValue;
}
public boolean isSingleValue() {
return singleValue;
}
public boolean isBetweenValue() {
return betweenValue;
}
public boolean isListValue() {
return listValue;
}
public String getTypeHandler() {
return typeHandler;
}
protected Criterion(String condition) {
super();
this.condition = condition;
this.typeHandler = null;
this.noValue = true;
}
protected Criterion(String condition, Object value, String typeHandler) {
super();
this.condition = condition;
this.value = value;
this.typeHandler = typeHandler;
if (value instanceof List<?>) {
this.listValue = true;
} else {
this.singleValue = true;
}
}
protected Criterion(String condition, Object value) {
this(condition, value, null);
}
protected Criterion(String condition, Object value, Object secondValue, String typeHandler) {
super();
this.condition = condition;
this.value = value;
this.secondValue = secondValue;
this.typeHandler = typeHandler;
this.betweenValue = true;
}
protected Criterion(String condition, Object value, Object secondValue) {
this(condition, value, secondValue, null);
}
}
}
DepartmentExample.java:
package com.cungudafa.spingmvc01.bean;
import java.util.ArrayList;
import java.util.List;
public class DepartmentExample {
protected String orderByClause;
protected boolean distinct;
protected List<Criteria> oredCriteria;
//一点新建一个DepartmentExample 就会new 一个oredCriteria List
public DepartmentExample() {
oredCriteria = new ArrayList<Criteria>();
}
public void setOrderByClause(String orderByClause) {
this.orderByClause = orderByClause;
}
public String getOrderByClause() {
return orderByClause;
}
public void setDistinct(boolean distinct) {
this.distinct = distinct;
}
public boolean isDistinct() {
return distinct;
}
public List<Criteria> getOredCriteria() {
return oredCriteria;
}
//证明使用or连接的
public void or(Criteria criteria) {
oredCriteria.add(criteria);
}
public Criteria or() {
Criteria criteria = createCriteriaInternal();
oredCriteria.add(criteria);
return criteria;
}
public Criteria createCriteria() {
Criteria criteria = createCriteriaInternal();
if (oredCriteria.size() == 0) {
oredCriteria.add(criteria);
}
return criteria;
}
protected Criteria createCriteriaInternal() {
Criteria criteria = new Criteria();
return criteria;
}
public void clear() {
oredCriteria.clear();
orderByClause = null;
distinct = false;
}
protected abstract static class GeneratedCriteria {
protected List<Criterion> criteria;
protected GeneratedCriteria() {
super();
criteria = new ArrayList<Criterion>();
}
public boolean isValid() {
return criteria.size() > 0;
}
public List<Criterion> getAllCriteria() {
return criteria;
}
public List<Criterion> getCriteria() {
return criteria;
}
protected void addCriterion(String condition) {
if (condition == null) {
throw new RuntimeException("Value for condition cannot be null");
}
criteria.add(new Criterion(condition));
}
protected void addCriterion(String condition, Object value, String property) {
if (value == null) {
throw new RuntimeException("Value for " + property + " cannot be null");
}
criteria.add(new Criterion(condition, value));
}
protected void addCriterion(String condition, Object value1, Object value2, String property) {
if (value1 == null || value2 == null) {
throw new RuntimeException("Between values for " + property + " cannot be null");
}
criteria.add(new Criterion(condition, value1, value2));
}
public Criteria andDepartmentIdIsNull() {
addCriterion("department_id is null");
return (Criteria) this;
}
public Criteria andDepartmentIdIsNotNull() {
addCriterion("department_id is not null");
return (Criteria) this;
}
public Criteria andDepartmentIdEqualTo(Integer value) {
addCriterion("department_id =", value, "departmentId");
return (Criteria) this;
}
public Criteria andDepartmentIdNotEqualTo(Integer value) {
addCriterion("department_id <>", value, "departmentId");
return (Criteria) this;
}
public Criteria andDepartmentIdGreaterThan(Integer value) {
addCriterion("department_id >", value, "departmentId");
return (Criteria) this;
}
public Criteria andDepartmentIdGreaterThanOrEqualTo(Integer value) {
addCriterion("department_id >=", value, "departmentId");
return (Criteria) this;
}
public Criteria andDepartmentIdLessThan(Integer value) {
addCriterion("department_id <", value, "departmentId");
return (Criteria) this;
}
public Criteria andDepartmentIdLessThanOrEqualTo(Integer value) {
addCriterion("department_id <=", value, "departmentId");
return (Criteria) this;
}
public Criteria andDepartmentIdIn(List<Integer> values) {
addCriterion("department_id in", values, "departmentId");
return (Criteria) this;
}
public Criteria andDepartmentIdNotIn(List<Integer> values) {
addCriterion("department_id not in", values, "departmentId");
return (Criteria) this;
}
public Criteria andDepartmentIdBetween(Integer value1, Integer value2) {
addCriterion("department_id between", value1, value2, "departmentId");
return (Criteria) this;
}
public Criteria andDepartmentIdNotBetween(Integer value1, Integer value2) {
addCriterion("department_id not between", value1, value2, "departmentId");
return (Criteria) this;
}
public Criteria andDepartmentNameIsNull() {
addCriterion("department_name is null");
return (Criteria) this;
}
public Criteria andDepartmentNameIsNotNull() {
addCriterion("department_name is not null");
return (Criteria) this;
}
public Criteria andDepartmentNameEqualTo(String value) {
addCriterion("department_name =", value, "departmentName");
return (Criteria) this;
}
public Criteria andDepartmentNameNotEqualTo(String value) {
addCriterion("department_name <>", value, "departmentName");
return (Criteria) this;
}
public Criteria andDepartmentNameGreaterThan(String value) {
addCriterion("department_name >", value, "departmentName");
return (Criteria) this;
}
public Criteria andDepartmentNameGreaterThanOrEqualTo(String value) {
addCriterion("department_name >=", value, "departmentName");
return (Criteria) this;
}
public Criteria andDepartmentNameLessThan(String value) {
addCriterion("department_name <", value, "departmentName");
return (Criteria) this;
}
public Criteria andDepartmentNameLessThanOrEqualTo(String value) {
addCriterion("department_name <=", value, "departmentName");
return (Criteria) this;
}
public Criteria andDepartmentNameLike(String value) {
addCriterion("department_name like", value, "departmentName");
return (Criteria) this;
}
public Criteria andDepartmentNameNotLike(String value) {
addCriterion("department_name not like", value, "departmentName");
return (Criteria) this;
}
public Criteria andDepartmentNameIn(List<String> values) {
addCriterion("department_name in", values, "departmentName");
return (Criteria) this;
}
public Criteria andDepartmentNameNotIn(List<String> values) {
addCriterion("department_name not in", values, "departmentName");
return (Criteria) this;
}
public Criteria andDepartmentNameBetween(String value1, String value2) {
addCriterion("department_name between", value1, value2, "departmentName");
return (Criteria) this;
}
public Criteria andDepartmentNameNotBetween(String value1, String value2) {
addCriterion("department_name not between", value1, value2, "departmentName");
return (Criteria) this;
}
}
public static class Criteria extends GeneratedCriteria {
protected Criteria() {
super();
}
}
public static class Criterion {
private String condition;
private Object value;
private Object secondValue;
private boolean noValue;
private boolean singleValue;
private boolean betweenValue;
private boolean listValue;
private String typeHandler;
public String getCondition() {
return condition;
}
public Object getValue() {
return value;
}
public Object getSecondValue() {
return secondValue;
}
public boolean isNoValue() {
return noValue;
}
public boolean isSingleValue() {
return singleValue;
}
public boolean isBetweenValue() {
return betweenValue;
}
public boolean isListValue() {
return listValue;
}
public String getTypeHandler() {
return typeHandler;
}
protected Criterion(String condition) {
super();
this.condition = condition;
this.typeHandler = null;
this.noValue = true;
}
protected Criterion(String condition, Object value, String typeHandler) {
super();
this.condition = condition;
this.value = value;
this.typeHandler = typeHandler;
if (value instanceof List<?>) {
this.listValue = true;
} else {
this.singleValue = true;
}
}
protected Criterion(String condition, Object value) {
this(condition, value, null);
}
protected Criterion(String condition, Object value, Object secondValue, String typeHandler) {
super();
this.condition = condition;
this.value = value;
this.secondValue = secondValue;
this.typeHandler = typeHandler;
this.betweenValue = true;
}
protected Criterion(String condition, Object value, Object secondValue) {
this(condition, value, secondValue, null);
}
}
}
EmployeeMapper.java:
package com.cungudafa.spingmvc01.dao;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.cungudafa.spingmvc01.bean.Employee;
import com.cungudafa.spingmvc01.bean.EmployeeExample;
public interface EmployeeMapper {
long countByExample(EmployeeExample example);
int deleteByExample(EmployeeExample example);
int deleteByPrimaryKey(Integer employeeId);
int insert(Employee record);
int insertSelective(Employee record);
List<Employee> selectByExample(EmployeeExample example);
Employee selectByPrimaryKey(Integer employeeId);
int updateByExampleSelective(@Param("record") Employee record, @Param("example") EmployeeExample example);
int updateByExample(@Param("record") Employee record, @Param("example") EmployeeExample example);
int updateByPrimaryKeySelective(Employee record);
int updateByPrimaryKey(Employee record);
}
DepartmentMapper.java:
package com.cungudafa.spingmvc01.dao;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.cungudafa.spingmvc01.bean.Department;
import com.cungudafa.spingmvc01.bean.DepartmentExample;
public interface DepartmentMapper {
long countByExample(DepartmentExample example);
int deleteByExample(DepartmentExample example);
int deleteByPrimaryKey(Integer departmentId);
int insert(Department record);
int insertSelective(Department record);
List<Department> selectByExample(DepartmentExample example);
Department selectByPrimaryKey(Integer departmentId);
int updateByExampleSelective(@Param("record") Department record, @Param("example") DepartmentExample example);
int updateByExample(@Param("record") Department record, @Param("example") DepartmentExample example);
int updateByPrimaryKeySelective(Department record);
int updateByPrimaryKey(Department record);
}
EmployeeMapper.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.cungudafa.spingmvc01.dao.EmployeeMapper">
<resultMap id="BaseResultMap" type="com.cungudafa.spingmvc01.bean.Employee">
<id column="employee_id" jdbcType="INTEGER" property="employeeId" />
<result column="employee_name" jdbcType="VARCHAR" property="employeeName" />
<result column="department_id" jdbcType="INTEGER" property="departmentId" />
</resultMap>
<sql id="Example_Where_Clause">
<where>
<foreach collection="oredCriteria" item="criteria" separator="or">
<if test="criteria.valid">
<trim prefix="(" prefixOverrides="and" suffix=")">
<foreach collection="criteria.criteria" item="criterion">
<choose>
<when test="criterion.noValue">
and ${criterion.condition}
</when>
<when test="criterion.singleValue">
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue">
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue">
and ${criterion.condition}
<foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Update_By_Example_Where_Clause">
<where>
<foreach collection="example.oredCriteria" item="criteria" separator="or">
<if test="criteria.valid">
<trim prefix="(" prefixOverrides="and" suffix=")">
<foreach collection="criteria.criteria" item="criterion">
<choose>
<when test="criterion.noValue">
and ${criterion.condition}
</when>
<when test="criterion.singleValue">
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue">
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue">
and ${criterion.condition}
<foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Base_Column_List">
employee_id, employee_name, department_id
</sql>
<select id="selectByExample" parameterType="com.cungudafa.spingmvc01.bean.EmployeeExample" resultMap="BaseResultMap">
select
<if test="distinct">
distinct
</if>
'false' as QUERYID,
<include refid="Base_Column_List" />
from employee
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null">
order by ${orderByClause}
</if>
</select>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from employee
where employee_id = #{employeeId,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from employee
where employee_id = #{employeeId,jdbcType=INTEGER}
</delete>
<delete id="deleteByExample" parameterType="com.cungudafa.spingmvc01.bean.EmployeeExample">
delete from employee
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
</delete>
<insert id="insert" parameterType="com.cungudafa.spingmvc01.bean.Employee">
insert into employee (employee_id, employee_name, department_id
)
values (#{employeeId,jdbcType=INTEGER}, #{employeeName,jdbcType=VARCHAR}, #{departmentId,jdbcType=INTEGER}
)
</insert>
<insert id="insertSelective" parameterType="com.cungudafa.spingmvc01.bean.Employee">
insert into employee
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="employeeId != null">
employee_id,
</if>
<if test="employeeName != null">
employee_name,
</if>
<if test="departmentId != null">
department_id,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="employeeId != null">
#{employeeId,jdbcType=INTEGER},
</if>
<if test="employeeName != null">
#{employeeName,jdbcType=VARCHAR},
</if>
<if test="departmentId != null">
#{departmentId,jdbcType=INTEGER},
</if>
</trim>
</insert>
<select id="countByExample" parameterType="com.cungudafa.spingmvc01.bean.EmployeeExample" resultType="java.lang.Long">
select count(*) from employee
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
</select>
<update id="updateByExampleSelective" parameterType="map">
update employee
<set>
<if test="record.employeeId != null">
employee_id = #{record.employeeId,jdbcType=INTEGER},
</if>
<if test="record.employeeName != null">
employee_name = #{record.employeeName,jdbcType=VARCHAR},
</if>
<if test="record.departmentId != null">
department_id = #{record.departmentId,jdbcType=INTEGER},
</if>
</set>
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByExample" parameterType="map">
update employee
set employee_id = #{record.employeeId,jdbcType=INTEGER},
employee_name = #{record.employeeName,jdbcType=VARCHAR},
department_id = #{record.departmentId,jdbcType=INTEGER}
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByPrimaryKeySelective" parameterType="com.cungudafa.spingmvc01.bean.Employee">
update employee
<set>
<if test="employeeName != null">
employee_name = #{employeeName,jdbcType=VARCHAR},
</if>
<if test="departmentId != null">
department_id = #{departmentId,jdbcType=INTEGER},
</if>
</set>
where employee_id = #{employeeId,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.cungudafa.spingmvc01.bean.Employee">
update employee
set employee_name = #{employeeName,jdbcType=VARCHAR},
department_id = #{departmentId,jdbcType=INTEGER}
where employee_id = #{employeeId,jdbcType=INTEGER}
</update>
</mapper>
DepartmentMapper.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.cungudafa.spingmvc01.dao.DepartmentMapper">
<resultMap id="BaseResultMap" type="com.cungudafa.spingmvc01.bean.Department">
<id column="department_id" jdbcType="INTEGER" property="departmentId" />
<result column="department_name" jdbcType="VARCHAR" property="departmentName" />
</resultMap>
<sql id="Example_Where_Clause">
<where>
<foreach collection="oredCriteria" item="criteria" separator="or">
<if test="criteria.valid">
<trim prefix="(" prefixOverrides="and" suffix=")">
<foreach collection="criteria.criteria" item="criterion">
<choose>
<when test="criterion.noValue">
and ${criterion.condition}
</when>
<when test="criterion.singleValue">
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue">
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue">
and ${criterion.condition}
<foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Update_By_Example_Where_Clause">
<where>
<foreach collection="example.oredCriteria" item="criteria" separator="or">
<if test="criteria.valid">
<trim prefix="(" prefixOverrides="and" suffix=")">
<foreach collection="criteria.criteria" item="criterion">
<choose>
<when test="criterion.noValue">
and ${criterion.condition}
</when>
<when test="criterion.singleValue">
and ${criterion.condition} #{criterion.value}
</when>
<when test="criterion.betweenValue">
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
</when>
<when test="criterion.listValue">
and ${criterion.condition}
<foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
#{listItem}
</foreach>
</when>
</choose>
</foreach>
</trim>
</if>
</foreach>
</where>
</sql>
<sql id="Base_Column_List">
department_id, department_name
</sql>
<select id="selectByExample" parameterType="com.cungudafa.spingmvc01.bean.DepartmentExample" resultMap="BaseResultMap">
select
<if test="distinct">
distinct
</if>
'false' as QUERYID,
<include refid="Base_Column_List" />
from department
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
<if test="orderByClause != null">
order by ${orderByClause}
</if>
</select>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from department
where department_id = #{departmentId,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from department
where department_id = #{departmentId,jdbcType=INTEGER}
</delete>
<delete id="deleteByExample" parameterType="com.cungudafa.spingmvc01.bean.DepartmentExample">
delete from department
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
</delete>
<insert id="insert" parameterType="com.cungudafa.spingmvc01.bean.Department">
insert into department (department_id, department_name)
values (#{departmentId,jdbcType=INTEGER}, #{departmentName,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" parameterType="com.cungudafa.spingmvc01.bean.Department">
insert into department
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="departmentId != null">
department_id,
</if>
<if test="departmentName != null">
department_name,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="departmentId != null">
#{departmentId,jdbcType=INTEGER},
</if>
<if test="departmentName != null">
#{departmentName,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<select id="countByExample" parameterType="com.cungudafa.spingmvc01.bean.DepartmentExample" resultType="java.lang.Long">
select count(*) from department
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
</select>
<update id="updateByExampleSelective" parameterType="map">
update department
<set>
<if test="record.departmentId != null">
department_id = #{record.departmentId,jdbcType=INTEGER},
</if>
<if test="record.departmentName != null">
department_name = #{record.departmentName,jdbcType=VARCHAR},
</if>
</set>
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByExample" parameterType="map">
update department
set department_id = #{record.departmentId,jdbcType=INTEGER},
department_name = #{record.departmentName,jdbcType=VARCHAR}
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
</if>
</update>
<update id="updateByPrimaryKeySelective" parameterType="com.cungudafa.spingmvc01.bean.Department">
update department
<set>
<if test="departmentName != null">
department_name = #{departmentName,jdbcType=VARCHAR},
</if>
</set>
where department_id = #{departmentId,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.cungudafa.spingmvc01.bean.Department">
update department
set department_name = #{departmentName,jdbcType=VARCHAR}
where department_id = #{departmentId,jdbcType=INTEGER}
</update>
</mapper>
附