国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

MyBatis存儲過程、MyBatis分頁、MyBatis一對多增刪改查操作

2019-11-26 13:32:17
字體:
來源:轉載
供稿:網友

一、用到的實體類如下:

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一對多增刪改查操作,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 银川市| 高陵县| 弥渡县| 新田县| 天台县| 临夏县| 永昌县| 南昌市| 涿鹿县| 富锦市| 合川市| 贡觉县| 锡林郭勒盟| 繁峙县| 石门县| 六盘水市| 大厂| 石柱| 施甸县| 虎林市| 中宁县| 广南县| 宿松县| 梅州市| 南涧| 冷水江市| 临漳县| 清丰县| 从化市| 漳州市| 新密市| 鄢陵县| 依兰县| 绍兴县| 汤阴县| 平定县| 且末县| 专栏| 高邑县| 巴彦淖尔市| 霸州市|