當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringBoot JPA
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot JPA
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、創建步驟
二、目錄結構
三、application.properties配置datasource
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.username=root spring.datasource.password=root spring.datasource.url=jdbc:mysql://localhost/springboot?userUnicode=true@characterEncoding=UTF8&serverTimezone=UTC四、domain層創建實體類User
package com.willam.domain;import javax.persistence.*;/*** @author :lijunxuan* @date :Created in 2019/6/28 17:04* @description :* @version: 1.0*/ @Table(name = "user") @Entity public class User {@Id //ID代表是主鍵@GeneratedValue(strategy = GenerationType.IDENTITY) //按照主鍵自增private Integer id;//@Column(name="username") //把數據庫里的名字和當前名字做一個綁定關系private String username;private String password;private String name;@Overridepublic String toString() {return "User{" +"id=" + id +", username='" + username + '\'' +", password='" + password + '\'' +", name='" + 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;} }五、Controller層UserController
package com.willam.Controller;import com.willam.Service.UserService; import com.willam.domain.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;import java.util.List;/*** @author :lijunxuan* @date :Created in 2019/6/28 17:02* @description :* @version: 1.0*/ @RestController @RequestMapping("/user") public class UserController {@AutowiredUserService userService;/*** 查詢所有用戶* @return*/@RequestMapping("/findAll")public List<User> findAll(){return userService.findAll();}/*** 通過ID查詢用戶* @param id* @return*/@RequestMapping("/findById")public User findById(Integer id){return userService.findById(id);}/*** 添加用戶* @param user**/@RequestMapping("/add")public void add(User user){userService.add(user);}/*** 通過用戶ID刪除用戶* @param id**/@RequestMapping("/deleteById")public void deleteById(Integer id){userService.deleteById(id);}}六、service層
(1)UserService接口
package com.willam.Service;import com.willam.domain.User;import java.util.List;public interface UserService {/*** 查詢所有用戶信息* @return*/List<User> findAll();/*** 通過ID查詢用戶* @param id* @return*/User findById(Integer id);/*** 添加用戶* @param user**/void add(User user);/*** 通過用戶ID刪除用戶* @param id**/void deleteById(Integer id); }(2)UserServiceImpl實現類
package com.willam.Service.impl;import com.willam.Dao.UserDao; import com.willam.Service.UserService; import com.willam.domain.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;import java.util.List; import java.util.Optional;/*** @author :lijunxuan* @date :Created in 2019/6/28 17:27* @description :* @version: 1.0*/ @Service public class UserServiceImpl implements UserService {@AutowiredUserDao userDao;@Overridepublic List<User> findAll() {return userDao.findAll();}@Overridepublic User findById(Integer id) {Optional<User> userById = userDao.findById(id);return userById.get();}@Overridepublic void add(User user) {userDao.save(user);}@Overridepublic void deleteById(Integer id) {userDao.deleteById(id);} }七、Dao層
package com.willam.Dao;import com.willam.domain.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository;@Repository public interface UserDao extends JpaRepository<User,Integer> { }八、 集成Spring Data Redis
實現步驟:
1. 添加Redis起步依賴
<!--redis起步依賴--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>2. 在application.properties中配置redis端口、地址
# Redis 配置(不填也是可以的) spring.redis.host=localhost spring.redis.port=63793. 注入RedisTemplate操作Redis緩存查詢所有用戶數據
將Controller層的findAll方法修改一下
@ResourceRedisTemplate redisTemplate;@RequestMapping("/findAll")public List<User> findAll(){/*如果第一次查詢,redis緩存沒有數據,就從數據庫中獲取如果有緩存數據,那么從緩存中獲取*/String users = (String)redisTemplate.boundValueOps("userDao.findAll").get();if (users==null){//數據庫查詢List<User> all=userService.findAll();users = all.toString();redisTemplate.boundValueOps("userDao.findAll").set(users); //把當前的數據緩存}return userService.findAll();}4. 測試緩存
打一個斷點
地址欄 發送請求 http://localhost:8080/user/findAll
后臺
redis中有緩存則直接走 return userService.findAll();
九、 集成定時器
(1)在SpringbootJpaApplication開啟定時器
(2)創建Timer類
package com.willam.Utils;import com.willam.Service.UserService; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component;import java.time.LocalDate; import java.time.LocalDateTime; import java.util.Date;/*** @author :lijunxuan* @date :Created in 2019/6/28 19:18* @description :* @version: 1.0*/ @Component //把當前的類加入spring容器 public class Timer {UserService userService;/*** 定時任務的配置* corn 表達式* fixedDelay 固定的延遲時間執行,上一個任務執行完成,多久之后下一個任務執行。* rateDelay 固定頻率執行,每隔固定時間執行任務*/@Scheduled(fixedRate = 2000)public void task(){System.out.println(new Date());System.out.println(LocalDateTime.now());} }(3)測試結果
每隔2秒執行一次
十、SpringBoot如何代碼測試
(1)加入依賴
<!--springBoot的測試依賴--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency>(2)編寫測試類:
- SpringRunner繼承SpringJUnit4ClassRunner,使用哪一個Spring提供的測試引擎都可以。指定運行測試的引擎
- @SpringBootTest的屬性值指的是引導類的字節碼對象
(3)測試結果
十一、Spring Boot 如何打包部署
(1)打成Jar包部署
在cmd切換到項目的路徑
如:H:\workspace\IdeaProject\SpringBoot\day01\day01_springboot_jpa
啟動命令:
啟動命令的時候配置jvm參數也是可以的。然后查看一下Java的參數配置結果
小技巧
(2)打成War包部署
1.執行maven打包命令或者使用IDEA的Maven工具打包,需要修改pom.xml文件中的打包類型。
<packaging>war</packaging>2. 注冊啟動類
- 創建 ServletInitializer.java,繼承 SpringBootServletInitializer ,覆蓋 configure(),把啟動類 Application 注冊進去。外部 Web 應用服務器構建 Web Application Context 的時候,會把啟動類添加進去
3.然后執行打包操作
啟動
總結
以上是生活随笔為你收集整理的SpringBoot JPA的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怎么调节电脑显卡刷新频率(电脑显卡刷新频
- 下一篇: 注册中心 Spring Cloud Eu