欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MyBatis存储过程、MyBatis分页、MyBatis一对多增删改查操作

程序员文章站 2024-03-31 20:08:04
一、用到的实体类如下: student.java package com.company.entity; import java.io.serializ...

一、用到的实体类如下:

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 
+ "]"; 
} 
}

二、实体对应的表结构

student表:

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

g_group表:

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;

三、student和group的映射文件如下,你可以在映射文件中找到,关于mybatis的增删改查操作,mybatis调用存储过程,mybatis分页以及mybatis对一对一、多对多的处理

xml文件中都标有注释,看的时候配合下面的具体实现看,虽然有点乱

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缓存 --> 
<cache eviction="lru" flushinterval="600000" size="1024" readonly="false" /> 
<!-- sql标签用来定义一些可以被重用的sql语句或字段或片段等 --> 
<sql id="studentcolumns">select id,name,birth from student</sql> 
<!-- 此处获得多对一的关系 ,但就单条记录而言却是一对一的关系,所以一对一的写法跟此相同--> 
<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> 
<!-- 意图是获得一个学生,并且获得该学生所属的组,跟上面的意思差不多 ,用association的select属性--> 
<!-- 于上面的相比个人感觉上面的效率要高些,因为上面只有一条sql语句 --> 
<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"><!-- 此处实用缓存 --> 
select g_id as id, g_name as name, g_position as position 
from g_group 
where g_id = #{id} 
</select> 
<!-- 动态sql语句 的测试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调用存储过程 --> 
<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向student表中插入一条数据 --> 
<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> 
<!-- 根据id获得学生的信息 --> 
<select id="getbyid" parametertype="int" resulttype="student"> 
<include refid="studentcolumns"/> where id=#{id} 
</select> 
<!-- 此处的实现方法是一个分页的原型,请查看istudentdaoimpl.java中的调用方法 --> 
<select id="getallstudent" resultmap="studentmap"> 
<include refid="studentcolumns"/> order by id<!--此处是引用了上面预定义好的sql语句--> 
</select> 
</mapper> 

以上所述是小编给大家介绍的mybatis存储过程、mybatis分页、mybatis一对多增删改查操作,希望对大家有所帮助