:
Étudiant.java
Package Com.Combany.entity; import java.io.serializable; Importer java.util.date; classe publique étudiant implémente Serializable {private statique final long SerialVersionUID = 1l; INT PRIVÉ ID; nom de chaîne privé; Date privée naissance; groupe de groupes privés; groupe public getGroup () {return groupe; } public void setGroup (groupe de groupe) {this.group = groupe; } public int getID () {return id; } public void setid (int id) {this.id = id; } public String getName () {Nom de retour; } public void setName (string name) {this.name = name; } Public Date getBirth () {retour naissance; } public void SetBirth (date naissance) {this.birth = naissance; } @Override public String toString () {return "Student [naissance =" + naissance + ", groupe =" + groupe + ", id =" + id + ", name =" + name + "]"; }}Group.java
Package Com.Combany.entity; Importer java.util.list; Group de classe publique {private int id; nom de chaîne privé; position de chaîne privée; Liste privée <Studiante> Étudiants; Liste publique <Student> GetSudents () {Retour des étudiants; } public void SetStudents (list <Student> étudiants) {this.students = étudiants; } public int getID () {return id; } public void setid (int id) {this.id = id; } public String getName () {Nom de retour; } public void setName (string name) {this.name = name; } public String getPosition () {position de retour; } public void setPosition (position de chaîne) {this.position = position; } @Override public String toString () {return "groupe [id =" + id + ", name =" + name + ", position =" + position + "]"; }}二、实体对应的表结构
Étudiant 表:
Create Table Student (id int Key primaire, nom varchar (20), Date de naissance, group_id int références g_group (g_id));
g_group 表:
Créer la table g_group (g_id int clé primaire, g_name varchar (20), g_position varchar (30));
séquence:
Créer une séquence Student_id_Sequence; Créer une séquence Group_ID_Sequence;
三、 Élève 和 Groupe 的映射文件如下 , 你可以在映射文件中找到 , 关于 Mybatis 的增删改查操作 , Mybatis 调用存储过程 , Mybatis 分页以及 Mybatis 对一对一、多对多的处理
xml 文件中都标有注释 , 看的时候配合下面的具体实现看 , 虽然有点乱
Student.xml
<? xml version = "1.0" Encoding = "UTF-8"?> <! Doctype Mappeur public "- // Mybatis.org//dtd Mapper 3.0 // en" "http://mybatis.org/dtd/mybatis.Dao.SentDa"> <Mawpper Namespace = "com.m.company.dao.istudda"> <maper <! - Mybatis 缓存 -> <cache eviction = "lru" flushinterval = "600000" size = "1024" readonly = "false" /> <! - SQL 标签用来定义一些可以被重用的 SQL 语句或字段或片段等 -> <sql id = "StudentColumns"> SELECT ID, nom, naissance de l'étudiant </ sql> <! id = "getStudentAndGroup"> <id chronn = "id" propriété = "id" /> <result Column = "name" Property = "name" /> <result Column = "naissance" propriété = "naissance" /> <Association Property = "Group" Column = "Group_id" Javatype = "Group"> <id Column = "g_id" Property = "id" /> <Result Column = "g_name" NOM Column = "g_position" propriété = "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 gauche où s.id = # {id} </lect> <! - 意图是获得一个学生 , 并且获得该学生所属的组 , 跟上面的意思差不多 跟上面的意思差不多, 用 Association 的 Select 属性 -> <! - 于上面的相比个人感觉上面的效率要高些 , 因为上面只有一条 sql 语句 -> <resultmap type = "Student" id = "getStudentAndGroupUSeSelectMap"> <id Column = "id" property = "id" /> <résultat colonne Column = "Birth" Property = "Birth" /> <Association Property = "Group" Column = "Group_id" Javatype = "Group" select = "SelectGroup" /> </ ResultMap> <SELECT ID = "GetStudentAndGroupUSelect" ResultMap = "GetStudentandGroupUSeSelectMap" ParamETRYPE = "Int"> SELECT * id = "selectGroup" resultType = "groupe" parameterType = "int" flushcache = "false" usecache = "true"> <! - 此处实用缓存 -> sélectionnez g_id comme id, g_name comme nom, g_position comme position dans g_group où g_id = # {id} </lect> <! id = "getStudentBysomecondition" ParameterType = "Student" resultType = "Student"> SELECT * FROM Student <où> <if test = "id! = null"> id> 2 </ if> <if test = "name! = null"> and name like '% g " <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, Javaty resultMap = StudentMap})} </lect> <! - MyBatis 向 Student 表中插入一条数据 -> <insert id = "add" ParameterType = "Student" keyColumn = "id"> <SelectKey KeyProperty = "id" Order = "avant" ResultType = "int"> Sélectionner Stu_id_sence.nextVal dans Dual </ SelectKey> Insist insert dans Stu_Id_Sevel) VALEURS (# {id}, # {name}, # {naissance}) </serser> <! - 根据 id 获得学生的信息 -> <select id = "getbyId" ParameterType = "int" resultType = "Student"> <include refid = "StudentColumns" /> Where id = # {id} </lect> <! - 此处的实现方法是一个分页的原型, 请查看 请查看 请查看 id = # {id} <select id = "getAllStudent" resultMap = "StudentMap"> <include refid = "StudentColumns" /> Order by Id <! - 此处是引用了上面预定义好的 SQL 语句 -> </lect> </ mapper>以上所述是小编给大家介绍的 Mybatis 存储过程、 Mybatis 分页、 Mybatis 一对多增删改查操作 , 希望对大家有所帮助 , 如果大家有任何疑问请给我留言 , 小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持!