一、用到的实体类如下 :
Estudiante.java
paquete com.company.entity; import java.io.serializable; import java.util.date; El estudiante de clase pública implementa serializable {privado estático final long SerialVersionUid = 1l; ID privado int; nombre de cadena privada; Nacimiento de fecha privada; Grupo de grupo privado; Public Group getGroup () {Return Group; } public void setGroup (grupo de grupo) {this.group = grupo; } public int getId () {return id; } public void setid (int id) {this.id = id; } public String getName () {nombre de retorno; } public void setName (nombre de cadena) {this.name = name; } Fecha pública GetBirth () {Return Birth; } public void setBirth (fecha de nacimiento) {this.birth = nacimiento; } @Override public String toString () {return "Student [Birth =" + Birth + ", Group =" + Group + ", id =" + id + ", nombre =" + nombre + "]"; }}Grupo.java
paquete com.company.entity; import java.util.list; Public Class Group {private int id; nombre de cadena privada; posición de cadena privada; Lista privada <Estudiante> Estudiantes; Lista pública <Estude> 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 () {nombre de retorno; } public void setName (nombre de cadena) {this.name = name; } public String getPosition () {posición de retorno; } public void setPosition (posición de cadena) {this.position = posicion; } @Override public string toString () {return "group [id =" + id + ", name =" + name + ", posicion =" + posicion + "]"; }}二、实体对应的表结构
Estudiante 表:
Cree Tabla Student (ID int Prime Key, Name Varchar (20), Fecha de nacimiento, Group_ID int References G_Group (g_id));
G_GROUP 表:
Crear tabla g_group (G_ID int primaria, g_name varchar (20), g_position varchar (30));
secuencia:
Crear secuencia student_id_sequence; Crear secuencia Group_id_Sequence;
三、 Estudiante 和 grupo 的映射文件如下 , 你可以在映射文件中找到 关于 关于 mybatis 的增删改查操作 , mybatis 调用存储过程 , mybatis 分页以及 mybatis 对一对一、多对多的处理
XML 文件中都标有注释 , 看的时候配合下面的具体实现看 虽然有点乱
estudiante.xml
<? xml versión = "1.0" encoding = "utf-8"?> <! Doctype mapper public "-// mybatis.org//dtd mapper 3.0 // en" "http://mybatis.org/dtd/mybatis-3-mapper.dttd"> <mepperspace = "company.da.dapany. < id = "getStudentAndGroup"> <id columna = "id" propiedad = "id"/> <resultado columna = "name" propiedad = "name"/> <resultado columna = "birth" propiedad = "nacimiento"/> <asociación propiedad = "group" column = "group_id" javatype = "grupo"> <id columna = "g_id" propiedad = "id"/> <resultado columna = "g_name" propiedad "name"/"name"/"name"/"name" columna = "g_position" propiedad = "posición"/> </asociación> </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 de student s -g_group on g_group en goup en goup en goup en s.group s.id = #{id} </select> <!-意图是获得一个学生 , 并且获得该学生所属的组 , 跟上面的意思差不多, 用 Association 的 Seleccione 属性-> <!-于上面的相比个人感觉上面的效率要高些 , 因为上面只有一条 sql 语句-> <resultmap type = "student" id = "getStudentAndGroupUsElectMap"> <id columna = "id" property = "id"/> <resultado columna = "nombre" name = "name"/"name"/"/" nace " Propiedad = "Birth"/> <Association Property = "Group" columna = "Group_id" javatype = "grupo" select = "selectGroup"/> </resultMap> <select id = "getStudentAndGroupUseSelect" resultMap = "getStudentAndGroupUsEsElectMap de parámetro" resultType = "Group" parametertype = "int" flushcache = "false" useCache = "true"> <!-此处实用缓存-> seleccione g_id como id, g_name as name, g_position como posición de g_group donde g_id = #{id} </select> <!-动态 sql 语句 的测试 的测试 的测试 的测试 的测试 的测试 的测试 的测试 的测试 的测试 的测试 的测试 的测试 的测试 的测试 的测试 的测试 的测试 的测试 的测试 的测试 的测试 的测试 的测试 的测试 的测试 的测试 的测试 的测试 的测试 的测试 it it = " parametertype = "Student" resultType = "Student"> Select * De Student <Where> <if test = "id! = Null"> id> 2 </if> <if test = "name! = null"> y como '%g%' </if> </where> </select> <!-mybatishis 调用存储过程-> <resultmap type = "estudiante" id = "studentmap"> <<d columna columna "Id" Id "Id" Id "/" Propiedad "ID" <resultado columna = "name" propiedad = "name"/> <resultado columna = "birth" propiedad = "nacimiento"/> </resultadomap> <select id = "getAlluser" DeclaryPe = "Callable"> {call get_all_student (#{students, mode = out, jdbctype = cursor, javatype = resultset, resultMap = studentMap}) MyBatis 向 estudiante 表中插入一条数据 -> <insertar id = "add" parametertype = "student" keycolumn = "id"> <selectKey keyProperty = "id" orden = "antes" antes de "resultype =" int "> seleccione Stu_id_Sequence.NextVal de dual </selectKey> Insert en los valores (id, nombre, nombre) (#{#}, n. </sert> <!-根据 根据 id 获得学生的信息-> <select id = "getByid" parametertype = "int" denttype = "student"> <include refid = "studentColumns"/> where id =#{id} </select> <!-此处的实现方法是一个分页的原型, 请查看 istentDaooImpl.Java 中的调用方法-> <select Id = "getAllStudent" resultmap = "sententmap" Refid = "StudentColumns"/> orden por id <以上所述是小编给大家介绍的 mybatis 存储过程、 mybatis 分页、 mybatis 一对多增删改查操作 , 希望对大家有所帮助 如果大家有任何疑问请给我留言 如果大家有任何疑问请给我留言 小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持! 小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持!