SpringBoot利用jpa連接MySQL數(shù)據(jù)庫的方法
添加依賴
在pom文件中添加如下依賴
<!--添加Jpa依賴--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!--數(shù)據(jù)庫鏈接驅動--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
配置數(shù)據(jù)庫連接
在application.yml中添加數(shù)據(jù)庫連接配置
# 應用服務 WEB 訪問端口 server: port: 8080 # 應用名稱 spring: application: name: HelloSpringBoot #數(shù)據(jù)庫配置連接 datasource: url: jdbc:mysql://127.0.0.1:3306/hello_jpa?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai username: root password: "123456" driver-class-name: com.mysql.cj.jdbc.Driver jpa: show-sql: true
配置上數(shù)據(jù)庫的連接地址和賬號密碼
url: jdbc:mysql://127.0.0.1:3306/hello_jpa??useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
數(shù)據(jù)庫連接地址
127.0.0.1表示本地
3306數(shù)據(jù)庫端口號
hello_jpa數(shù)據(jù)庫名稱
useUnicode=true&characterEncoding=utf-8配置數(shù)據(jù)庫連接使用UTF8編碼
serverTimezone=Asia/Shanghai配置時間為北京時間
username 配置數(shù)據(jù)庫名稱 root
password 配置數(shù)據(jù)庫密碼 123456
driver-class-name 配置mysql的驅動 com.mysql.cj.jdbc.Driver
show-sql配置控制臺是否打印訪問數(shù)據(jù)庫時的sql語句,配置為true,顯示sql語句,方便調試。
啟動項目,看到如下信息,表示數(shù)據(jù)庫連接成功,如果顯示錯誤,請查驗是否數(shù)據(jù)庫連接信息有錯誤。
創(chuàng)建測試數(shù)據(jù)庫表
CREATE TABLE `jpa_user` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '自增主鍵', `username` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '用戶名稱', `age` tinyint(4) NULL DEFAULT NULL COMMENT '年齡', `gender` tinyint(4) NULL DEFAULT NULL COMMENT '性別0女 1男', `phone` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '手機號', PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
Java代碼部分
建實體類和Repository
jpa_user表對應實體類User
//引入lombok的getset注解,也可以不引入,自己寫下面字段的對應getset方法 @Getter @Setter //注解此類為實體類 @Entity //關聯(lián)數(shù)據(jù)庫表的名稱 @Table(name = "jpa_user") public class User { //聲明屬性為主鍵 @Id //指定主鍵生成策略 @GeneratedValue(strategy = GenerationType.IDENTITY) // @Column注解,設置屬性關聯(lián)的數(shù)據(jù)庫表字段 // 注意:如果屬性名和表字段名相同,可以不設置,比如這個實體類對應的所有字段就都是與數(shù)據(jù)庫表字段相同的,都可以不設置Column注解 @Column(name = "id") private Long id; @Column(name = "username") private String username; @Column(name = "age") private Integer age; @Column(name = "gender") private Integer gender; @Column(name = "phone") private String phone; //駝峰命名法和數(shù)據(jù)庫中的下劃線字段是對應的 @Column(name = "access_card_number") private String accessCardNumber; }
創(chuàng)建UserRepository,數(shù)據(jù)庫操作類
public interface UserRepository extends JpaRepository<User, Long> { }
是的,這個接口沒有定義方法,只要繼承JpaRepository就可以,<User, Long>分別對應的要連接的實體類和主鍵類型。
JpaRepository內置好了基本的增刪查改接口及排序和分頁功能。
做個簡單的代碼測試
往數(shù)據(jù)庫中插入數(shù)據(jù)
//insert into jpa_user (access_card_number, age, gender, phone, username) values (?, ?, ?, ?, ?) User user = new User(); user.setUsername("test"); user.setPhone("13112345678"); user.setAge(18); user.setGender(0); user.setAccessCardNumber("ic_001"); userRepository.save(user);
根據(jù)主鍵id查找數(shù)據(jù)
//select * from jpa_user user where user.id=? User user = userRepository.findById(id).orElse(null);
修改id為1的數(shù)據(jù)
//update jpa_user set access_card_number=?, age=?, gender=?, phone=?, username=? where id=? User user = userRepository.findById(1L).orElse(null); user.setPhone("13212345678"); userRepository.save(user);
刪除id為1的數(shù)據(jù)
//update jpa_user set access_card_number=?, age=?, gender=?, phone=?, username=? where id=? User user = userRepository.findById(1L).orElse(null); user.setPhone("13212345678"); userRepository.save(user);
where語句與查找數(shù)據(jù)
在UserRepository中定義一個方法
//select * from jpa_user user where (user.age between ? and ?) and user.gender=? List<User> findByAgeBetweenAndGender(int minAge, int maxAge, int gender);
在repository中我們可以自定義查找條件的方法
findBy前綴為查找實體數(shù)據(jù),countBy前綴表示查找實體數(shù)量,deleteBy前綴為刪除數(shù)據(jù)
這三種后面跟隨的都是查找條件,多條件用and或or連接
比如上面的例子就是搜索(age between minAge and maxAge) and gender = gender
搜索條件支持定向查找,模糊查找,比較查找等等。
測試代碼地址:HelloSpringBoot: SpringBoot測試項目
到此這篇關于SpringBoot利用jpa連接MySQL數(shù)據(jù)庫的文章就介紹到這了,更多相關SpringBoot連接MySQL內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot集成Druid連接池連接MySQL8.0.11
這篇博客簡單介紹spring boot集成druid連接池的簡單配置和注意事項,文中通過示例代碼介紹的非常詳細,需要的朋友們下面隨著小編來一起學習學習吧2021-07-07SpringMVC 中HttpMessageConverter簡介和Http請求415 的問題
本文介紹且記錄如何解決在SpringMVC 中遇到415 Unsupported Media Type 的問題,并且順便介紹Spring MVC的HTTP請求信息轉換器HttpMessageConverter2016-07-07使用@ConfigurationProperties注解獲取為null的解決方法
在SpringBoot中,當想需要獲取到配置文件數(shù)據(jù)時,除了可以用 Spring 自帶的@Value注解外,SpringBoot還提供了一種更加方便的方式:@ConfigurationProperties,但我們在通過通過get方法去取值一直為null,本文介紹了使用@ConfigurationProperties注解獲取為null的解決方法2024-09-09Spring的@Validation和javax包下的@Valid區(qū)別以及自定義校驗注解
這篇文章主要介紹了Spring的@Validation和javax包下的@Valid區(qū)別以及自定義校驗注解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-01-01PowerJob的TransportServiceAware工作流程源碼解讀
這篇文章主要介紹了PowerJob的TransportServiceAware工作流程源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-01-01sonar-scanner連接sonarquebe7的sonar.java.binaries問題的解決方案
今天小編就為大家分享一篇關于sonar-scanner連接sonarquebe7的sonar.java.binaries問題的解決方案,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12springboot項目整合mybatis并配置mybatis中間件的實現(xiàn)
這篇文章主要介紹了springboot項目整合mybatis并配置mybatis中間件的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-04-04