diff --git a/springboot-jpa-demo/pom.xml b/springboot-jpa-demo/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..e039b2a693a46f6e02397b921db905c0122f1435 --- /dev/null +++ b/springboot-jpa-demo/pom.xml @@ -0,0 +1,79 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.6.2 + + + com.cunyu + springboot-jpa-demo + 0.0.1-SNAPSHOT + springboot-jpa-demo + springboot-jpa-demo + + 1.8 + + + + com.alibaba + druid + 1.2.8 + + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-devtools + runtime + true + + + mysql + mysql-connector-java + runtime + + + org.springframework.boot + spring-boot-configuration-processor + true + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.projectlombok + lombok + + + + + + + + diff --git a/springboot-jpa-demo/src/main/java/com/cunyu/springbootjpademo/SpringbootJpaDemoApplication.java b/springboot-jpa-demo/src/main/java/com/cunyu/springbootjpademo/SpringbootJpaDemoApplication.java new file mode 100644 index 0000000000000000000000000000000000000000..8938eb55945dfe78f2d712bd862893d5dcc5a0a1 --- /dev/null +++ b/springboot-jpa-demo/src/main/java/com/cunyu/springbootjpademo/SpringbootJpaDemoApplication.java @@ -0,0 +1,13 @@ +package com.cunyu.springbootjpademo; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringbootJpaDemoApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringbootJpaDemoApplication.class, args); + } + +} diff --git a/springboot-jpa-demo/src/main/java/com/cunyu/springbootjpademo/controller/UserController.java b/springboot-jpa-demo/src/main/java/com/cunyu/springbootjpademo/controller/UserController.java new file mode 100644 index 0000000000000000000000000000000000000000..98fa0638207becc192881441b15723d654da5d1d --- /dev/null +++ b/springboot-jpa-demo/src/main/java/com/cunyu/springbootjpademo/controller/UserController.java @@ -0,0 +1,67 @@ +package com.cunyu.springbootjpademo.controller; + +import com.cunyu.springbootjpademo.entity.User; +import com.cunyu.springbootjpademo.repository.UserRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +/** + * Created with IntelliJ IDEA. + * + * @author : 村雨遥 + * @version : 1.0 + * @project : springboot-jpa-demo + * @package : com.cunyu.springbootjpademo.controller + * @className : UserController + * @createTime : 2022/1/7 10:55 + * @email : 747731461@qq.com + * @微信 : cunyu1024 + * @公众号 : 村雨遥 + * @网站 : https://cunyu1943.github.io + * @description : + */ + +@RestController +@RequestMapping("/user") +public class UserController { + @Autowired + private UserRepository userRepository; + + /** + * 新增用户 + * + * @param user + */ + @PostMapping("/add") + public void addUser(@RequestBody User user) { + userRepository.save(user); + } + + /** + * 根据用户 id 删除用户 + * + * @param id + */ + @PostMapping("/delete/{id}") + public void deleteUser(@PathVariable Integer id) { + userRepository.deleteById(id); + } + + /** + * 更新用户,id 相同则更新,不同则新增 + * + * @param user + */ + @PostMapping("/update") + public void updateUser(@RequestBody User user) { + userRepository.save(user); + } + + @PostMapping("/query") + public List queryUsers() { + List userList = userRepository.findAll(); + return userList; + } +} diff --git a/springboot-jpa-demo/src/main/java/com/cunyu/springbootjpademo/entity/User.java b/springboot-jpa-demo/src/main/java/com/cunyu/springbootjpademo/entity/User.java new file mode 100644 index 0000000000000000000000000000000000000000..d8ef09aca56aab15cfaaf29cad997a0332d32fab --- /dev/null +++ b/springboot-jpa-demo/src/main/java/com/cunyu/springbootjpademo/entity/User.java @@ -0,0 +1,49 @@ +package com.cunyu.springbootjpademo.entity; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +/** + * Created with IntelliJ IDEA. + * + * @author : 村雨遥 + * @version : 1.0 + * @project : springboot-jpa-demo + * @package : com.cunyu.springbootjpademo.entity + * @className : User + * @createTime : 2022/1/7 10:19 + * @email : 747731461@qq.com + * @微信 : cunyu1024 + * @公众号 : 村雨遥 + * @网站 : https://cunyu1943.github.io + * @description : + */ + +@Entity +@Data +@AllArgsConstructor +@NoArgsConstructor +public class User { + @Id + @GeneratedValue + @Column(name = "id") + private Integer id; + @Column(name = "username") + private String username; + @Column(name = "password") + private String password; + @Column(name = "ch_name") + private String chName; + + public User(String username, String password, String chName) { + this.username = username; + this.password = password; + this.chName = chName; + } +} diff --git a/springboot-jpa-demo/src/main/java/com/cunyu/springbootjpademo/repository/UserRepository.java b/springboot-jpa-demo/src/main/java/com/cunyu/springbootjpademo/repository/UserRepository.java new file mode 100644 index 0000000000000000000000000000000000000000..61504ba766a51dbae19e6b219f26e4221369639a --- /dev/null +++ b/springboot-jpa-demo/src/main/java/com/cunyu/springbootjpademo/repository/UserRepository.java @@ -0,0 +1,24 @@ +package com.cunyu.springbootjpademo.repository; + +import com.cunyu.springbootjpademo.entity.User; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Service; + +/** + * Created with IntelliJ IDEA. + * + * @author : 村雨遥 + * @version : 1.0 + * @project : springboot-jpa-demo + * @package : com.cunyu.springbootjpademo.repository + * @className : UserRepository + * @createTime : 2022/1/7 10:49 + * @email : 747731461@qq.com + * @微信 : cunyu1024 + * @公众号 : 村雨遥 + * @网站 : https://cunyu1943.github.io + * @description : + */ +@Service +public interface UserRepository extends JpaRepository { +} diff --git a/springboot-jpa-demo/src/main/resources/application.yml b/springboot-jpa-demo/src/main/resources/application.yml new file mode 100644 index 0000000000000000000000000000000000000000..0606130d9b27359e95ba220cb3b94b94e4fd2013 --- /dev/null +++ b/springboot-jpa-demo/src/main/resources/application.yml @@ -0,0 +1,13 @@ +server: + port: 8080 +spring: + datasource: + driver-class-name: com.mysql.cj.jdbc.Driver + url: jdbc:mysql://localhost:3306/employee-management?characterEncoding=utf-8 + password: "0908" + username: root +# type: com.alibaba.druid.pool.DruidDataSource + jpa: + hibernate: + ddl-auto: update + show-sql: true diff --git a/springboot-jpa-demo/src/test/java/com/cunyu/springbootjpademo/SpringbootJpaDemoApplicationTests.java b/springboot-jpa-demo/src/test/java/com/cunyu/springbootjpademo/SpringbootJpaDemoApplicationTests.java new file mode 100644 index 0000000000000000000000000000000000000000..58dcc368fb6858f1163a45c072acce50d2f2e7cf --- /dev/null +++ b/springboot-jpa-demo/src/test/java/com/cunyu/springbootjpademo/SpringbootJpaDemoApplicationTests.java @@ -0,0 +1,13 @@ +package com.cunyu.springbootjpademo; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class SpringbootJpaDemoApplicationTests { + + @Test + void contextLoads() { + } + +}