SpringBoot使用spring data jpa

引言: java

在至關長的一段時間內,實現應用程序的數據訪問層一直很麻煩。 必須編寫太多樣板代碼來執行簡單查詢以及執行分頁和審計。 Spring Data JPA旨在經過減小實際須要的工做量來顯著改善數據訪問層的實現。 做爲開發人員,您編寫repository接口,包括自定義查找器方法,Spring將自動提供實現。

Spring Data JPA旨在經過減小實際須要的工做量來顯著改善數據訪問層的實現。 做爲開發人員,您編寫repository接口,包括自定義查找器方法,Spring將自動提供實現。mysql

 

 代碼編寫:web

1.依賴配置:redis

複製代碼
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.wf</groupId> <artifactId>springbootdemo</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.2.RELEASE</version> </parent> <!-- 添加spring mvc 依賴--> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--FreeMarker 模板引擎--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> <!--整合springboot與mybatis的整合--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> <!--測試junit--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--redis緩存--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <!--將mapper下的mapper接口與mapper映射文件放在一個mapper包下所須要的依賴--> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.*</include> </includes> <filtering>false</filtering> </resource> </resources> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
複製代碼

2.resources配置application.properties:spring

複製代碼
#DB Configation
spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=root #JPA Configation spring.jpa.database=MySQL spring.jpa.generate-ddl=true spring.jpa.show-sql=true
複製代碼

若是鏈接不上數據庫,那改爲這段配置sql

複製代碼
#DB Configration
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/數據庫名稱?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT spring.datasource.username=root spring.datasource.password=root #JAPConfigration spring.jpa.database=MySQL spring.jpa.show-sql=true spring.jpa.generate-ddl=true
複製代碼

也能夠使用19年初剛出的新技術spring data jpa(是爺爺輩,jpa是父輩,Hibernate是孫子輩--->已差很少被淘汰)數據庫

 

 

 

3.設置測試controller:apache

複製代碼
package com.wf.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.HashMap; import java.util.Map; @RestController @RequestMapping("/test") public class TestController { @RequestMapping("/hello") public Map sayHell0(){ Map map=new HashMap(); map.put("json數據01","我愛java"); return map; } }
複製代碼

效果圖:json

 

 進入正題:緩存

1.建立數據庫並添加數據:

2.建立對應的pojo對象:

複製代碼
package com.wf.entity; import javax.persistence.*; //標記此類爲實體類 @Entity //標記對應的數據庫中的表 @Table(name = "user") public class User { //設置id爲主鍵 自增  @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; private String username; private String password; private String name; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", password='" + password + '\'' + ", name='" + name + '\'' + '}'; } public User(Integer id, String username, String password, String name) { this.id = id; this.username = username; this.password = password; this.name = name; } public User() { super(); } }
複製代碼

注意:對應的幾個註解須要格外配置好

 

 

3.建立對應的controller接口:

複製代碼
package com.wf.controller; import com.wf.dao.UserDao; import com.wf.entity.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController @RequestMapping("/user") public class UserController { @Autowired private UserDao userDao; //查詢出全部數據 @RequestMapping("/list") public List<User> getUserList(){ return userDao.findAll(); } }
複製代碼

4.對應的dao層:

複製代碼
package com.wf.dao; import com.xhn.entity.User; import org.springframework.data.jpa.repository.JpaRepository; public interface UserDao extends JpaRepository<User,Integer> { }
複製代碼

代碼測試:

 

 

 

 

 

 本博主對應的json插件爲:JSON-handle_0.5.6.crx,能夠在網上自行百度

相關文章
相關標籤/搜索