1.项目结构
2.添加依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.1</version> <relativePath/> </parent> <groupId>com.fizzyelf</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>JPATest</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </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-data-jpa</artifactId> </dependency>
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.22</version> </dependency>
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency>
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> </dependencies>
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
</project>
|
3.application.properties
1 2 3 4 5 6 7 8 9 10 11 12
| server.port=8443
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/hibernatetest?serverTimezone=GMT spring.datasource.username=root spring.datasource.password=Good18882336322
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show_sql = true
spring.jpa.properties.hibernate.format_sql = true
|
4.新建一个user表
5.idea连接数据库
然后在弹出框输入数据库相关信息,记得加上时区,最后点击尝试连接,连接成功后,点击确定,这样idea就和数据库连接上了。
6.编写实体类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package com.fizzyelf.demo.Entity;
import lombok.Data;
import javax.persistence.*;
@Data @Entity @Table(name="user") public class User { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) @Column(name="id") private Integer id;
@Column(name="user_name") private String userName;
@Column(name="password") private String password; }
|
此时idea还解析不到数据库,我们点击“分配数据源”
将数据源选择刚刚我们添加的数据库,这样idea就可以解析到数据库中的表,和字段了。
7.repository 数据层
1 2 3 4 5 6 7 8 9 10 11 12 13
| package com.fizzyelf.demo.Dao;
import com.fizzyelf.demo.Entity.User; import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<User,Integer> { }
|
JpaRepository接口中定义了许多方法,其中就包括了基本的增删改查的方法。
8.Service业务层
接口UserService
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| package com.fizzyelf.demo.Service;
import com.fizzyelf.demo.Entity.User;
import java.util.List;
public interface UserService {
User insertUser(User user);
void deleteUser(int id);
User findUserById(int id);
List<User> findAllUser();
User updateUser(User user); }
|
实现类UserServiceImpl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| package com.fizzyelf.demo.Service.impl;
import com.fizzyelf.demo.Dao.UserRepository; import com.fizzyelf.demo.Entity.User; import com.fizzyelf.demo.Service.UserService; import org.springframework.stereotype.Service;
import javax.annotation.Resource; import java.util.List;
@Service public class UserServiceImpl implements UserService { @Resource private UserRepository userRepository;
@Override public User insertUser(User user){ return userRepository.save(user); }
@Override public void deleteUser(int id) { userRepository.deleteById(id); }
@Override public User findUserById(int id){ return userRepository.findById(id).orElse(null); }
@Override public List<User> findAllUser() { return userRepository.findAll(); }
@Override public User updateUser(User user) { return userRepository.save(user); } }
|
9.Controller控制层
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| package com.fizzyelf.demo.Controller;
import com.fizzyelf.demo.Entity.User; import com.fizzyelf.demo.Service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController @RequestMapping("/user") public class UserController { @Autowired private UserService userService;
@PostMapping("") public User addUser(@RequestBody User user){ return userService.insertUser(user); }
@DeleteMapping ("/{id}") public void deleteUser(@PathVariable("id") int id){ userService.deleteUser(id); }
@GetMapping ("/{id}") public User findUser(@PathVariable("id") int id){ return userService.findUserById(id); }
@GetMapping ("") public List<User> findAllUser(){ return userService.findAllUser(); }
@PutMapping ("") public User updateUser(@RequestBody User user){ return userService.updateUser(user); } }
|
10.测试
新增一条数据
查询数据
修改
删除