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> <!-- 測(cè)試--> <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文件,寫(xiě)sql語(yǔ)句。
/** ?* @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語(yǔ)句--> ? ? <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ù)庫(kù)的操作。
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ì)象,訪問(wèn)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(); ? ? } }
寫(xiě)application.properties文件。
配置數(shù)據(jù)庫(kù)的連接信息
server.port=9001 server.servlet.context-path=/orm # 連接數(shù)據(jù)庫(kù) 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接口分開(kāi)管理
現(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訪問(wèn)數(shù)據(jù)庫(kù),使用事務(wù)管理器:DataSourceTransactionManager
聲明式事務(wù):在xml配置文件或者使用注解說(shuō)明事務(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):使用庫(kù)的隔離級(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語(yǔ)句"); ? ? //拋出一個(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)文章
Spring Boot之內(nèi)嵌tomcat版本升級(jí)操作示例
這篇文章主要為大家介紹了Spring Boot之內(nèi)嵌tomcat版本升級(jí)操作示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06Java數(shù)組的定義、初始化、及二維數(shù)組用法分析
這篇文章主要介紹了Java數(shù)組的定義、初始化、及二維數(shù)組用法,結(jié)合具體實(shí)例形式分析了java數(shù)組概念、功能、數(shù)組定義、靜態(tài)數(shù)組、動(dòng)態(tài)數(shù)組、二維數(shù)組等相關(guān)使用技巧,需要的朋友可以參考下2019-01-01SpringBoot中使用Filter和Interceptor的示例代碼
這篇文章主要介紹了SpringBoot中使用Filter和Interceptor的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06Mybatis分頁(yè)插件PageHelper的分頁(yè)原理剖析
這篇文章主要介紹了Mybatis分頁(yè)插件PageHelper的分頁(yè)原理剖析,PageHelper作為一個(gè)啟動(dòng)器,那么就和其他啟動(dòng)器加載一樣,先讀取spring.factories文件里面配置的類,轉(zhuǎn)成Bean加載本系統(tǒng)中,然后執(zhí)行他的前置后置處理方法,完成初始化,需要的朋友可以參考下2023-08-08關(guān)于eclipse安裝spring插件報(bào)錯(cuò)An error occurred while collecting item
這篇文章主要介紹了關(guān)于eclipse安裝spring插件報(bào)錯(cuò)An error occurred while collecting items to be installed...解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08Java?@Scheduled定時(shí)任務(wù)不執(zhí)行解決辦法
這篇文章主要給大家介紹了關(guān)于Java?@Scheduled定時(shí)任務(wù)不執(zhí)行解決的相關(guān)資料,當(dāng)@Scheduled定時(shí)任務(wù)不執(zhí)行時(shí)可以根據(jù)以下步驟進(jìn)行排查和解決,需要的朋友可以參考下2023-10-10Java基于對(duì)象流實(shí)現(xiàn)銀行系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java基于對(duì)象流實(shí)現(xiàn)銀行系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09ElasticSearch創(chuàng)建后索引修改數(shù)據(jù)類型方法步驟
Elasticsearch存儲(chǔ)數(shù)據(jù)之前需要先創(chuàng)建索引,類似于結(jié)構(gòu)型數(shù)據(jù)庫(kù)建庫(kù)建表,創(chuàng)建索引時(shí)定義了每個(gè)字段的索引方式和數(shù)據(jù)類型,這篇文章主要給大家介紹了關(guān)于ElasticSearch創(chuàng)建后索引修改數(shù)據(jù)類型的方法步驟,需要的朋友可以參考下2023-09-09