MyBatis stored procedures MyBatis paging MyBatis one to many add delete change and check operations

  • 2020-05-17 05:39:51
  • OfStack

1. The entity classes used are as follows:

Student.java


package com.company.entity; 
import java.io.Serializable; 
import java.util.Date; 
public class Student implements Serializable{ 
private static final long serialVersionUID = 1L; 
private int id; 
private String name; 
private Date birth; 
private Group group; 
public Group getGroup() { 
return group; 
} 
public void setGroup(Group group) { 
this.group = group; 
} 
public int getId() { 
return id; 
} 
public void setId(int id) { 
this.id = id; 
} 
public String getName() { 
return name; 
} 
public void setName(String name) { 
this.name = name; 
} 
public Date getBirth() { 
return birth; 
} 
public void setBirth(Date birth) { 
this.birth = birth; 
} 
@Override 
public String toString() { 
return "Student [birth=" + birth + ", group=" + group + ", id=" + id 
+ ", name=" + name + "]"; 
} 
}

Group.Java


package com.company.entity; 
import java.util.List; 
public class Group { 
private int id; 
private String name; 
private String position; 
private List<Student> students; 
public List<Student> getStudents() { 
return students; 
} 
public void setStudents(List<Student> students) { 
this.students = students; 
} 
public int getId() { 
return id; 
} 
public void setId(int id) { 
this.id = id; 
} 
public String getName() { 
return name; 
} 
public void setName(String name) { 
this.name = name; 
} 
public String getPosition() { 
return position; 
} 
public void setPosition(String position) { 
this.position = position; 
} 
@Override 
public String toString() { 
return "Group [id=" + id + ", name=" + name + ", position=" + position 
+ "]"; 
} 
}

2. Table structure corresponding to the entity

student table:


create table student(
id int primary key,
name varchar(20),
birth date,
group_id int references g_group(g_id));

g_group table:


create table g_group(
g_id int primary key,
g_name varchar(20),
g_position varchar(30));

sequence:


create sequence student_id_sequence;
create sequence group_id_sequence;

3. The mapping files for Student and Group are as follows, which you can find in the mapping files, about the addition, deletion, and modification of MyBatis, MyBatis calling stored procedures, MyBatis paging, and MyBatis 1-to-1, many-to-many processing

The xml files are all marked with comments. When you look at them, you can see them together with the following implementation, although it is a bit messy

student.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.company.dao.IStudentDAO"> 
<!-- mybatis The cache  --> 
<cache eviction="LRU" flushInterval="600000" size="1024" readOnly="false" /> 
<!-- sql Tags are used to define 1 Something that can be reused sql Statement or field or fragment, etc  --> 
<sql id="studentColumns">select id,name,birth from student</sql> 
<!--  Get multiple pairs here 1 The relationship between  , But for a single record it is 1 right 1 so 1 right 1 I'm going to write it the same way --> 
<resultMap type="Student" id="getStudentAndGroup" > 
<id column="id" property="id"/> 
<result column="name" property="name"/> 
<result column="birth" property="birth"/> 
<association property="group" column="group_id" javaType="Group"> 
<id column="g_id" property="id"/> 
<result column="g_name" property="name"/> 
<result column="g_position" property="position"/> 
</association> 
</resultMap> 
<select id="many2one" resultMap="getStudentAndGroup" parameterType="int" > 
select s.id,s.name,s.birth,s.group_id,g.g_id,g.g_name,g.g_position 
from student s 
left join g_group g on s.group_id = g.g_id 
where s.id = #{id} 
</select> 
<!--  Intent is to obtain 1 Three students, and the group to which the student belongs  , with association the select attribute --> 
<!--  It's a little bit more efficient on the top than on the top, because there's only one 1 article sql statements  --> 
<resultMap type="Student" id="getStudentAndGroupUseSelectMap"> 
<id column="id" property="id"/> 
<result column="name" property="name"/> 
<result column="birth" property="birth"/> 
<association property="group" column="group_id" javaType="Group" select="selectGroup" /> 
</resultMap> 
<select id="getStudentAndGroupUseSelect" resultMap="getStudentAndGroupUseSelectMap" parameterType="int"> 
select * 
from student 
where id = #{id} 
</select> 
<select id="selectGroup" resultType="Group" parameterType="int" flushCache="false" useCache="true"><!--  Useful cache here  --> 
select g_id as id, g_name as name, g_position as position 
from g_group 
where g_id = #{id} 
</select> 
<!--  dynamic sql statements   The test of dynamic sql--> 
<select id="getStudentBySomeCondition" parameterType="Student" resultType="Student"> 
select * 
from student 
<where> 
<if test="id != null"> 
id>2 
</if> 
<if test="name != null"> 
and name like '%g%' 
</if> 
</where> 
</select> 
<!-- MyBatis Calling stored procedure  --> 
<resultMap type="Student" id="studentMap"> 
<id column="id" property="id"/> 
<result column="name" property="name"/> 
<result column="birth" property="birth"/> 
</resultMap> 
<select id="getAllUser" statementType="CALLABLE" > 
{call get_all_student(#{students ,mode=OUT, jdbcType=CURSOR, javaType=ResultSet, resultMap=studentMap} )} 
</select> 
<!-- MyBatis to student Inserted in the table 1 The data  --> 
<insert id="add" parameterType="Student" keyColumn="id"> 
<selectKey keyProperty="id" order="BEFORE" resultType="int"> 
select stu_id_sequence.nextval from dual 
</selectKey> 
insert into student(id,name,birth) values(#{id},#{name},#{birth}) 
</insert> 
<!--  According to the id Get information about the students  --> 
<select id="getById" parameterType="int" resultType="Student"> 
<include refid="studentColumns"/> where id=#{id} 
</select> 
<!--  Here's how to do it 1 A prototype for pagination , Please see the IStudentDAOImpl.java Call method in  --> 
<select id="getAllStudent" resultMap="studentMap"> 
<include refid="studentColumns"/> order by id<!-- This is a reference to the one predefined above sql statements --> 
</select> 
</mapper> 

Related articles: