Spring Boot JPA訪問Mysql示例
上篇演示了通過Maven構(gòu)建Spring Boot 項(xiàng)目,引用web模塊啟動(dòng)應(yīng)用,完成簡(jiǎn)單的web 應(yīng)用訪問,本章內(nèi)容在此基礎(chǔ)上面加入數(shù)據(jù)訪問與端口修改,下文代碼與演例(本用例純手工測(cè)試通過,放心入坑)。
修改默認(rèn)端口
在src\main\resources下加入application.properties內(nèi)容如下
server.port=8888
項(xiàng)目目錄結(jié)構(gòu)
啟動(dòng)應(yīng)用,日志顯示:
端口已經(jīng)由默認(rèn)的8080 變更為8888
JPA訪問mysql數(shù)據(jù)庫
1、POM中加入
<!-- Spring Boot JPA --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> <version>RELEASE</version> </dependency> <!-- MYSQL --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
2、在src\test\resources下加入application.properties內(nèi)容如下(正式應(yīng)用中請(qǐng)把配置加入至src\main\resources下application.properties中):
server.port=8888 spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=dbuser spring.datasource.password=dbpass spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.jpa.properties.hibernate.hbm2ddl.auto=create-drop
- create : 會(huì)根據(jù)你的model類來生成表,但是每次運(yùn)行都會(huì)刪除上一次的表,重新生成表,哪怕2次沒有任何改變 (生產(chǎn)禁用,不小心用了會(huì)哭的。。)
- create-drop : 根據(jù)model類生成表,但是sessionFactory一關(guān)閉,表就自動(dòng)刪除 (生產(chǎn)禁用)
- update : 最常用的屬性,也根據(jù)model類生成表,即使表結(jié)構(gòu)改變了,表中的行仍然存在,不會(huì)刪除以前的行
- validate : 只會(huì)和數(shù)據(jù)庫中的表進(jìn)行比較,不會(huì)創(chuàng)建新表,但是會(huì)插入新值
3、新建實(shí)體
src\main\java\com\entity\User.java
package com.entity; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; import java.io.Serializable; @Entity @Table(name="t_user") public class User implements Serializable { private static final long serialVersionUID = -3258839839160856613L; @Id @GeneratedValue private Long id; private String name; private String moblie; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getMoblie() { return moblie; } public void setMoblie(String moblie) { this.moblie = moblie; } }
新建數(shù)據(jù)訪問接口(JPA)
src\main\java\com\dao\UserRepository .java
package com.dao; import entity.User; import org.springframework.data.jpa.repository.JpaRepository; /** * Description: * date: 2017/3/15 16:28 */ public interface UserRepository extends JpaRepository<User, Long> { User findByName(String name); }
從源碼上面可以看出,JpaRepository已經(jīng)實(shí)現(xiàn)了save(更新與保存)、delete、getOne、findAll等方法,所以對(duì)于基礎(chǔ)數(shù)據(jù)的操作,接口上不需要再定義,直接使用就好。
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package org.springframework.data.jpa.repository; import java.io.Serializable; import java.util.List; import org.springframework.data.domain.Example; import org.springframework.data.domain.Sort; import org.springframework.data.repository.NoRepositoryBean; import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.data.repository.query.QueryByExampleExecutor; @NoRepositoryBean public interface JpaRepository<T, ID extends Serializable> extends PagingAndSortingRepository<T, ID>, QueryByExampleExecutor<T> { List<T> findAll(); List<T> findAll(Sort var1); List<T> findAll(Iterable<ID> var1); <S extends T> List<S> save(Iterable<S> var1); void flush(); <S extends T> S saveAndFlush(S var1); void deleteInBatch(Iterable<T> var1); void deleteAllInBatch(); T getOne(ID var1); <S extends T> List<S> findAll(Example<S> var1); <S extends T> List<S> findAll(Example<S> var1, Sort var2); }
4.編寫對(duì)應(yīng)的單元測(cè)試來驗(yàn)證編寫的內(nèi)容是否正確
POM中加入
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency>
創(chuàng)建單元測(cè)試用例
src\test\java\UserTest.java
import com.SampleController; import com.dao.UserRepository; import com.entity.User; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; /** * date: 2017/3/15 17:21 */ @RunWith(SpringRunner.class) @SpringBootTest(classes = SampleController.class) public class UserTest { @Autowired private UserRepository userRepository; @Test public void saveTest() { User user = new User(); user.setName("王大錘"); user.setMoblie("13300000000"); userRepository.save(user); Assert.assertEquals("13300000000", userRepository.findByName("王大錘").getMoblie()); } }
運(yùn)行結(jié)果示例
以上就是本文的全部?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)行操作
- SpringBoot用JdbcTemplates訪問Mysql實(shí)例代碼
- SpringBoot入門系列之JPA mysql
- Spring boot 使用mysql實(shí)例詳解
- 詳解在Spring Boot中使用Mysql和JPA
- Spring Boot高級(jí)教程之Spring Boot連接MySql數(shù)據(jù)庫
相關(guān)文章
java 生成有序賬號(hào)的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄猨ava 生成有序賬號(hào)的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10詳談jvm--Java中init和clinit的區(qū)別
下面小編就為大家?guī)硪黄斦刯vm--Java中init和clinit的區(qū)別。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10JDK8配置環(huán)境變量的bat文件的詳細(xì)教程
這篇文章主要介紹了JDK8配置環(huán)境變量的bat文件,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07關(guān)于注解FeignClient的使用規(guī)范
這篇文章主要介紹了關(guān)于注解FeignClient的使用規(guī)范,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03基于Redis實(shí)現(xiàn)分布式應(yīng)用限流的方法
本篇文章主要介紹了基于 Redis 實(shí)現(xiàn)分布式應(yīng)用限流的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-12-12Java基礎(chǔ)教程之獲取操作系統(tǒng)及瀏覽器信息
最近在開發(fā)中需要從request中獲取操作系統(tǒng),瀏覽器及瀏覽器版本信息,所以下面這篇文章主要給的大家介紹了關(guān)于Java基礎(chǔ)教程之獲取操作系統(tǒng)及瀏覽器信息的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-10-10java后臺(tái)驗(yàn)證碼生成的實(shí)現(xiàn)方法
在我們使用進(jìn)行系統(tǒng)開發(fā)時(shí),為了提高系統(tǒng)的安全性,在登錄的時(shí)候多數(shù)人都會(huì)要求輸入驗(yàn)證,本文介紹了java后臺(tái)驗(yàn)證碼生成的實(shí)現(xiàn)方法,感興趣的一起來了解一下2021-05-05