欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2016年11月21日 14:00:17   作者:bestlove13141516  
本文通過一段代碼給大家介紹了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)站的支持!

相關(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)出功能

    這篇文章主要介紹了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-09
  • Netty學(xué)習(xí)教程之基礎(chǔ)使用篇

    Netty學(xué)習(xí)教程之基礎(chǔ)使用篇

    Netty是由JBOSS提供的一個Java開源框架。Netty提供異步的、事件驅(qū)動的網(wǎng)絡(luò)應(yīng)用程序框架和工具,用以快速開發(fā)高性能、高可靠性的網(wǎng)絡(luò)服務(wù)器和客戶端程序。下面這篇文章主要給大家介紹了關(guān)于Netty基礎(chǔ)使用的相關(guān)資料,需要的朋友可以參考下。
    2017-05-05
  • SpringAop源碼及調(diào)用過程概述

    SpringAop源碼及調(diào)用過程概述

    這篇文章主要介紹了SpringAop源碼及調(diào)用過程概述,Spring AOP(面向切面編程)是Spring框架的一個重要特性,它提供了一種在程序運(yùn)行期間動態(tài)地將額外的行為織入到代碼中的方式,需要的朋友可以參考下
    2023-10-10
  • Java獲取用戶訪問IP及地理位置的方法詳解

    Java獲取用戶訪問IP及地理位置的方法詳解

    這篇文章主要介紹了Java獲取用戶訪問IP及地理位置的方法,結(jié)合實(shí)例形式詳細(xì)分析了Java基于百度地圖開放平臺獲取用戶訪問IP及地理位置相關(guān)操作技巧,需要的朋友可以參考下
    2020-04-04
  • Java 通過JDBC連接Mysql數(shù)據(jù)庫

    Java 通過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問題的解決

    這篇文章主要介紹了基于strict-origin-when-cross-origin問題的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • 線程池中execute與submit的區(qū)別說明

    線程池中execute與submit的區(qū)別說明

    這篇文章主要介紹了線程池execute與submit的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • SpringBoot項(xiàng)目實(shí)戰(zhàn)之?dāng)?shù)據(jù)交互篇

    SpringBoot項(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-03
  • Mybatis的一級緩存和二級緩存原理分析與使用

    Mybatis的一級緩存和二級緩存原理分析與使用

    mybatis-plus 是一個 Mybatis 的增強(qiáng)工具,在 Mybatis 的基礎(chǔ)上只做增強(qiáng)不做改變,為簡化開發(fā)、提高效率而生,這篇文章帶你了解Mybatis的一級和二級緩存
    2021-11-11
  • java統(tǒng)計(jì)字符串單詞個數(shù)的方法解析

    java統(tǒng)計(jì)字符串單詞個數(shù)的方法解析

    在一些項(xiàng)目中可能需要對一段字符串中的單詞進(jìn)行統(tǒng)計(jì),本文在這里分享了一個簡單的demo,有需要的朋友可以拿去看一下
    2017-01-01

最新評論