SpringBoot實(shí)現(xiàn)ORM操作MySQL的幾種方法
使用mybatis框架操作數(shù)據(jù),在springboot框架中集成mybatis
使用步驟:
mybatis起步依賴:完成mybatis對(duì)象自動(dòng)配置,對(duì)象放在容器中。
<dependencies>
<!-- web起步依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mybaitis起步依賴-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<!-- mysql驅(qū)動(dòng)-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- 測試-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
pom.xml指定把src/main/java目錄中的xml文件包含到classpath中。
?? ?<build> <!--?? ??? ?resources插件--> ?? ??? ?<resources> ?? ??? ??? ?<resource> ?? ??? ??? ??? ?<directory>src/main/java</directory> ?? ??? ??? ??? ?<includes> ?? ??? ??? ??? ??? ?<include>**/*.xml</include> ?? ??? ??? ??? ?</includes> ?? ??? ??? ?</resource> ?? ??? ?</resources> ?? ??? ?<plugins> ?? ??? ??? ?<plugin> ?? ??? ??? ??? ?<groupId>org.springframework.boot</groupId> ?? ??? ??? ??? ?<artifactId>spring-boot-maven-plugin</artifactId> ?? ??? ??? ?</plugin> ?? ??? ?</plugins> ?? ?</build>
創(chuàng)建實(shí)體類Studnet
創(chuàng)建Dao接口StudentDao,創(chuàng)建一個(gè)查詢學(xué)生的方法。
/**
?* @Mapper :告訴MyBatis這是dao接口,創(chuàng)建此接口的代理對(duì)象,
?* ? ? ?位置:在類的上面。
?* **/
@Mapper
public interface StudentDao {
? ? Student selectById(@Param("stuId") Integer id);
}創(chuàng)建Dao接口對(duì)應(yīng)的Mapper文件,xml文件,寫sql語句。
/**
?* @Mapper :告訴MyBatis這是dao接口,創(chuàng)建此接口的代理對(duì)象,
?* ? ? ?位置:在類的上面。
?* **/
@Mapper
public interface StudentDao {
? ? Student selectById(@Param("stuId") Integer id);
}<?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.firewolf.dao.StudentDao">
<!-- ? ?定義sql語句-->
? ? <select id="selectById" resultType="com.firewolf.model.Student">
? ? ? ? select id,name,age from student where id=#{stuId}
? ? </select>
</mapper>創(chuàng)建servlet層對(duì)象,創(chuàng)建StudentService接口和它的實(shí)現(xiàn)類。去調(diào)用dao對(duì)象的方法,完成數(shù)據(jù)庫的操作。
package com.firewolf.service;
public interface StudentService {
? ? Student queryStudent(Integer id);
}
package com.firewolf.service.impl;
@Service
public class StudentServiceImpl implements StudentService {
? ? @Resource
? ? private StudentDao studentDao;
? ? @Override
? ? public Student queryStudent(Integer id) {
? ? ? ? Student student=studentDao.selectById(id);
? ? ? ? return student;
? ? }
}創(chuàng)建Controller對(duì)象,訪問Service。
@Controller
public class StudentController {
? ? @Resource
? ? private StudentService studentService;
? ? @RequestMapping("/student/query")
? ? @ResponseBody
? ? public String queryStudent(Integer id){
? ? ? ? Student student = studentService.queryStudent(id);
? ? ? ? return student.toString();
? ? }
}寫application.properties文件。
配置數(shù)據(jù)庫的連接信息
server.port=9001 server.servlet.context-path=/orm # 連接數(shù)據(jù)庫 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/springdb?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8 spring.datasource.username=root spring.datasource.password=991231gao ?
1.第一種方式:@Mapper
@Mapper:放在dao接口的上面,每個(gè)接口都需要使用這個(gè)注解。
/**
?* @Mapper :告訴MyBatis這是dao接口,創(chuàng)建此接口的代理對(duì)象,
?* ? ? ?位置:在類的上面。
?* **/
@Mapper
public interface StudentDao {
? ? Student selectById(@Param("stuId") Integer id);
}2.第二種方式 @MapperScan
/**
?* @MapperScan : 找到Dao接口和Mapper文件。
?*?? ??? ? basePackages:dao接口所在的包名
?* **/
@SpringBootApplication
@MapperScan(basePackages = {"com.firewolf.dao","com.firewolf.mapper"})
public class Application {
?? ?public static void main(String[] args) {
?? ??? ?SpringApplication.run(Application.class, args);
?? ?}
}3.第三種方式:Mapper文件和Dao接口分開管理
現(xiàn)在把Mapper文件放在resources
- 在resources目錄中創(chuàng)建子目錄,例如mapper
- 把mapper文件放到mapper目錄中。
- 在application.properties文件中,指定mapper文件的目錄。
# 指定mapper文件的位置 mybatis.mapper-locations=classpath:mapper/*.xml # mybaitis的日志 mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
在pom.xml中指定目錄,把resources目錄中的文件,編譯到目標(biāo)目錄中。
<!--?? ??? ?resources插件--> ?? ??? ?<resources> ?? ??? ??? ?<resource> ?? ??? ??? ??? ?<directory>src/main/resources</directory> ?? ??? ??? ??? ?<includes> ?? ??? ??? ??? ??? ?<include>**/*.*</include> ?? ??? ??? ??? ?</includes> ?? ??? ??? ?</resource> ?? ??? ?</resources>
4.事務(wù)
spring框架中的事務(wù)
管理事務(wù)的對(duì)象:事務(wù)管理器(接口,接口有很多的實(shí)現(xiàn)類)。
例如:使用jdbc或mybatis訪問數(shù)據(jù)庫,使用事務(wù)管理器:DataSourceTransactionManager
聲明式事務(wù):在xml配置文件或者使用注解說明事務(wù)控制的內(nèi)容。
控制事務(wù):隔離級(jí)別,傳播行為,超時(shí)時(shí)間。
事務(wù)處理方式
- spring框架中的@Transactional
- aspectj框架可以在xml配置文件中,聲明事務(wù)控制的內(nèi)容。
springboot中使用事務(wù):上面的兩種方式都可以。
- 在業(yè)務(wù)方法的上面加入@Transactional,加入注解后,方法有事務(wù)功能了。
- 明確在主啟動(dòng)類的上面,加入@EnableTransactionManager。
@SpringBootApplication
@EnableTransactionManagement
@MapperScan(value="com.firewolf.dao")
public class Application {
? ?public static void main(String[] args) {
? ??? ??? ?SpringApplication.run(Application.class, args);
? ?}
}例子:
/**
?* @Transactional: 表示方法的有事務(wù)支持
?* ? ? ? 默認(rèn):使用庫的隔離級(jí)別, REQUIRED 傳播行為; 超時(shí)時(shí)間 ?-1
?* ? ? ? 拋出運(yùn)行時(shí)異常,回滾事務(wù)
?*/
@Transactional
@Override
public int addStudent(Student student) {
? ? System.out.println("業(yè)務(wù)方法addStudent");
? ? int rows ?= ?studentDao.insert(student);
? ? System.out.println("執(zhí)行sql語句");
? ? //拋出一個(gè)運(yùn)行時(shí)異常, 目的是回滾事務(wù)
? ? //int m ? = 10 / 0 ;
? ? return rows;
}到此這篇關(guān)于SpringBoot實(shí)現(xiàn)ORM操作MySQL的幾種方法的文章就介紹到這了,更多相關(guān)SpringBoot ORM操作MySQL內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java設(shè)計(jì)模式之裝飾者模式詳解和代碼實(shí)例
這篇文章主要介紹了Java設(shè)計(jì)模式之裝飾者模式詳解和代碼實(shí)例,Decorator模式(別名Wrapper):動(dòng)態(tài)將職責(zé)附加到對(duì)象上,若要擴(kuò)展功能,裝飾者提供了比繼承更具彈性的代替方案,需要的朋友可以參考下2014-09-09
解決dubbo啟動(dòng)報(bào)服務(wù)注冊(cè)失敗Failed?to?register?dubbo
這篇文章主要介紹了解決dubbo啟動(dòng)報(bào)服務(wù)注冊(cè)失敗Failed?to?register?dubbo問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
SpringBoot實(shí)現(xiàn)數(shù)據(jù)預(yù)熱的方式小結(jié)
這里用到的數(shù)據(jù)預(yù)熱,就是在項(xiàng)目啟動(dòng)時(shí)將一些數(shù)據(jù)量較大的數(shù)據(jù)加載到緩存中(筆者這里用的Redis),那么在項(xiàng)目啟動(dòng)有哪些方式可以實(shí)現(xiàn)數(shù)據(jù)預(yù)熱呢,本文就來給大家講講幾種實(shí)現(xiàn)數(shù)據(jù)預(yù)熱的方式,需要的朋友可以參考下2023-09-09
解決SpringBoot整合Mybatis掃描不到Mapper的問題
這篇文章主要介紹了解決SpringBoot整合Mybatis掃描不到Mapper的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04
Maven遠(yuǎn)程倉庫地址修改實(shí)現(xiàn)解析
這篇文章主要介紹了Maven遠(yuǎn)程倉庫地址修改實(shí)現(xiàn)解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
SpringBoot啟動(dòng)時(shí)運(yùn)行特定代碼的多種方式小結(jié)
SpringBoot提供了多種方式在應(yīng)用程序啟動(dòng)時(shí)運(yùn)行特定的代碼,包括CommandLineRunner、ApplicationRunner、@PostConstruct、InitializingBean、事件機(jī)制和自定義注解等,下面就來具體介紹一下2025-01-01
IntelliJ?IDEA的代碼擱置功能實(shí)現(xiàn)
本文主要介紹了IntelliJ?IDEA的代碼擱置功能實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
基于Springboot+Mybatis對(duì)數(shù)據(jù)訪問層進(jìn)行單元測試的方式分享
本文將介紹一種快高效、可復(fù)用的解決測試方案——對(duì)數(shù)據(jù)訪問層做單元測試,文章通過代碼示例介紹的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下2023-07-07

