SpringBoot整合mybaties

一、mysql环境准备

1、创建数据库yx,创建用户表user

CREATE TABLE `user`  (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
`password` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
`age` int(11) NULL DEFAULT NULL,
`birthday` date NULL DEFAULT NULL,
`des` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 7 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Dynamic;

二、码代码

0.代码结构如下

image.png

1.pom.xml

     
    
        mysql
        mysql-connector-java
        8.0.25
    

    
    
        org.mybatis.spring.boot
        mybatis-spring-boot-starter
        2.1.3
    

2.application.yaml

spring:
#数据库连接配置
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/yx?characterEncoding=utf-8&useSSL=false
username: root
password: 123456

#mybatis的相关配置
mybatis:
#mapper配置文件
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.yx.entity
#开启驼峰命名
configuration:
map-underscore-to-camel-case: true

3./com/yx/pojo/User.java

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

import java.util.Date;

@Data
public class User {

  private String username;

  @JsonProperty(access = JsonProperty.Access.WRITE_ONLY) //表示 Restful接口返回给前台时不包含该字段
  private String password;

  private Integer age;

  @JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss a", locale = "zh", timezone = "GTM+8")
  private Date birthday;

  private String des;
}  

4./com/yx/mapper/UserMapper.java

@Mapper
@Repository
public interface  UserMapper {

  //查询
  public List queryAll();
  //添加数据
  public int add(User userLogin);
  //根据用户名查询数据
  public User queryByName(String username);
}

5./mapper/UserMapper.xml




  

  
    insert into user(username, password, age, birthday, des) values (#{username},#{password},#{age},#{birthday},#{des})
  

  


6./com/yx/services/UserServicesI.java

public interface UserServicesI {
  //查询
  public List queryAll();
  //添加数据
  public int add(User userLogin);
  //根据用户名查询数据
  public User queryByName(String username);
}

7./com/yx/services/UserServicesImpl.java

@Service
public class UserServicesImpl implements UserServicesI {

  @Resource
  UserMapper userMapper;

  @Override
  public List queryAll() {
    return userMapper.queryAll();
  }

  @Override
  public int add(User user) {
    return userMapper.add(user);
  }

  @Override
  public User queryByName(String username) {
    return userMapper.queryByName(username);
  }

}

8./com/yx/controller/UserController.java

@RestController
@RequestMapping("/user")
public class UserController {

@Resource
UserServicesImpl userServices;

@RequestMapping(value = "/login", method = RequestMethod.POST)
public Object login(HttpServletResponse response,
                    @RequestParam(value = "username", required = true) String userName,
                    @RequestParam(value = "password", required = true) String password){
    User user1 = userServices.queryByName(userName);
    if(user1 != null){ //  假设查到即为登录成功
        return ApiResult.success(user1);
    }
    else{
        //查无用户
        return ApiResult.failure(ResponseCode.ACCOUNT_NOT_EXIST);
    }
}

@RequestMapping(value = "/register", method = RequestMethod.POST)
public Object register(HttpServletRequest request, @RequestBody User user){
    //将用户信息插入到数据库中
    int add = userServices.add(user);
    return ApiResult.success(add);
}

@RequestMapping(value = "/get", method = RequestMethod.GET)
public Object register(HttpServletRequest request){
    List users = userServices.queryAll();
    return ApiResult.success(users);
}

}

三、Postman测试

1.注册

image.png
image.png

忽略password未加密,此处只是示意

2.登录

image.png

3.获取用户列表

image.png

你可能感兴趣的:(SpringBoot整合mybaties)