Skip to content

Commit

Permalink
[spring boot] 用户登录/注册
Browse files Browse the repository at this point in the history
  • Loading branch information
SunYufei committed Feb 27, 2024
1 parent 16add13 commit a023368
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 0 deletions.
5 changes: 5 additions & 0 deletions spring-boot-demo/api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
<groupId>ml.sun</groupId>
<artifactId>common</artifactId>
</dependency>
<!-- service -->
<dependency>
<groupId>ml.sun</groupId>
<artifactId>service</artifactId>
</dependency>
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ml.sun.controller

import ml.sun.service.user.IUserService
import ml.sun.service.user.vo.UserInfoVO
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("user")
class UserController(private val service: IUserService) {
@PostMapping("login")
fun login(username: String, password: String) = service.login(username, password)

@PostMapping("register")
fun register(@RequestBody userInfo: UserInfoVO) = service.register(userInfo)
}
2 changes: 2 additions & 0 deletions spring-boot-demo/api/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
spring:
profiles:
active: ${profileActive:dev}
config:
import: application-service.yml

logging:
file:
Expand Down
1 change: 1 addition & 0 deletions spring-boot-demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
<configuration>
<compilerPlugins>
<plugin>spring</plugin>
<plugin>jpa</plugin>
</compilerPlugins>
</configuration>
</plugin>
Expand Down
11 changes: 11 additions & 0 deletions spring-boot-demo/service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,20 @@
<packaging>jar</packaging>

<dependencies>
<!-- spring boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- database -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ml.sun.service.user

import ml.sun.service.user.vo.UserInfoVO

interface IUserService {
fun login(username: String, password: String): String

fun register(vo: UserInfoVO): String
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package ml.sun.service.user.impl

import ml.sun.service.user.IUserService
import ml.sun.service.user.repository.UserInfoEntity
import ml.sun.service.user.repository.UserRepository
import ml.sun.service.user.vo.UserInfoVO
import org.springframework.stereotype.Service

@Service
class UserServiceImpl(private val repository: UserRepository) : IUserService {
override fun login(username: String, password: String): String =
when (repository.findByUsernameAndPassword(username, password)) {
null -> "username / password ERROR"
else -> "login success"
}

override fun register(vo: UserInfoVO): String {
if (checkUserExists(vo.username)) {
return "user already registered"
}
repository.save(UserInfoEntity(vo))
return "register success"
}

fun checkUserExists(username: String): Boolean = repository.findByUsername(username) != null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ml.sun.service.user.repository

import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.GeneratedValue
import jakarta.persistence.Id
import jakarta.persistence.Table
import ml.sun.service.user.vo.UserInfoVO
import java.time.LocalDateTime

@Entity
@Table(name = "user_info")
data class UserInfoEntity(
@Id @GeneratedValue var id: Int,
@Column var username: String,
@Column var password: String,
@Column var createDate: LocalDateTime,
@Column var email: String
) {
constructor(vo: UserInfoVO) : this(0, vo.username, vo.password, LocalDateTime.now(), vo.email)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ml.sun.service.user.repository

import org.springframework.data.jpa.repository.JpaRepository

interface UserRepository : JpaRepository<UserInfoEntity, Int> {
fun findByUsername(username: String): UserInfoEntity?

fun findByUsernameAndPassword(username: String, password: String): UserInfoEntity?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ml.sun.service.user.vo

import ml.sun.service.user.repository.UserInfoEntity

data class UserInfoVO(val username: String, val password: String, val email: String) {
constructor(entity: UserInfoEntity) : this(entity.username, entity.password, entity.email)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
spring:
datasource:
url: jdbc:h2:mem:design
driver-class-name: org.h2.Driver
username: design
password: design
h2:
console:
enabled: true
jpa:
properties:
hibernate:
hbm2ddl:
auto: update
show-sql: true

0 comments on commit a023368

Please sign in to comment.