Mybatis示例講解注解開發(fā)中的單表操作
Mybatis注解開發(fā)單表操作
MyBatis的常用注解
Mybatis也可以使用注解開發(fā)方式,這樣我們就可以減少編寫Mapper映射文件了。我們先圍繞一些基本的CRUD來學(xué)習(xí),再學(xué)習(xí)復(fù)雜映射多表操作。
| 注解 | 說明 |
|---|---|
| @Insert | 實(shí)現(xiàn)新增 |
| @Update | 實(shí)現(xiàn)更新 |
| @Delete | 實(shí)現(xiàn)刪除 |
| @Select | 實(shí)現(xiàn)查詢 |
| @Result | 實(shí)現(xiàn)結(jié)果集封裝 |
| @Results | 可以與@Result 一起使用,封裝多個(gè)結(jié)果集 |
| @One | 實(shí)現(xiàn)一對(duì)一結(jié)果集封裝 |
| @Many | 實(shí)現(xiàn)一對(duì)多結(jié)果集封 |
MyBatis的增刪改查
我們完成簡單的student表的增刪改查的操作
步驟一:創(chuàng)建mapper接口
public interface StudentMapper {
//查詢?nèi)?
@Select("SELECT * FROM student")
public abstract List<Student> selectAll();
//新增操作
@Insert("INSERT INTO student VALUES (#{id},#{name},#{age})")
public abstract Integer insert(Student stu);
//修改操作
@Update("UPDATE student SET name=#{name},age=#{age} WHERE id=#{id}")
public abstract Integer update(Student stu);
//刪除操作
@Delete("DELETE FROM student WHERE id=#{id}")
public abstract Integer delete(Integer id);
}步驟二:測試類
public class Test01 {
@Test
public void selectAll() throws Exception{
//1.加載核心配置文件
InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
//2.獲取SqlSession工廠對(duì)象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//3.通過工廠對(duì)象獲取SqlSession對(duì)象
SqlSession sqlSession = sqlSessionFactory.openSession(true);
//4.獲取StudentMapper接口的實(shí)現(xiàn)類對(duì)象
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
//5.調(diào)用實(shí)現(xiàn)類對(duì)象中的方法,接收結(jié)果
List<Student> list = mapper.selectAll();
//6.處理結(jié)果
for (Student student : list) {
System.out.println(student);
}
//7.釋放資源
sqlSession.close();
is.close();
}
@Test
public void insert() throws Exception{
//1.加載核心配置文件
InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
//2.獲取SqlSession工廠對(duì)象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//3.通過工廠對(duì)象獲取SqlSession對(duì)象
SqlSession sqlSession = sqlSessionFactory.openSession(true);
//4.獲取StudentMapper接口的實(shí)現(xiàn)類對(duì)象
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
//5.調(diào)用實(shí)現(xiàn)類對(duì)象中的方法,接收結(jié)果
Student stu = new Student(4,"趙六",26);
Integer result = mapper.insert(stu);
//6.處理結(jié)果
System.out.println(result);
//7.釋放資源
sqlSession.close();
is.close();
}
@Test
public void update() throws Exception{
//1.加載核心配置文件
InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
//2.獲取SqlSession工廠對(duì)象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//3.通過工廠對(duì)象獲取SqlSession對(duì)象
SqlSession sqlSession = sqlSessionFactory.openSession(true);
//4.獲取StudentMapper接口的實(shí)現(xiàn)類對(duì)象
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
//5.調(diào)用實(shí)現(xiàn)類對(duì)象中的方法,接收結(jié)果
Student stu = new Student(4,"趙六",36);
Integer result = mapper.update(stu);
//6.處理結(jié)果
System.out.println(result);
//7.釋放資源
sqlSession.close();
is.close();
}
@Test
public void delete() throws Exception{
//1.加載核心配置文件
InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
//2.獲取SqlSession工廠對(duì)象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//3.通過工廠對(duì)象獲取SqlSession對(duì)象
SqlSession sqlSession = sqlSessionFactory.openSession(true);
//4.獲取StudentMapper接口的實(shí)現(xiàn)類對(duì)象
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
//5.調(diào)用實(shí)現(xiàn)類對(duì)象中的方法,接收結(jié)果
Integer result = mapper.delete(4);
//6.處理結(jié)果
System.out.println(result);
//7.釋放資源
sqlSession.close();
is.close();
}
}例如運(yùn)行test1結(jié)果如下:

注意:
修改MyBatis的核心配置文件,我們使用了注解替代的映射文件,所以我們只需要加載使用了注解的Mapper接口即可
<mappers>
<!--掃描使用注解的類-->
<mapper class="com.yyl.mapper.UserMapper"></mapper>
</mappers>
? 或者指定掃描包含映射關(guān)系的接口所在的包也可以
<mappers>
<!--掃描使用注解的類所在的包-->
<package name="com.yyl.mapper"></package>
</mappers>

注解開發(fā)總結(jié)
注解可以簡化開發(fā)操作,省略映射配置文件的編寫。
常用注解
@Select(“查詢的 SQL 語句”):執(zhí)行查詢操作注解
@Insert(“查詢的 SQL 語句”):執(zhí)行新增操作注解
@Update(“查詢的 SQL 語句”):執(zhí)行修改操作注解
@Delete(“查詢的 SQL 語句”):執(zhí)行刪除操作注解
配置映射關(guān)系
<mappers>
<package name="接口所在包"/>
</mappers>
練習(xí)項(xiàng)目代碼
bean
package com.yyl.bean;
public class Student {
private Integer id;
private String name;
private Integer age;
public Student() {
}
public Student(Integer id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}mapper.class
package com.yyl.mapper;
import com.yyl.bean.Student;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.util.List;
public interface StudentMapper {
//查詢?nèi)?
@Select("SELECT * FROM student")
public abstract List<Student> selectAll();
//新增操作
@Insert("INSERT INTO student VALUES (#{id},#{name},#{age})")
public abstract Integer insert(Student stu);
//修改操作
@Update("UPDATE student SET name=#{name},age=#{age} WHERE id=#{id}")
public abstract Integer update(Student stu);
//刪除操作
@Delete("DELETE FROM student WHERE id=#{id}")
public abstract Integer delete(Integer id);
}test
package com.yyl.test;
import com.yyl.bean.Student;
import com.yyl.mapper.StudentMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import java.io.InputStream;
import java.util.List;
public class Test01 {
@Test
public void selectAll() throws Exception{
//1.加載核心配置文件
InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
//2.獲取SqlSession工廠對(duì)象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//3.通過工廠對(duì)象獲取SqlSession對(duì)象
SqlSession sqlSession = sqlSessionFactory.openSession(true);
//4.獲取StudentMapper接口的實(shí)現(xiàn)類對(duì)象
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
//5.調(diào)用實(shí)現(xiàn)類對(duì)象中的方法,接收結(jié)果
List<Student> list = mapper.selectAll();
//6.處理結(jié)果
for (Student student : list) {
System.out.println(student);
}
//7.釋放資源
sqlSession.close();
is.close();
}
@Test
public void insert() throws Exception{
//1.加載核心配置文件
InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
//2.獲取SqlSession工廠對(duì)象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//3.通過工廠對(duì)象獲取SqlSession對(duì)象
SqlSession sqlSession = sqlSessionFactory.openSession(true);
//4.獲取StudentMapper接口的實(shí)現(xiàn)類對(duì)象
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
//5.調(diào)用實(shí)現(xiàn)類對(duì)象中的方法,接收結(jié)果
Student stu = new Student(4,"趙六",26);
Integer result = mapper.insert(stu);
//6.處理結(jié)果
System.out.println(result);
//7.釋放資源
sqlSession.close();
is.close();
}
@Test
public void update() throws Exception{
//1.加載核心配置文件
InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
//2.獲取SqlSession工廠對(duì)象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//3.通過工廠對(duì)象獲取SqlSession對(duì)象
SqlSession sqlSession = sqlSessionFactory.openSession(true);
//4.獲取StudentMapper接口的實(shí)現(xiàn)類對(duì)象
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
//5.調(diào)用實(shí)現(xiàn)類對(duì)象中的方法,接收結(jié)果
Student stu = new Student(4,"趙六",36);
Integer result = mapper.update(stu);
//6.處理結(jié)果
System.out.println(result);
//7.釋放資源
sqlSession.close();
is.close();
}
@Test
public void delete() throws Exception{
//1.加載核心配置文件
InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
//2.獲取SqlSession工廠對(duì)象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//3.通過工廠對(duì)象獲取SqlSession對(duì)象
SqlSession sqlSession = sqlSessionFactory.openSession(true);
//4.獲取StudentMapper接口的實(shí)現(xiàn)類對(duì)象
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
//5.調(diào)用實(shí)現(xiàn)類對(duì)象中的方法,接收結(jié)果
Integer result = mapper.delete(4);
//6.處理結(jié)果
System.out.println(result);
//7.釋放資源
sqlSession.close();
is.close();
}
}xml
<?xml version="1.0" encoding="UTF-8" ?>
<!--MyBatis的DTD約束-->
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<!--configuration 核心根標(biāo)簽-->
<configuration>
<!--引入數(shù)據(jù)庫連接的配置文件-->
<properties resource="jdbc.properties"/>
<!--配置LOG4J-->
<settings>
<setting name="logImpl" value="log4j"/>
</settings>
<!--起別名-->
<typeAliases>
<package name="com.yyl.bean"/>
</typeAliases>
<!--environments配置數(shù)據(jù)庫環(huán)境,環(huán)境可以有多個(gè)。default屬性指定使用的是哪個(gè)-->
<environments default="mysql">
<!--environment配置數(shù)據(jù)庫環(huán)境 id屬性唯一標(biāo)識(shí)-->
<environment id="mysql">
<!-- transactionManager事務(wù)管理。 type屬性,采用JDBC默認(rèn)的事務(wù)-->
<transactionManager type="JDBC"></transactionManager>
<!-- dataSource數(shù)據(jù)源信息 type屬性 連接池-->
<dataSource type="POOLED">
<!-- property獲取數(shù)據(jù)庫連接的配置信息 -->
<property name="driver" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
<!--配置映射關(guān)系-->
<mappers>
<package name="com.yyl.mapper"/>
</mappers>
</configuration>到此這篇關(guān)于Mybatis示例講解注解開發(fā)中的單表操作的文章就介紹到這了,更多相關(guān)Mybatis單表操作內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- MyBatis注解實(shí)現(xiàn)動(dòng)態(tài)SQL問題
- Mybatis基于MapperScan注解的動(dòng)態(tài)代理加載機(jī)制詳解
- MyBatis關(guān)閉一級(jí)緩存的兩種方式(分注解和xml兩種方式)
- Mybatis插件+注解實(shí)現(xiàn)數(shù)據(jù)脫敏方式
- Mybatis中@Param注解的用法詳解
- mybatis中@Param注解總是報(bào)取不到參數(shù)問題及解決
- Mybatis如何實(shí)現(xiàn)@Select等注解動(dòng)態(tài)組合SQL語句
- MyBatis注解CRUD與執(zhí)行流程深入探究
相關(guān)文章
MyBatis-Plus實(shí)現(xiàn)分頁的方法使用詳解
這篇文章主要為大家介紹了MyBatis-Plus的分頁的方法使用,包括:不傳參數(shù)時(shí)的默認(rèn)結(jié)果、查詢不存在的數(shù)據(jù)、手動(dòng)包裝page和自定義SQL,需要的可以參考一下2022-03-03
SpringCloud對(duì)服務(wù)內(nèi)某個(gè)client進(jìn)行單獨(dú)配置的操作步驟
我們的微服務(wù)項(xiàng)目用的是springCloud,某個(gè)微服務(wù)接口因?yàn)閿?shù)據(jù)處理量大,出現(xiàn)了接口超時(shí)的情況,我們需要單獨(dú)修改這一個(gè)feignClient的超時(shí)時(shí)間,所以本文介紹了SpringCloud對(duì)服務(wù)內(nèi)某個(gè)client進(jìn)行單獨(dú)配置的操作步驟,需要的朋友可以參考下2023-10-10
淺談SpringBoot2.4 配置文件加載機(jī)制大變化
這篇文章主要介紹了淺談SpringBoot2.4 配置文件加載機(jī)制大變化,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
Spring Bean生命周期之Bean元信息的配置與解析階段詳解
這篇文章主要為大家詳細(xì)介紹了Spring Bean生命周期之Bean元信息的配置與解析階段,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03
java線程池ThreadPoolExecutor類使用小結(jié)
這篇文章主要介紹了java線程池ThreadPoolExecutor類使用,本文主要對(duì)ThreadPoolExecutor的使用方法進(jìn)行一個(gè)詳細(xì)的概述,示例代碼介紹了ThreadPoolExecutor的構(gòu)造函數(shù)的相關(guān)知識(shí),感興趣的朋友一起看看吧2022-03-03
Maven配置文件修改及導(dǎo)入第三方j(luò)ar包的實(shí)現(xiàn)
本文主要介紹了Maven配置文件修改及導(dǎo)入第三方j(luò)ar包的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-08-08
SpringBoot配置Redis自定義過期時(shí)間操作
這篇文章主要介紹了SpringBoot配置Redis自定義過期時(shí)間操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07

