Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
springboot-mybatis
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wzy
springboot-mybatis
Commits
ba74b150
Commit
ba74b150
authored
May 07, 2021
by
wzy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
@DuplicatedUsernameConstraint
parent
780764ec
Hide whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
107 additions
and
30 deletions
+107
-30
src/main/java/com/wzy/tacocloud/TacoCloudApplication.java
+2
-1
src/main/java/com/wzy/tacocloud/application/user/DuplicatedUsernameConstraint.java
+16
-0
src/main/java/com/wzy/tacocloud/application/user/DuplicatedUsernameValidator.java
+18
-0
src/main/java/com/wzy/tacocloud/application/user/RegisterParam.java
+1
-0
src/main/java/com/wzy/tacocloud/application/user/UserService.java
+12
-9
src/main/java/com/wzy/tacocloud/controller/UserController.java
+3
-2
src/main/java/com/wzy/tacocloud/core/user/User.java
+1
-2
src/main/java/com/wzy/tacocloud/infrastructure/mybatis/mapper/UserMapper.java
+2
-0
src/main/java/com/wzy/tacocloud/infrastructure/repository/MyBatisUserRepository.java
+3
-3
src/main/resources/application.yml
+7
-6
src/main/resources/mapper/UserMapper.xml
+31
-7
src/main/resources/mapper/UserReadService.xml
+11
-0
No files found.
src/main/java/com/wzy/tacocloud/TacoCloudApplication.java
View file @
ba74b150
...
...
@@ -2,8 +2,9 @@ package com.wzy.tacocloud;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
import
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
;
@SpringBootApplication
// 表明这是一个 Spring 引导应用程序
@SpringBootApplication
public
class
TacoCloudApplication
{
public
static
void
main
(
String
[]
args
)
{
...
...
src/main/java/com/wzy/tacocloud/application/user/DuplicatedUsernameConstraint.java
0 → 100644
View file @
ba74b150
package
com
.
wzy
.
tacocloud
.
application
.
user
;
import
javax.validation.Constraint
;
import
javax.validation.Payload
;
import
java.lang.annotation.Retention
;
import
java.lang.annotation.RetentionPolicy
;
@Constraint
(
validatedBy
=
com
.
wzy
.
tacocloud
.
application
.
user
.
DuplicatedUsernameValidator
.
class
)
@Retention
(
RetentionPolicy
.
RUNTIME
)
@interface
DuplicatedUsernameConstraint
{
String
message
()
default
"用户名重复"
;
Class
<?>[]
groups
()
default
{};
Class
<?
extends
Payload
>[]
payload
()
default
{};
}
src/main/java/com/wzy/tacocloud/application/user/DuplicatedUsernameValidator.java
0 → 100644
View file @
ba74b150
package
com
.
wzy
.
tacocloud
.
application
.
user
;
import
com.wzy.tacocloud.core.user.UserRepository
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
javax.validation.ConstraintValidator
;
import
javax.validation.ConstraintValidatorContext
;
class
DuplicatedUsernameValidator
implements
ConstraintValidator
<
DuplicatedUsernameConstraint
,
String
>
{
@Autowired
private
UserRepository
userRepository
;
@Override
public
boolean
isValid
(
String
value
,
ConstraintValidatorContext
context
)
{
return
(
value
==
null
||
value
.
isEmpty
())
||
!
userRepository
.
findByUsername
(
value
).
isPresent
();
}
}
src/main/java/com/wzy/tacocloud/application/user/RegisterParam.java
View file @
ba74b150
...
...
@@ -12,6 +12,7 @@ import javax.validation.constraints.NotBlank;
@AllArgsConstructor
@NoArgsConstructor
public
class
RegisterParam
{
@DuplicatedUsernameConstraint
@NotBlank
(
message
=
"用户名不能为空"
)
private
String
username
;
...
...
src/main/java/com/wzy/tacocloud/application/user/UserService.java
View file @
ba74b150
...
...
@@ -12,22 +12,25 @@ import javax.validation.Valid;
@Service
@Validated
public
class
UserService
{
//
private final UserRepository userRepository;
private
final
UserRepository
userRepository
;
// private final EncryptService encryptService;
//
// @Autowired
// public UserService(
// UserRepository userRepository,
// EncryptService encryptService) {
// this.userRepository = userRepository;
@Autowired
public
UserService
(
UserRepository
userRepository
// ,EncryptService encryptService
)
{
this
.
userRepository
=
userRepository
;
// this.encryptService = encryptService;
//
}
}
public
User
createUser
(
@Valid
RegisterParam
registerParam
)
{
return
new
User
(
final
User
user
=
new
User
(
registerParam
.
getUsername
(),
registerParam
.
getPassword
()
);
userRepository
.
save
(
user
);
return
user
;
}
}
src/main/java/com/wzy/tacocloud/controller/UserController.java
View file @
ba74b150
...
...
@@ -17,8 +17,9 @@ import org.springframework.web.bind.annotation.RestController;
import
java.util.HashMap
;
import
java.util.Map
;
import
static
org
.
springframework
.
web
.
bind
.
annotation
.
RequestMethod
.
POST
;
@RestController
@RequestMapping
(
"/users"
)
public
class
UserController
{
// private final UserRepository userRepository;
private
final
UserService
userService
;
...
...
@@ -33,7 +34,7 @@ public class UserController {
this
.
jwtService
=
jwtService
;
}
@RequestMapping
(
"/register"
)
@RequestMapping
(
path
=
"/register"
,
method
=
POST
)
public
ResponseEntity
<?>
createUser
(
@Valid
@RequestBody
RegisterParam
registerParam
)
{
User
user
=
userService
.
createUser
(
registerParam
);
UserData
userData
=
userQueryService
.
findById
(
user
.
getId
()).
orElse
(
null
);
...
...
src/main/java/com/wzy/tacocloud/core/user/User.java
View file @
ba74b150
package
com
.
wzy
.
tacocloud
.
core
.
user
;
import
com.wzy.tacocloud.Util
;
import
java.util.UUID
;
import
lombok.EqualsAndHashCode
;
import
lombok.Getter
;
import
lombok.NoArgsConstructor
;
import
java.util.UUID
;
@Getter
@NoArgsConstructor
@EqualsAndHashCode
(
of
=
{
"id"
})
...
...
src/main/java/com/wzy/tacocloud/infrastructure/mybatis/mapper/UserMapper.java
View file @
ba74b150
...
...
@@ -8,5 +8,6 @@ import org.apache.ibatis.annotations.Param;
public
interface
UserMapper
{
void
insert
(
@Param
(
"user"
)
User
user
);
User
findById
(
@Param
(
"id"
)
String
id
);
User
findByUsername
(
@Param
(
"username"
)
String
username
);
void
update
(
@Param
(
"user"
)
User
user
);
}
\ No newline at end of file
src/main/java/com/wzy/tacocloud/infrastructure/repository/MyBatisUserRepository.java
View file @
ba74b150
package
com
.
wzy
.
tacocloud
.
infrastructure
.
repository
;
import
com.wzy.tacocloud.core.user.UserRepository
;
import
com.wzy.tacocloud.core.user.User
;
import
com.wzy.tacocloud.core.user.UserRepository
;
import
com.wzy.tacocloud.infrastructure.mybatis.mapper.UserMapper
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Repository
;
...
...
@@ -28,11 +28,11 @@ public class MyBatisUserRepository implements UserRepository {
@Override
public
Optional
<
User
>
findById
(
String
id
)
{
return
Optional
.
empty
(
);
return
Optional
.
ofNullable
(
userMapper
.
findById
(
id
)
);
}
@Override
public
Optional
<
User
>
findByUsername
(
String
username
)
{
return
Optional
.
empty
(
);
return
Optional
.
ofNullable
(
userMapper
.
findByUsername
(
username
)
);
}
}
src/main/resources/application.yml
View file @
ba74b150
...
...
@@ -6,9 +6,10 @@ spring:
username
:
root
password
:
123
mvc
:
static-path-pattern
:
/static/**
#配置静态路径
mybatis
:
type-aliases-package
:
com.wzy.tacocloud.entity
mapper-locations
:
classpath:com/wzy/tacocloud/mapper/*.xml
\ No newline at end of file
type-handlers-package
:
com.wzy.tacocloud.infrastructure.mybatis
mapper-locations
:
mapper/*.xml
jwt
:
secret
:
nRvyYC4soFxBdZ-F-5Nnzz5USXstR1YylsTd-mA0aKtI9HUlriGrtkf-TiuDapkLiUCogO3JOK7kwZisrHp6wA
sessionTime
:
86400
\ No newline at end of file
src/main/resources/mapper/UserMapper.xml
View file @
ba74b150
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper
namespace=
"day2.demo.test.mapper.UserMapper"
>
<select
id=
"selectUserById"
resultType=
"User"
parameterType=
"int"
>
SELECT * from u where id = #{id}
<mapper
namespace=
"com.wzy.tacocloud.infrastructure.mybatis.mapper.UserMapper"
>
<select
id=
"findById"
resultMap=
"user"
>
select id, username, password
from users
where id = #{id}
</select>
<select
id=
"selectUserByName"
resultType=
"User"
parameterType=
"String"
>
SELECT * from u where NAME = #{NAME}
<select
id=
"findByUsername"
resultMap=
"user"
>
select * from users where username = #{username}
</select>
<insert
id=
"insertUser"
parameterType=
"User"
>
insert into u(name,password) values(#{name},#{password})
<update
id=
"update"
>
update users
<set>
<if
test=
"user.username != ''"
>
username = #{user.username},
</if>
<if
test=
"user.password != ''"
>
password = #{user.password},
</if>
</set>
where id = #{user.id}
</update>
<insert
id=
"insert"
>
insert into users (id, username, password)
values (#{user.id},
#{user.username},
#{user.password})
</insert>
<resultMap
id=
"user"
type=
"com.wzy.tacocloud.core.user.User"
>
<id
column=
"id"
property=
"id"
/>
<result
column=
"username"
property=
"username"
/>
<result
column=
"password"
property=
"password"
/>
</resultMap>
</mapper>
\ No newline at end of file
src/main/resources/mapper/UserReadService.xml
0 → 100644
View file @
ba74b150
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper
namespace=
"com.wzy.tacocloud.infrastructure.mybatis.readservice.UserReadService"
>
<select
id=
"findByUsername"
resultType=
"com.wzy.tacocloud.application.data.UserData"
>
select * from users where username = #{username}
</select>
<select
id=
"findById"
resultType=
"com.wzy.tacocloud.application.data.UserData"
>
select * from users where id = #{id}
</select>
</mapper>
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment