springboot JPA访问Mysql
灰太狼_cxh 人气:01.代码实现:
添加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>
yml配置
spring: application: name: auth-server datasource: url: jdbc:mysql://127.0.0.1:3306/spring_cloud?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai&autoReconnect=true&failOverReadOnly=false username: root password: 123456 hikari: connection-timeout: 30000 idle-timeout: 600000 max-lifetime: 1800000 maximum-pool-size: 9 jpa: properties: hibernate: hbm2ddl: auto: create-drop
spring.jpa.properties.hibernate.hbm2ddl.auto是hibernate的配置属性,其主要作用是:自动创建、更新、验证数据库表结构。该参数的几种配置如下:
create:每次加载hibernate时都会删除上一次的生成的表,然后根据你的model类再重新来生成新表,哪怕两次没有任何改变也要这样执行,这就是导致数据库表数据丢失的一个重要原因。
create-drop:每次加载hibernate时根据model类生成表,但是sessionFactory一关闭,表就自动删除。
update:最常用的属性,第一次加载hibernate时根据model类会自动建立起表的结构(前提是先建立好数据库),以后加载hibernate时根据model类自动更新表结构,即使表结构改变了但表中的行仍然存在不会删除以前的行。要注意的是当部署到服务器后,表结构是不会被马上建立起来的,是要等应用第一次运行起来后才会。
validate:每次加载hibernate时,验证创建数据库表结构,只会和数据库中的表进行比较,不会创建新表,但是会插入新值。
控制层
package com.cxh.jpa.controller; import com.cxh.jpa.entity.User; import com.cxh.jpa.service.IUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController public class UserController { @Autowired private IUserService userService; @GetMapping("/findAll") public List<User> findAll(){ return userService.findAll(); } @GetMapping("/findByName") public User findByName(String name){ return userService.findByName(name); } @PostMapping("/add") public String add(User user){ return userService.add(user); } @PutMapping("/edit") public String edit(User user){ return userService.edit(user); } @DeleteMapping("/deleteById") public String deleteById(Long id){ return userService.deleteById(id); } }
服务层
package com.cxh.jpa.service.impl; import com.cxh.jpa.entity.User; import com.cxh.jpa.repository.UserRepository; import com.cxh.jpa.service.IUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserServiceImpl implements IUserService { @Autowired private UserRepository userRepository; @Override public List<User> findAll() { return userRepository.findAll(); } @Override public User findByName(String name) { return userRepository.findByName(name); } @Override public String add(User user) { userRepository.save(user); return "添加成功"; } @Override public String edit(User user) { userRepository.save(user); return "修改成功"; } @Override public String deleteById(Long id) { userRepository.deleteById(id); return "删除成功"; } }
访问层
package com.cxh.jpa.repository; import com.cxh.jpa.entity.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; public interface UserRepository extends JpaRepository<User, Long> { User findByName(String name); @Query("from User u where u.name=:name") User findUser(@Param("name") String name); }
实体类
package com.cxh.jpa.entity; import lombok.Data; import lombok.NoArgsConstructor; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; @Entity @Data @NoArgsConstructor public class User { @Id @GeneratedValue private Long id; private String name; private Integer age; }
@Entity注解标识了User类是一个持久化的实体
@Data和@NoArgsConstructor是Lombok中的注解。用来自动生成各参数的Set、Get函数以及不带参数的构造函数。
@Id和@GeneratedValue用来标识User对应对应数据库表中的主键
2.实现效果:
运行项目,打开postman发起请求
加载全部内容