SpringBoot入門系列之JPA mysql
一,準(zhǔn)備工作,建立spring-boot-sample-mysql工程
A、Artifact中輸入spring-boot-sample-MySQL
B、勾選Web下的web
C、勾選SQL下的JPA MYSQL
2、Eclips中導(dǎo)入工程spring-boot-sample-mysql
A、解壓快捷工程spring-boot-sample-mysql到某文件夾
B、eclips中file->import->Import Existing Maven Projects-->Select Maven projects-->finish導(dǎo)入工程
3、工程導(dǎo)入之后,文件結(jié)構(gòu)如下圖
4、在包c(diǎn)om.example下建立web文件夾
5、便于測(cè)試,引入spring-boot-sample-helloworld的HelloController及配置文件logback.xml
HelloController代碼為
package com.example.web; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { protected static Logger logger=LoggerFactory.getLogger(HelloController.class); @RequestMapping("/") public String helloworld(){ logger.debug("訪問(wèn)hello"); return "Hello world!"; } @RequestMapping("/hello/{name}") public String helloName(@PathVariable String name){ logger.debug("訪問(wèn)helloName,Name={}",name); return "Hello "+name; } }
logback.xml配置為
<configuration> <!-- %m輸出的信息,%p日志級(jí)別,%t線程名,%d日期,%c類的全名,,,, --> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d %p (%file:%line\)- %m%n</pattern> <charset>GBK</charset> </encoder> </appender> <appender name="baselog" class="ch.qos.logback.core.rolling.RollingFileAppender"> <File>log/base.log</File> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>log/base.log.%d.i%</fileNamePattern> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <!-- or whenever the file size reaches 64 MB --> <maxFileSize>64 MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> <encoder> <pattern> %d %p (%file:%line\)- %m%n </pattern> <charset>UTF-8</charset> <!-- 此處設(shè)置字符集 --> </encoder> </appender> <root level="info"> <appender-ref ref="STDOUT" /> </root> <logger name="com.example" level="DEBUG"> <appender-ref ref="baselog" /> </logger> </configuration>
注:logback.xml文件位于src/main/resources下
6、啟動(dòng)工程,通過(guò)瀏覽器查看正確性
http://localhost:8080/hello/上帝
二,使用JPA,構(gòu)建業(yè)務(wù)對(duì)象及訪問(wèn)庫(kù)
1、在包c(diǎn)om.example下建立domain文件夾
2、在domain中建立類Person
package com.example.domain; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity public class Person { @Id @GeneratedValue private Long id; private String name; private Integer age; private String address; public Person() { super(); } public Person(Long id, String name, Integer age, String address) { super(); this.id = id; this.name = name; this.age = age; this.address = address; } 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 Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
注意:構(gòu)造函數(shù)
3、在包c(diǎn)om.example下建立repository文件夾
4、在repository中建立接口PersonRepository
package com.example.repository; import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; import com.example.domain.Person; @Repository public interface PersonRepository extends JpaRepository<Person,Long> { List<Person> findByName(String name); List<Person> findByAddress(String address); List<Person> findByNameAndAddress(String name,String address); @Query("select p from Person p where p.name=:name and p.address=:address") List<Person> withNameAndAddressQuery(@Param("name")String Name,@Param("address")String address); }
5、在web中建立DataController
package com.example.web; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort.Direction; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.example.domain.Person; import com.example.repository.PersonRepository; @RestController public class DataController { protected static Logger logger=LoggerFactory.getLogger(DataController.class); @Autowired PersonRepository personRepository; @RequestMapping("/save") public Person save(String name,String address,Integer age){ logger.debug("save 開始"); Person p=personRepository.save(new Person(null,name,age,address)); logger.debug("save 結(jié)束"); return p; } @RequestMapping("/q1") public List<Person> q1(String address){ logger.debug("q1 開始"); logger.debug("q1 接收參數(shù)address={}",address); List<Person> people=personRepository.findByAddress(address); return people; } @RequestMapping("/q2") public List<Person> q2(String name,String address){ logger.debug("q2 開始"); logger.debug("q2接收參數(shù)name={},address={}",name,address); return personRepository.findByNameAndAddress(name, address); } @RequestMapping("/q3") public List<Person> q3(String name,String address){ logger.debug("q3 開始"); logger.debug("q3接收參數(shù)name={},address={}",name,address); return personRepository.withNameAndAddressQuery(name, address); } @RequestMapping("/sort") public List<Person> sort(){ logger.debug("sort 開始"); List<Person> people=personRepository.findAll(new Sort(Direction.ASC,"age")); return people; } @RequestMapping("/page") public Page<Person> page(){ logger.debug("page 開始"); Page<Person> people=personRepository.findAll(new PageRequest(1,2)); return people; } }
6、配置數(shù)據(jù)庫(kù)連接,在application.properties(src/main/resources下)
spring.datasource.url=jdbc:mysql://192.168.56.201:3306/bootsample?useUnicode=true&characterEncoding=UTF-8 spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true spring.jackson.serialization.indent_output=true
7、運(yùn)行測(cè)試
A、先保存數(shù)據(jù)
http://localhost:8080/save?name=aa&&address=北京&&age=1
http://localhost:8080/save?name=ab&&address=北京&&age=2
http://localhost:8080/save?name=cq1&&address=重慶&&age=50
http://localhost:8080/save?name=cq2&&address=重慶&&age=51
B、查詢q1
http://localhost:8080/q1?address=北京
C、查詢q2
http://localhost:8080/q2?address=北京&&name=aa
D、查詢q3
http://localhost:8080/q3?address=北京&&name=aa
E、排序
F、分頁(yè)
運(yùn)用hibernate訪問(wèn)mysql,基本也是老技術(shù),只是用JPA簡(jiǎn)化了dao層代碼,對(duì)于業(yè)務(wù)對(duì)象基本沒有變化。
以上所述是小編給大家介紹的SpringBoot入門系列之JPA mysql,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Springboot2.0配置JPA多數(shù)據(jù)源連接兩個(gè)mysql數(shù)據(jù)庫(kù)方式
- springboot 中 druid+jpa+MYSQL數(shù)據(jù)庫(kù)配置過(guò)程
- 解決springboot的JPA在Mysql8新增記錄失敗的問(wèn)題
- SpringBoot+MySQL+Jpa實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)的增刪改查和分頁(yè)詳解
- springboot使用spring-data-jpa操作MySQL數(shù)據(jù)庫(kù)
- SpringBoot連接MYSQL數(shù)據(jù)庫(kù)并使用JPA進(jìn)行操作
- springboot整合JPA訪問(wèn)Mysql的實(shí)現(xiàn)方法
相關(guān)文章
springboot學(xué)習(xí)之構(gòu)建簡(jiǎn)單項(xiàng)目搭建步驟詳解
這篇文章主要介紹了springboot學(xué)習(xí)之構(gòu)建簡(jiǎn)單項(xiàng)目搭建步驟詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-10-10淺談一個(gè)基礎(chǔ)的SpringBoot項(xiàng)目該包含哪些
這篇文章主要介紹了淺談一個(gè)基礎(chǔ)的SpringBoot項(xiàng)目該包含哪些,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10MyBatis中使用分頁(yè)插件PageHelper實(shí)現(xiàn)分頁(yè)功能
分頁(yè)是經(jīng)常使用的功能,本文主要介紹了Mybatis中處理特殊SQL處理邏輯,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06Java中定時(shí)器Timer致命缺點(diǎn)案例詳解
這篇文章主要介紹了Java中定時(shí)器Timer致命缺點(diǎn),以Java中定時(shí)器Time為案例整理下我的學(xué)習(xí)方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-02-02SpringBoot實(shí)現(xiàn)Server-Sent Events(SSE)的使用完整指南
使用SpringBoot實(shí)現(xiàn)Server-Sent Events(SSE)可以有效處理實(shí)時(shí)數(shù)據(jù)推送需求,具有單向通信、輕量級(jí)和高實(shí)時(shí)性等優(yōu)勢(shì),本文詳細(xì)介紹了在SpringBoot中創(chuàng)建SSE端點(diǎn)的步驟,并通過(guò)代碼示例展示了客戶端如何接收數(shù)據(jù),適用于實(shí)時(shí)通知、數(shù)據(jù)展示和在線聊天等場(chǎng)景2024-09-09java判斷域名無(wú)法訪問(wèn)自行訪問(wèn)下一條
這篇文章主要為大家介紹了java實(shí)現(xiàn)判斷域名無(wú)法訪問(wèn)的時(shí)候自行訪問(wèn)下一條域名示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12java模擬實(shí)現(xiàn)銀行ATM機(jī)操作
這篇文章主要為大家詳細(xì)介紹了java模擬實(shí)現(xiàn)銀行ATM機(jī)操作,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05java Timer 定時(shí)每天凌晨1點(diǎn)執(zhí)行任務(wù)
這篇文章主要介紹了java Timer 定時(shí)每天凌晨1點(diǎn)執(zhí)行任務(wù)的代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09