一、用到的实体类如下
Estudante.java
pacote com.company.entity; importar java.io.serializable; importar java.util.date; A classe pública Student implementa serializável {private estático final serialversionuid = 1L; private int id; nome de string privado; data privada nascimento; grupo de grupo privado; grupo público 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 () {return name; } public void setName (nome da string) {this.name = name; } public data getBirth () {retornar nascimento; } public void Setbirth (data de nascimento) {this.birth = nascimento; } @Override public string tostring () {return "Student [nascimento =" + nascimento + ", grupo =" + grupo + ", id =" + id + ", nome =" + name + "]"; }}Grupo.java
pacote com.company.entity; importar java.util.list; Public Class Group {private int id; nome de string privado; posição privada de string; Lista privada <estudante> alunos; Lista pública <very Student> getStudents () {return Students; } public void SetStudents (lista <estudante> alunos) {this.students = estudantes; } public int getId () {return id; } public void setId (int id) {this.id = id; } public string getName () {return name; } public void setName (nome da string) {this.name = name; } public string getPosition () {retorna posição; } public void setPosition (posição da string) {this.Position = Position; } @Override public string tostring () {return "group [id =" + id + ", name =" + name + ", position =" + position + "]"; }}二、实体对应的表结构
estudante 表:
Criar Table Student (Id int Primary Key, Nome Varchar (20), Data de Nascimento, Grupo_id Int Referências G_Group (G_ID));
g_group 表:
Criar tabela g_group (g_id int a chave primária, g_name varchar (20), g_position varchar (30));
Sequência:
Criar sequence student_id_sequence; crie sequence group_id_sequence;
三、 Student 和 Grupo 的映射文件如下 , 你可以在映射文件中找到 , 关于 mybatis 的增删改查操作 , mybatis 调用存储过程 , mybatis 分页以及 mybatis 对一对一、多对多的处理
xml 文件中都标有注释 , 看的时候配合下面的具体实现看 , 虽然有点乱
estudante.xml
<? xml versão = "1.0" Encoding = "utf-8"?> <! <!-mybatis 缓存-> <cache despettion = "lru" flushInterval = "600000" size = "1024" readOnly = "false" /> <!-sql 标签用来定义一些可以被重用的 sql 语句或字段或片段等-> <sql id = "studentcolumns"> selecione id, nome do aluno < /sql> <! id = "getStudentAndgroup"> <id column = "id" propriedade = "id"/> <resultado column = "name" propriedade = "name"/> <resultado column = "nascimento" property = "nascimento"/> <idiates de propriedade "grupo" column = "group_id" "javatype =" result "> <id column =" g_id "") 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, g.girth, s.group_id, g.g_id, g.g_name, g.g.g.pirth de onde s.id = #{id} </select> <!-意图是获得一个学生 , 并且获得该学生所属的组 , 跟上面的意思差不多, 用 Associação 的 Selecione 属性-> <!-于上面的相比个人感觉上面的效率要高些 , 因为上面只有一条 sql 语句-> <resultado column = "nascimento" propriedade = "nascimento"/> <Association Property = "Group" column = "group_id" javatype = "group" select = "selectGroup"/> </resultmap> <select id = "getStudEntAndGroupUseSelect" ResultMap = "getStudentRoupRoupUsElectMap" parameterType = " id = "selectGroup" resultype = "group" parameterType = "int" flushcache = "false" usecache = "true"> <!-此处实用缓存-> selecione g_id como id, g_name como nome, g_position como posição de g_group onde g_id = #{id} </select> <! id = "getStudentBySomeCondition" ParameterType = "Student" resultType = "Student"> Selecione * do aluno <where> <se test = "id! = null"> id> 2 </se> <se test = "name! <id column = "id" property = "id"/> <resultado colun = "name" propriedade = "name"/> <resultado column = "nascimento" property = "nascimento"/> </resultMap> <select id = "getAllUser" statustype = "callable"> {Call Get_All_Student (#{estudantes, out = resultMap = studentMap})} </leclect> <!-mybatis 向 aluno 表中插入一条数据-> <insert id = "add" parametertype = "student" keycolumn = "id"> <selectKey KeyProperty = "id" order = "Antes" resultType = "int"> selecione STU_IDENCENENCE.NextVal. valores (#{id},#{name},#{nascimento}) </insert> <!-根据 id 获得学生的信息-> <select id = "getByid" parameterType = "int" resultType = "Student"> <! <select id = "getAllStudent" resultmap = "studentmap"> <incluir refid = "studentColumns"/> encomendar por id <!-此处是引用了上面预定义好的 sql 语句-> </leclect> </mapper>以上所述是小编给大家介绍的 mybatis 存储过程、 mybatis 分页、 mybatis 一对多增删改查操作 , 希望对大家有所帮助 , 如果大家有任何疑问请给我留言 , 小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持!