MyBatis存儲過程、MyBatis分頁、MyBatis一對多增刪改查操作
一、用到的實(shí)體類如下:
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 + "]"; } }
二、實(shí)體對應(yīng)的表結(jié)構(gòu)
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的映射文件如下,你可以在映射文件中找到,關(guān)于MyBatis的增刪改查操作,MyBatis調(diào)用存儲過程,MyBatis分頁以及MyBatis對一對一、多對多的處理
xml文件中都標(biāo)有注釋,看的時候配合下面的具體實(shí)現(xiàn)看,雖然有點(diǎn)亂
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標(biāo)簽用來定義一些可以被重用的sql語句或字段或片段等 --> <sql id="studentColumns">select id,name,birth from student</sql> <!-- 此處獲得多對一的關(guān)系 ,但就單條記錄而言卻是一對一的關(guān)系,所以一對一的寫法跟此相同--> <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> <!-- 意圖是獲得一個學(xué)生,并且獲得該學(xué)生所屬的組,跟上面的意思差不多 ,用association的select屬性--> <!-- 于上面的相比個人感覺上面的效率要高些,因?yàn)樯厦嬷挥幸粭lsql語句 --> <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"><!-- 此處實(shí)用緩存 --> select g_id as id, g_name as name, g_position as position from g_group where g_id = #{id} </select> <!-- 動態(tài)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調(diào)用存儲過程 --> <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表中插入一條數(shù)據(jù) --> <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> <!-- 根據(jù)id獲得學(xué)生的信息 --> <select id="getById" parameterType="int" resultType="Student"> <include refid="studentColumns"/> where id=#{id} </select> <!-- 此處的實(shí)現(xiàn)方法是一個分頁的原型,請查看IStudentDAOImpl.java中的調(diào)用方法 --> <select id="getAllStudent" resultMap="studentMap"> <include refid="studentColumns"/> order by id<!--此處是引用了上面預(yù)定義好的sql語句--> </select> </mapper>
以上所述是小編給大家介紹的MyBatis存儲過程、MyBatis分頁、MyBatis一對多增刪改查操作,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- Mybatis增刪改查mapper文件寫法詳解
- MyBatis中SqlSession實(shí)現(xiàn)增刪改查案例
- Spring boot + mybatis + Vue.js + ElementUI 實(shí)現(xiàn)數(shù)據(jù)的增刪改查實(shí)例代碼(二)
- Mybatis實(shí)現(xiàn)增刪改查及分頁查詢的方法
- Spring boot + mybatis + Vue.js + ElementUI 實(shí)現(xiàn)數(shù)據(jù)的增刪改查實(shí)例代碼(一)
- MyBatis入門之增刪改查+數(shù)據(jù)庫字段和實(shí)體字段不一致問題處理方法
- Mybatis 條件查詢 批量增刪改查功能
- MyBatis增刪改查快速上手
相關(guān)文章
Spring?boot?easyexcel?實(shí)現(xiàn)復(fù)合數(shù)據(jù)導(dǎo)出、按模塊導(dǎo)出功能
這篇文章主要介紹了Spring?boot?easyexcel?實(shí)現(xiàn)復(fù)合數(shù)據(jù)導(dǎo)出、按模塊導(dǎo)出,實(shí)現(xiàn)思路流程是準(zhǔn)備一個導(dǎo)出基礎(chǔ)填充模板,默認(rèn)填充key,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-09-09Java 通過JDBC連接Mysql數(shù)據(jù)庫
本文給大家詳細(xì)介紹了java如何使用JDBC連接Mysql的方法以及驅(qū)動包的安裝,最后給大家附上了java通過JDBC連接其他各種數(shù)據(jù)庫的方法,有需要的小伙伴可以參考下。2015-11-11基于strict-origin-when-cross-origin問題的解決
這篇文章主要介紹了基于strict-origin-when-cross-origin問題的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03SpringBoot項(xiàng)目實(shí)戰(zhàn)之?dāng)?shù)據(jù)交互篇
這篇文章主要給大家介紹了關(guān)于SpringBoot項(xiàng)目實(shí)戰(zhàn)之?dāng)?shù)據(jù)交互篇的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2022-03-03java統(tǒng)計(jì)字符串單詞個數(shù)的方法解析
在一些項(xiàng)目中可能需要對一段字符串中的單詞進(jìn)行統(tǒng)計(jì),本文在這里分享了一個簡單的demo,有需要的朋友可以拿去看一下2017-01-01