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

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



一、用到的实体类如下:
Student.java
[html]  view plain copy
  1. package com.company.entity;  
  2.   
  3. import java.io.Serializable;  
  4. import java.util.Date;  
  5.   
  6. public class Student implements Serializable{  
  7.       
  8.     private static final long serialVersionUID = 1L;  
  9.     private int id;  
  10.     private String name;  
  11.     private Date birth;  
  12.     private Group group;  
  13.       
  14.       
  15.     public Group getGroup() {  
  16.         return group;  
  17.     }  
  18.     public void setGroup(Group group) {  
  19.         this.group = group;  
  20.     }  
  21.     public int getId() {  
  22.         return id;  
  23.     }  
  24.     public void setId(int id) {  
  25.         this.id = id;  
  26.     }  
  27.     public String getName() {  
  28.         return name;  
  29.     }  
  30.     public void setName(String name) {  
  31.         this.name = name;  
  32.     }  
  33.     public Date getBirth() {  
  34.         return birth;  
  35.     }  
  36.     public void setBirth(Date birth) {  
  37.         this.birth = birth;  
  38.     }  
  39.     @Override  
  40.     public String toString() {  
  41.         return "Student [birth=" + birth + "group=" + group + "id=" + id  
  42.                 + ", name=" + name + "]";  
  43.     }  
  44.       
  45.       
  46. }  


Group.Java

[java]  view plain copy
  1. package com.company.entity;  
  2.   
  3. import java.util.List;  
  4.   
  5. public class Group {  
  6.     private int id;  
  7.     private String name;  
  8.     private String position;  
  9.     private List<Student> students;  
  10.       
  11.       
  12.     public List<Student> getStudents() {  
  13.         return students;  
  14.     }  
  15.     public void setStudents(List<Student> students) {  
  16.         this.students = students;  
  17.     }  
  18.     public int getId() {  
  19.         return id;  
  20.     }  
  21.     public void setId(int id) {  
  22.         this.id = id;  
  23.     }  
  24.     public String getName() {  
  25.         return name;  
  26.     }  
  27.     public void setName(String name) {  
  28.         this.name = name;  
  29.     }  
  30.     public String getPosition() {  
  31.         return position;  
  32.     }  
  33.     public void setPosition(String position) {  
  34.         this.position = position;  
  35.     }  
  36.     @Override  
  37.     public String toString() {  
  38.         return "Group [id=" + id + ", name=" + name + ", position=" + position  
  39.                 + "]";  
  40.     }  
  41.       
  42. }  

 

二、实体对应的表结构

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

[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  
  3. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
  4. <mapper namespace="com.company.dao.IStudentDAO">  
  5.       
  6.     <!-- mybatis缓存 -->  
  7.     <cache eviction="LRU" flushInterval="600000" size="1024" readOnly="false" />  
  8.       
  9.     <!-- sql标签用来定义一些可以被重用的sql语句或字段或片段等 -->  
  10.     <sql id="studentColumns">select id,name,birth from student</sql>  
  11.       
  12.     <!-- 此处获得多对一的关系 ,但就单条记录而言却是一对一的关系,所以一对一的写法跟此相同-->  
  13.     <resultMap type="Student" id="getStudentAndGroup" >  
  14.         <id column="id" property="id"/>  
  15.         <result column="name" property="name"/>  
  16.         <result column="birth" property="birth"/>  
  17.         <association property="group" column="group_id" javaType="Group">  
  18.             <id column="g_id" property="id"/>  
  19.             <result column="g_name" property="name"/>  
  20.             <result column="g_position" property="position"/>  
  21.         </association>  
  22.     </resultMap>  
  23.     <select id="many2one" resultMap="getStudentAndGroup" parameterType="int" >  
  24.         select s.id,s.name,s.birth,s.group_id,g.g_id,g.g_name,g.g_position   
  25.         from student s   
  26.         left join g_group g on s.group_id = g.g_id  
  27.         where s.id = #{id}  
  28.     </select>  
  29.       
  30.       
  31.     <!-- 意图是获得一个学生,并且获得该学生所属的组,跟上面的意思差不多 ,用association的select属性-->  
  32.     <!-- 于上面的相比个人感觉上面的效率要高些,因为上面只有一条sql语句 -->  
  33.     <resultMap type="Student" id="getStudentAndGroupUseSelectMap">  
  34.         <id column="id" property="id"/>  
  35.         <result column="name" property="name"/>  
  36.         <result column="birth" property="birth"/>  
  37.         <association property="group" column="group_id" javaType="Group" select="selectGroup" />  
  38.     </resultMap>  
  39.     <select id="getStudentAndGroupUseSelect" resultMap="getStudentAndGroupUseSelectMap" parameterType="int">  
  40.         select *   
  41.         from student   
  42.         where id = #{id}  
  43.     </select>  
  44.     <select id="selectGroup" resultType="Group" parameterType="int" flushCache="false" useCache="true"><!-- 此处实用缓存 -->  
  45.         select g_id as id, g_name as name, g_position as position   
  46.         from g_group   
  47.         where g_id = #{id}  
  48.     </select>  
  49.   
  50.     <!-- 动态sql语句 的测试dynamic sql-->      
  51.     <select id="getStudentBySomeCondition" parameterType="Student" resultType="Student">  
  52.         select *  
  53.         from student  
  54.         <where>  
  55.             <if test="id != null">  
  56.                 id>2  
  57.             </if>  
  58.             <if test="name != null">  
  59.                 and name like '%g%'  
  60.             </if>  
  61.         </where>  
  62.     </select>  
  63.       
  64.     <!-- MyBatis调用存储过程 -->  
  65.     <resultMap type="Student" id="studentMap">  
  66.         <id column="id" property="id"/>  
  67.         <result column="name" property="name"/>  
  68.         <result column="birth" property="birth"/>  
  69.     </resultMap>  
  70.     <select id="getAllUser" statementType="CALLABLE" >  
  71.         {call get_all_student(#{students ,mode=OUTjdbcType=CURSORjavaType=ResultSetresultMap=studentMap} )}  
  72.     </select>  
  73.       
  74.       
  75.     <!-- MyBatis向student表中插入一条数据 -->  
  76.     <insert id="add" parameterType="Student" keyColumn="id">  
  77.         <selectKey keyProperty="id" order="BEFORE" resultType="int">   
  78.             select stu_id_sequence.nextval from dual  
  79.         </selectKey>  
  80.         insert into student(id,name,birth) values(#{id},#{name},#{birth})  
  81.     </insert>  
  82.       
  83.     <!-- 根据id获得学生的信息 -->  
  84.     <select id="getById" parameterType="int" resultType="Student">  
  85.         <include refid="studentColumns"/> where id=#{id}  
  86.     </select>  
  87.       
  88.     <!-- 此处的实现方法是一个分页的原型,请查看IStudentDAOImpl.java中的调用方法 -->  
  89.     <select id="getAllStudent" resultMap="studentMap">  
  90.         <include refid="studentColumns"/> order by id<!--此处是引用了上面预定义好的sql语句-->  
  91.     </select>  
  92.       
  93.       
  94. </mapper>  

你可能感兴趣的:(MyBatisMyBatis存储过程、MyBatis分页、MyBatis一对多增删改查操作)