詳解在Spring Boot中使用Mysql和JPA
本文向你展示如何在Spring Boot的Web應(yīng)用中使用Mysq數(shù)據(jù)庫,也充分展示Spring Boot的優(yōu)勢(shì)(盡可能少的代碼和配置)。數(shù)據(jù)訪問層我們將使用Spring Data JPA和Hibernate(JPA的實(shí)現(xiàn)之一)。
1.Maven pom.xml文件
在你的項(xiàng)目中增加如下依賴文件
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies>
2.屬性配置文件application.properties
在src/main/resources/application.properties中設(shè)置數(shù)據(jù)源和jpa配置。
spring.datasource.url = jdbc:mysql://localhost:3306/test spring.datasource.username = root spring.datasource.password = root spring.datasource.driverClassName = com.mysql.jdbc.Driver # Specify the DBMS spring.jpa.database = MYSQL # Show or not log for each sql query spring.jpa.show-sql = true # Hibernate ddl auto (create, create-drop, update) spring.jpa.hibernate.ddl-auto = update # Naming strategy spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy # stripped before adding them to the entity manager) spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
全部的配置都在如上的文件中了,不需要另外的XML配置和Java配置。
上文中的數(shù)據(jù)庫配置,你需要換成你的數(shù)據(jù)庫的地址和用戶名密碼。
hibernate的ddl-auto=update配置表名,數(shù)據(jù)庫的表和列會(huì)自動(dòng)創(chuàng)建(根據(jù)Java實(shí)體的熟悉), 這里 可以看到更多得hibernate配置。
3.User實(shí)體
創(chuàng)建一個(gè)User實(shí)體,User包含三個(gè)屬性id,email和name。User實(shí)體和Mysql數(shù)據(jù)庫的users表相對(duì)應(yīng)。
@Entity @Table(name = "users") public class User { // ============== // PRIVATE FIELDS // ============== // An autogenerated id (unique for each user in the db) @Id @GeneratedValue(strategy = GenerationType.AUTO) private long id; // The user email @NotNull private String email; // The user name @NotNull private String name; // ============== // PUBLIC METHODS // ============== public User() { } public User(long id) { this.id = id; } // Getter and setter methods // ... } // class User
4.User實(shí)體的數(shù)據(jù)訪問層UserDao
本例中UserDao非常簡(jiǎn)單,只需要繼承CrudRespositroy即可,CrudRespositroy已經(jīng)實(shí)現(xiàn)了save,delete,deleteAll,findOne和findAll.(比較神奇的時(shí)這些方法其實(shí)CrudRespositroy中其實(shí)并沒有實(shí)現(xiàn),并且通過對(duì)dao方法的命名還可以實(shí)現(xiàn)新的方法)
@Transactional public interface UserDao extends CrudRepository<User, Long> { public User findByEmail(String email); }
5.測(cè)試的控制器UserController
新建一個(gè)查詢控制器UserController
@Controller public class UserController { @RequestMapping("/get-by-email") @ResponseBody public String getByEmail(String email) { String userId; User user = userDao.findByEmail(email); if (user != null) { userId = String.valueOf(user.getId()); return "The user id is: " + userId; } return "user " + email + " is not exist."; } }
你可以使用瀏覽器訪問url http://127.0.0.1:8080/get-by-email?email=qiyadeng@gmail.com,可以獲得用戶的Id(你可以先在Mysql數(shù)據(jù)庫中新增一條記錄)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Spring Boot Mysql 數(shù)據(jù)庫操作示例
- Spring Boot如何解決Mysql斷連問題
- Spring Boot 添加MySQL數(shù)據(jù)庫及JPA實(shí)例
- Spring Boot中使用jdbctemplate 操作MYSQL數(shù)據(jù)庫實(shí)例
- SpringBoot連接MYSQL數(shù)據(jù)庫并使用JPA進(jìn)行操作
- Spring Boot JPA訪問Mysql示例
- SpringBoot用JdbcTemplates訪問Mysql實(shí)例代碼
- SpringBoot入門系列之JPA mysql
- Spring boot 使用mysql實(shí)例詳解
- Spring Boot高級(jí)教程之Spring Boot連接MySql數(shù)據(jù)庫
相關(guān)文章
MybatisPlus?QueryWrapper常用方法實(shí)例
MyBatis-Plus(opens new window)是一個(gè)MyBatis(opens new window)的增強(qiáng)工具,在 MyBatis的基礎(chǔ)上只做增強(qiáng)不做改變,為簡(jiǎn)化開發(fā)、提高效率而生,下面這篇文章主要給大家介紹了關(guān)于MybatisPlus?QueryWrapper常用方法的相關(guān)資料,需要的朋友可以參考下2022-04-04java實(shí)現(xiàn)學(xué)生教師管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)學(xué)生教師管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-10-10基于Jenkins自動(dòng)打包并部署docker環(huán)境的操作過程
這篇文章主要介紹了基于Jenkins自動(dòng)打包并部署docker環(huán)境,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08Java實(shí)現(xiàn)學(xué)生成績(jī)管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)學(xué)生成績(jī)管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04如何開啟控制臺(tái)輸出mybatis執(zhí)行的sql日志問題
這篇文章主要介紹了如何開啟控制臺(tái)輸出mybatis執(zhí)行的sql日志問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09Java調(diào)用瀏覽器打開網(wǎng)頁完整實(shí)例
這篇文章主要介紹了Java調(diào)用瀏覽器打開網(wǎng)頁的方法,以完整實(shí)例形式分析了java打開網(wǎng)頁的相關(guān)技巧,需要的朋友可以參考下2015-05-05