Merge pull request '更换数据库连接为JPA,添加数据库认证信息。' (#1) from feature/db into main
Reviewed-on: #1
This commit is contained in:
commit
2c45142355
24
pom.xml
24
pom.xml
@ -46,6 +46,10 @@
|
|||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-jdbc</artifactId>
|
<artifactId>spring-boot-starter-jdbc</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||||
|
</dependency>
|
||||||
<!-- <dependency>-->
|
<!-- <dependency>-->
|
||||||
<!-- <groupId>org.springframework.boot</groupId>-->
|
<!-- <groupId>org.springframework.boot</groupId>-->
|
||||||
<!-- <artifactId>spring-boot-starter-security</artifactId>-->
|
<!-- <artifactId>spring-boot-starter-security</artifactId>-->
|
||||||
@ -58,11 +62,11 @@
|
|||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-web-services</artifactId>
|
<artifactId>spring-boot-starter-web-services</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<!-- <dependency>-->
|
||||||
<groupId>org.mybatis.spring.boot</groupId>
|
<!-- <groupId>org.mybatis.spring.boot</groupId>-->
|
||||||
<artifactId>mybatis-spring-boot-starter</artifactId>
|
<!-- <artifactId>mybatis-spring-boot-starter</artifactId>-->
|
||||||
<version>3.0.3</version>
|
<!-- <version>3.0.3</version>-->
|
||||||
</dependency>
|
<!-- </dependency>-->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.session</groupId>
|
<groupId>org.springframework.session</groupId>
|
||||||
<artifactId>spring-session-jdbc</artifactId>
|
<artifactId>spring-session-jdbc</artifactId>
|
||||||
@ -74,11 +78,11 @@
|
|||||||
<scope>runtime</scope>
|
<scope>runtime</scope>
|
||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<!-- <dependency>-->
|
||||||
<groupId>com.h2database</groupId>
|
<!-- <groupId>com.h2database</groupId>-->
|
||||||
<artifactId>h2</artifactId>
|
<!-- <artifactId>h2</artifactId>-->
|
||||||
<scope>runtime</scope>
|
<!-- <scope>runtime</scope>-->
|
||||||
</dependency>
|
<!-- </dependency>-->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.mysql</groupId>
|
<groupId>com.mysql</groupId>
|
||||||
<artifactId>mysql-connector-j</artifactId>
|
<artifactId>mysql-connector-j</artifactId>
|
||||||
|
|||||||
@ -0,0 +1,23 @@
|
|||||||
|
package org.cmh.backend.authentication.controller;
|
||||||
|
|
||||||
|
import org.cmh.backend.authentication.model.User;
|
||||||
|
import org.cmh.backend.authentication.service.UserService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/users")
|
||||||
|
public class UserController {
|
||||||
|
@Autowired
|
||||||
|
private UserService userService;
|
||||||
|
|
||||||
|
@GetMapping("/{username}")
|
||||||
|
public ResponseEntity<User> getUser(@PathVariable String username) {
|
||||||
|
User user = userService.getUserByUsername(username);
|
||||||
|
return ResponseEntity.ok(user);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,39 @@
|
|||||||
|
package org.cmh.backend.authentication.model;
|
||||||
|
|
||||||
|
import jakarta.persistence.Entity;
|
||||||
|
import jakarta.persistence.GeneratedValue;
|
||||||
|
import jakarta.persistence.GenerationType;
|
||||||
|
import jakarta.persistence.Id;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class User {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
private Long id;
|
||||||
|
private String username;
|
||||||
|
private String password;
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,8 @@
|
|||||||
|
package org.cmh.backend.authentication.repository;
|
||||||
|
|
||||||
|
import org.cmh.backend.authentication.model.User;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
|
public interface UserRepository extends JpaRepository<User, Long> {
|
||||||
|
User findByUsername(String username);
|
||||||
|
}
|
||||||
@ -0,0 +1,16 @@
|
|||||||
|
package org.cmh.backend.authentication.service;
|
||||||
|
|
||||||
|
import org.cmh.backend.authentication.model.User;
|
||||||
|
import org.cmh.backend.authentication.repository.UserRepository;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class UserService {
|
||||||
|
@Autowired
|
||||||
|
private UserRepository userRepository;
|
||||||
|
|
||||||
|
public User getUserByUsername(String username) {
|
||||||
|
return userRepository.findByUsername(username);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1 +1,24 @@
|
|||||||
spring.application.name=backend
|
spring.application.name=backend
|
||||||
|
server.port=8080
|
||||||
|
spring.datasource.url=jdbc:mysql://ecs1.heshunme.xyz:3306/cmh
|
||||||
|
spring.datasource.username=neu
|
||||||
|
spring.datasource.password=5rWL55uf5rGH5a6e6K6t5bCP57uE
|
||||||
|
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
|
||||||
|
# JPA settings
|
||||||
|
spring.jpa.hibernate.ddl-auto=update
|
||||||
|
spring.jpa.show-sql=true
|
||||||
|
spring.jpa.properties.hibernate.format_sql=true
|
||||||
|
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
|
||||||
|
# Data source pool properties
|
||||||
|
spring.datasource.hikari.maximum-pool-size=10
|
||||||
|
spring.datasource.hikari.minimum-idle=5
|
||||||
|
spring.datasource.hikari.idle-timeout=30000
|
||||||
|
spring.datasource.hikari.pool-name=SpringBootHikariCP
|
||||||
|
spring.datasource.hikari.max-lifetime=2000000
|
||||||
|
spring.datasource.hikari.connection-timeout=30000
|
||||||
|
# Servlet
|
||||||
|
server.servlet.encoding.enabled=true
|
||||||
|
server.servlet.encoding.force=true
|
||||||
|
server.servlet.encoding.charset=utf-8
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user