赞
踩
项目代码已上传至github:https://github.com/snowlavenderlove/springbootValidator.git
1.创建数据库spring_validator,并建表user,表结构如图
2.通过https://start.spring.io/官网创建项目springbootValidator,项目结构如图,创建项目教程可参考博文:https://blog.csdn.net/qq_37231511/article/details/90669242
3.pom.xml中添加logging/druid/validator依赖,此处放全
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>1.5.2.RELEASE</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <groupId>com.xue</groupId>
- <artifactId>springbootValidator</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>springbootValidator</name>
- <description>Demo project for Spring Boot</description>
-
- <properties>
- <java.version>1.8</java.version>
- <maven-jar-plugin.version>3.0.0</maven-jar-plugin.version>
- </properties>
-
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-jpa</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <dependency>
- <groupId>org.mybatis.spring.boot</groupId>
- <artifactId>mybatis-spring-boot-starter</artifactId>
- <version>1.3.0</version>
- </dependency>
-
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <scope>runtime</scope>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>commons-logging</groupId>
- <artifactId>commons-logging</artifactId>
- <version>1.2</version>
- </dependency>
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid</artifactId>
- <version>1.1.17</version>
- </dependency>
- <dependency>
- <groupId>org.hibernate.validator</groupId>
- <artifactId>hibernate-validator</artifactId>
- <version>6.0.16.Final</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-thymeleaf</artifactId>
- </dependency>
- </dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
-
- </project>
4.通过generator-mybatis创建代码,教程可见博文:https://blog.csdn.net/qq_37231511/article/details/90692784,创建完后放到项目中如图:
5.编辑application.properies文件
- #mysql
- spring.datasource.driver-class-name=com.mysql.jdbc.Driver
- spring.datasource.url=jdbc:mysql://127.0.0.1:3306/spring_validator
- spring.datasource.username=root
- spring.datasource.password=123456
-
- #druid
- spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
- #dao
- mybatis.type-aliases-package==com.xue.repository.dao
- mybatis.mapper-locations=classpath*:com/xue/repository/mapper/*.xml
-
6.在entity下创建包http,并创建UserReqBean和UserResBean,代码分别为:
- package com.xue.entity.http;
-
- import javax.validation.constraints.Size;
-
- import org.hibernate.validator.constraints.NotEmpty;
-
- public class UserReqBean {
- /**
- * 在实体类上写上相关验证信息
- */
- private Integer id;
- @NotEmpty(message = "用户名不能为空!")
- private String name;
- @Size(min=3,max=10,message = "密码长度在3到10位")
- private String pwd;
-
- public Integer getId() {
- return id;
- }
-
- public void setId(Integer id) {
- this.id = id;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name == null ? null : name.trim();
- }
-
- public String getPwd() {
- return pwd;
- }
-
- public void setPwd(String pwd) {
- this.pwd = pwd == null ? null : pwd.trim();
- }
-
- @Override
- public String toString() {
- return "UserReqBean [id=" + id + ", name=" + name + ", pwd=" + pwd + "]";
- }
-
-
-
- }
- package com.xue.entity.http;
-
- public class UserResBean {
-
- private Integer id;
-
- private String name;
-
- private String pwd;
-
- public Integer getId() {
- return id;
- }
-
- public void setId(Integer id) {
- this.id = id;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name == null ? null : name.trim();
- }
-
- public String getPwd() {
- return pwd;
- }
-
- public void setPwd(String pwd) {
- this.pwd = pwd == null ? null : pwd.trim();
- }
-
-
- }
7.创建controller包,新建UserController
- package com.xue.controller;
-
- import java.util.List;
-
- import javax.validation.Valid;
-
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.validation.BindingResult;
- import org.springframework.validation.ObjectError;
- import org.springframework.web.bind.annotation.RequestBody;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.ResponseBody;
-
- import com.xue.entity.http.UserReqBean;
- import com.xue.entity.http.UserResBean;
- import com.xue.service.UserService;
-
- @Controller
- public class UserController {
-
- @Autowired
- private UserService userService;
-
- @RequestMapping("/loginHtml")
- public String loginHtml(){
-
- return "loginHtml";
- }
-
- @RequestMapping("/registerHtml")
- public String registerHtml(){
-
- return "registerHtml";
- }
- /**
- * 注意的是@Valid 和 BindingResult 是一 一对应的
- * 如果有多个@Valid,那么每个@Valid后面都需要添加BindingResult用于接收bean中的校验信息
- */
-
- @RequestMapping("/login")
- @ResponseBody
- public String login(@Valid @RequestBody UserReqBean inparam,BindingResult bingdingResult){
-
- if(bingdingResult.hasErrors()){
- List<ObjectError> ls=bingdingResult.getAllErrors();
- for (int i = 0; i < ls.size(); i++) {
- System.out.println("error:"+ls.get(i));
- }
- }
-
- UserResBean res = userService.login(inparam);
-
- System.out.println(inparam);
-
- if(null != res){
- return "登陆成功,用户名:"+res.getName()+" 密码"+res.getPwd();
- }else{
- return "登陆失败";
- }
-
-
- }
- /**
- * 注意的是@Valid 和 BindingResult 是一 一对应的
- * 如果有多个@Valid,那么每个@Valid后面都需要添加BindingResult用于接收bean中的校验信息
- */
- @RequestMapping("/register")
- @ResponseBody
- public String register(@Valid @RequestBody UserReqBean inparam,BindingResult bingdingResult){
-
- System.out.println("进入入口--------------------------");
-
- if(bingdingResult.hasErrors()){
- List<ObjectError> ls=bingdingResult.getAllErrors();
- for (int i = 0; i < ls.size(); i++) {
- System.out.println("error:"+ls.get(i));
- }
- }
-
- int result = userService.register(inparam);
-
- if(0 != result){
- return "注册成功";
-
- }else{
- return "注册失败";
- }
-
-
- }
-
-
-
-
- }
-
8.创建service包,创建UserService接口与UserServiceImpl实现类,代码分别为
- package com.xue.service;
-
- import com.xue.entity.http.UserReqBean;
- import com.xue.entity.http.UserResBean;
-
- public interface UserService {
-
- // 增
-
- public int register(UserReqBean user);
- // 查
-
- public UserResBean login(UserReqBean user);
-
- }
- package com.xue.service.Impl;
-
- import java.util.HashMap;
- import java.util.Map;
-
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- import com.xue.entity.http.UserReqBean;
- import com.xue.entity.http.UserResBean;
- import com.xue.entity.model.User;
- import com.xue.repository.dao.UserMapper;
- import com.xue.service.UserService;
- @Service
- public class UserServiceImpl implements UserService {
-
- @Autowired
- private UserMapper userMapper;
-
- @Override
- public int register(UserReqBean data) {
-
- int result = 0;
-
- User user = new User();
- user.setName(data.getName());
- user.setPwd(data.getPwd());
-
- try {
- result = userMapper.insertUser(user);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
- return result;
- }
-
- @Override
- public UserResBean login(UserReqBean data) {
- UserResBean res = null;
- res = new UserResBean();
- User user = null;
- Map<String,Object> params = new HashMap<String,Object>();
-
- params.put("name", data.getName());
-
- params.put("pwd", data.getPwd());
-
-
- try {
- user = userMapper.selectUser(params);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
-
-
- if(null != user){
- res.setName(user.getName());
- res.setPwd(user.getPwd());
-
- return res;
- }
-
-
-
- return res;
- }
-
-
-
- }
-
9.编辑UserMapper.java,在代码最后添加
- int insertUser(User user);
-
- User selectUser(Map<String,Object> inparams);
10.编辑UserMapper.xml,在代码最后添加
- <select id="selectUser" parameterType="com.xue.entity.model.User" resultMap="BaseResultMap">
- select name,pwd from user where name =#{name} and pwd = #{pwd}
- </select>
- <insert id="insertUser" parameterType="com.xue.entity.model.User">
- insert into user(name,pwd) values(#{name},#{pwd})
- </insert>
11.代码到此结束,最终项目结构为
12.通过后台接口测试软件输入正常的数据进行测试register接口
13.通过后台接口测试软件输入不符合规则的数据进行测试register接口,如图:后台打印出了信息
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。