Commit ba74b150 by wzy

@DuplicatedUsernameConstraint

parent 780764ec
......@@ -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) {
......
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 {};
}
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();
}
}
......@@ -12,6 +12,7 @@ import javax.validation.constraints.NotBlank;
@AllArgsConstructor
@NoArgsConstructor
public class RegisterParam {
@DuplicatedUsernameConstraint
@NotBlank(message = "用户名不能为空")
private String username;
......
......@@ -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;
}
}
......@@ -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);
......
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"})
......
......@@ -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
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));
}
}
......@@ -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
<?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
<?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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment