SpringBoot整合數(shù)據(jù)庫(kù)訪問(wèn)層的實(shí)戰(zhàn)
一、springboot整合使用JdbcTemplate
1.pom依賴
<!--SpringBoot整合jdbc模板框架-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--SpringBoot整合mysql驅(qū)動(dòng)類-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
2.application.yml新增配置
spring:
datasource:
url: jdbc:mysql://localhost:3306/test
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
3.建表sql
CREATE TABLE `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(32) NOT NULL COMMENT '用戶名稱', `age` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
4.UserService
@RestController
public class UserService {
@Autowired
private JdbcTemplate jdbcTemplate;
@RequestMapping("/insertUser")
public String insertUser(String name,Integer age){
int update = jdbcTemplate.update("insert into users values(null,?,?);", name, age);
return update > 0 ? "success" : "false";
}
}
5.瀏覽器訪問(wèn)
http://127.0.0.1:8080/insertUser?name=你好&age=21


二、整合mybatis框架查詢
1.pom依賴
<!-- springboot 整合mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
2.實(shí)體類UserEntity
public class UserEntity {
private String userName;
private Integer age;
private Integer id;
//GET...SET...省略
}
3.UserMapper接口
public interface UserMapper {
@Select("select id as id,name as userName,age as age from users where id = #{id};")
UserEntity selectByUserId(@Param("id") Integer id);
}
4.UserService
@RestController
public class UserService {
@Autowired
private UserMapper userMapper;
@RequestMapping("/mybatisFindById")
public UserEntity mybatisFindById(Integer id){
return userMapper.selectByUserId(id);
}
}
5.app啟動(dòng)
注意:這里需要加注解@MapperScan("com.sjyl.mapper")來(lái)告訴spring接口mapper的掃包范圍
@SpringBootApplication
@MapperScan("com.sjyl.mapper")
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class);
}
}
測(cè)試連接:http://127.0.0.1:8080/mybatisFindById?id=3

三、整合mybatis框架插入
1.UserMapper添加insertUser
public interface UserMapper {
@Insert("Insert into users values (null,#{userName},#{age});")
int insertUser(@Param("userName") String userName,@Param("age") Integer age);
@Select("select id as id,name as userName,age as age from users where id = #{id};")
UserEntity selectByUserId(@Param("id") Integer id);
}
2.UserService添加insertUserMybatis
@RestController
public class UserService {
@Autowired
private UserMapper userMapper;
@RequestMapping("/mybatisFindById")
public UserEntity mybatisFindById(Integer id){
return userMapper.selectByUserId(id);
}
@RequestMapping("/insertUserMybatis")
public String insertUserMybatis(String userName,Integer age){
int insert = userMapper.insertUser(userName,age);
return insert > 0 ? "success" : "false";
}
}
測(cè)試連接:http://127.0.0.1:8080/insertUserMybatis?userName=%E5%BC%A0%E4%B8%89&age=21

到此這篇關(guān)于SpringBoot整合數(shù)據(jù)庫(kù)訪問(wèn)層的實(shí)戰(zhàn)的文章就介紹到這了,更多相關(guān)SpringBoot 數(shù)據(jù)庫(kù)訪問(wèn)層內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot使用MyBatis-Flex實(shí)現(xiàn)靈活的數(shù)據(jù)庫(kù)訪問(wèn)
- SpringBoot中使用JdbcTemplate訪問(wèn)Oracle數(shù)據(jù)庫(kù)的案例詳解
- SpringBoot訪問(wèn)MongoDB數(shù)據(jù)庫(kù)的兩種方式
- SpringBoot使用JdbcTemplate訪問(wèn)操作數(shù)據(jù)庫(kù)基本用法
- SpringBoot快速整合Mybatis、MybatisPlus(代碼生成器)實(shí)現(xiàn)數(shù)據(jù)庫(kù)訪問(wèn)功能
- SpringBoot如何訪問(wèn)不同的數(shù)據(jù)庫(kù)的方法實(shí)現(xiàn)
相關(guān)文章
SpringBoot+Prometheus+Grafana實(shí)現(xiàn)應(yīng)用監(jiān)控和報(bào)警的詳細(xì)步驟
這篇文章主要介紹了SpringBoot+Prometheus+Grafana實(shí)現(xiàn)應(yīng)用監(jiān)控和報(bào)警的詳細(xì)步驟,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02
Java中如何利用Set判斷List集合中是否有重復(fù)元素
在開(kāi)發(fā)工作中,我們有時(shí)需要去判斷List集合中是否含有重復(fù)的元素,這時(shí)候我們不需要找出重復(fù)的元素,我們只需要返回一個(gè)?Boolean?類型就可以了,下面通過(guò)本文給大家介紹Java中利用Set判斷List集合中是否有重復(fù)元素,需要的朋友可以參考下2023-05-05
基于Spring Data的AuditorAware審計(jì)功能的示例代碼
這篇文章主要介紹了基于Spring Data的AuditorAware審計(jì)功能的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
Spring Data Envers支持有條件變動(dòng)紀(jì)錄的保存和查詢的方法
通過(guò)spring-data-envers可以很容易的實(shí)現(xiàn)數(shù)據(jù)變動(dòng)紀(jì)錄的保存和查詢,本文介紹支持有條件變動(dòng)紀(jì)錄的保存和查詢的方法,通過(guò)spring-data-envers很容易的實(shí)現(xiàn)變動(dòng)紀(jì)錄的保存和查詢,只需要增加幾個(gè)注解就可以,感興趣的朋友跟隨小編一起看看吧2023-10-10
Netty源碼分析NioEventLoop處理IO事件相關(guān)邏輯
這篇文章主要介紹了Netty源碼分析NioEventLoop處理IO事件相關(guān)邏輯,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03
java實(shí)現(xiàn)幸運(yùn)抽獎(jiǎng)系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)幸運(yùn)抽獎(jiǎng)系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07
SpringBoot項(xiàng)目發(fā)送釘釘消息功能實(shí)現(xiàn)
在工作中的一些告警需要發(fā)送釘釘通知,有的是發(fā)給個(gè)人,有的要發(fā)到群里,這時(shí)項(xiàng)目就需要接入釘釘,實(shí)現(xiàn)發(fā)消息的功能,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-02-02

