mirror of
https://gitee.com/many2many/java-web.git
synced 2025-01-11 14:40:55 +08:00
example
This commit is contained in:
parent
19254c73bf
commit
81502c754b
@ -1,8 +1,6 @@
|
||||
package com.lk.demo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
|
||||
|
@ -1,11 +1,27 @@
|
||||
package com.lk.demo;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table(name = "users")
|
||||
public class User {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String name;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String password;
|
||||
|
||||
@Column()
|
||||
private String email;
|
||||
|
||||
@Column
|
||||
private String phone;
|
||||
|
||||
@Column
|
||||
private Integer age;
|
||||
|
||||
public String getName() {
|
||||
|
@ -87,6 +87,6 @@ public class UserController {
|
||||
@DeleteMapping("/{id}")
|
||||
public String deleteUser(@PathVariable Long id){
|
||||
System.out.println("delete id: "+id);
|
||||
return "ok";
|
||||
return userService.delete(id);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,6 @@
|
||||
package com.lk.demo;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface UserRepository extends JpaRepository<User, Long> {
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
package com.lk.demo;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
@ -10,39 +11,38 @@ import java.util.List;
|
||||
@Service
|
||||
public class UserService {
|
||||
|
||||
@Autowired
|
||||
UserRepository userRepository;
|
||||
|
||||
public User getUser(Long id){
|
||||
User user = new User();
|
||||
user.setId(id);
|
||||
user.setName("lk");
|
||||
user.setAge(18);
|
||||
return user;
|
||||
return userRepository.findById(id).orElse(null);
|
||||
}
|
||||
|
||||
|
||||
public List<User> queryUsers(){
|
||||
List<User> users = new ArrayList<>();
|
||||
User u1 = new User();
|
||||
u1.setName("John");
|
||||
u1.setEmail("john@gmail.com");
|
||||
u1.setPhone("123456789");
|
||||
users.add(u1);
|
||||
User u2 = new User();
|
||||
u2.setName("Mike");
|
||||
u2.setEmail("mike@gmail.com");
|
||||
u2.setPhone("123456789");
|
||||
users.add(u2);
|
||||
return users;
|
||||
return userRepository.findAll();
|
||||
}
|
||||
|
||||
|
||||
public User createUser(User user){
|
||||
user.setId(22L);
|
||||
return user;
|
||||
return userRepository.save(user);
|
||||
}
|
||||
|
||||
public User updateUser(Long id, User user){
|
||||
user.setId(id);
|
||||
return user;
|
||||
userRepository.findById(id).ifPresent(u->{
|
||||
// u.setName(user.getName());
|
||||
u.setPassword(user.getPassword());
|
||||
u.setEmail(user.getEmail());
|
||||
u.setPhone(user.getPhone());
|
||||
u.setAge(user.getAge());
|
||||
userRepository.save(u);
|
||||
});
|
||||
return userRepository.findById(id).orElse(null);
|
||||
}
|
||||
|
||||
public String delete(Long id){
|
||||
userRepository.deleteById(id);
|
||||
return "ok";
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ spring:
|
||||
jackson:
|
||||
property-naming-strategy: SNAKE_CASE # 驼峰转下划线
|
||||
datasource:
|
||||
url: jdbc:mysql://localhost:3306/paopao?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true # MySQL连接URL,paopao是数据库名
|
||||
url: jdbc:mysql://localhost:3306/demo?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true # MySQL连接URL,paopao是数据库名
|
||||
driverClassName: com.mysql.cj.jdbc.Driver # MySQL的Driver类名
|
||||
username: root # MySQL用户名
|
||||
password: root # MySQL密码
|
||||
@ -13,4 +13,30 @@ spring:
|
||||
database-platform: org.hibernate.dialect.MySQL8Dialect # 修改为MySQL的Dialect
|
||||
hibernate:
|
||||
ddl-auto: update # 根据需要调整,update适用于开发和某些生产环境
|
||||
format_sql: true # 保持原样,用于格式化SQL输出
|
||||
format_sql: true # 保持原样,用于格式化SQL输出
|
||||
|
||||
#
|
||||
#spring:
|
||||
#
|
||||
# jackson:
|
||||
# property-naming-strategy: SNAKE_CASE # 驼峰转下划线
|
||||
#
|
||||
# datasource:
|
||||
# url: jdbc:h2:file:./demo.h2 # 使用文件存储
|
||||
# driverClassName: org.h2.Driver
|
||||
# username: root
|
||||
# password: root
|
||||
# h2:
|
||||
# console: # 开启console访问 默认false
|
||||
# enabled: true
|
||||
# settings:
|
||||
# trace: true # 开启h2 console 跟踪 方便调试 默认 false
|
||||
# web-allow-others: true # 允许console 远程访问 默认false
|
||||
# path: /h2 # h2 访问路径上下文
|
||||
# jpa:
|
||||
# show-sql: true
|
||||
# open-in-view: false
|
||||
# defer-datasource-initialization: true
|
||||
# database-platform: org.hibernate.dialect.H2Dialect
|
||||
# hibernate:
|
||||
# ddl-auto: update
|
Loading…
Reference in New Issue
Block a user