1.更改mapper子工程的application.yml,修改连接的数据库
spring: datasource: druid: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/fmmall2?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8 username: root password: 123456 mybatis: mapper-locations: classpath:mappers/*Mapper.xml type-aliases-package: com.qfedu.fmmall.entity
2.创建user实体类
beans-src-main-java-com.qfedu.fmmall.entity- User.java:
package com.qfedu.fmmall.entity; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.util.Date; @Data @NoArgsConstructor @AllArgsConstructor @ApiModel(value = "User对象",description = "买家信息") public class User { private int userId; private String username; private String password; private String nickname; private String realname; private String userImg; private String userMobile; private String userEmail; private String userSex; private Date userBirth; private Date userRegtime; private Date userModtime; }
2.创建DAO接口,定义操作方法。
mapper-src-main-java-com.qfedu.fmmall.dao- UserDAO.java:
package com.qfedu.fmmall.dao; import com.qfedu.fmmall.entity.User; //@Repository //@Mapper public interface UserDAO { //用户注册 public int insertUser(User user); //根据用户名查询用户信息 public User queryUserByName(String name); }
3.创建DAO接口的mapper文件并完成配置。 mapper-src-main-resources-mappers- UserMapper.xml :
<?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.qfedu.fmmall.dao.UserDAO"> <insert id="insertUser"> insert into users(username,password,user_regtime,user_modtime) values(#{user.username},#{user.password},#{user.userRegtime},#{userModtime}) </insert> <resultMap id="userMap" type="User"> <id column="user_id" property=""/> <result column="username" property="username"/> <result column="password" property="password"/> <result column="nickname" property="nickname"/> <result column="realname" property="realname"/> <result column="user_img" property="userImg"/> <result column="user_mobile" property="userMobile"/> <result column="user_email" property="userEmail"/> <result column="user_sex" property="userSex"/> <result column="user_birth" property="userBirth"/> <result column="user_regtime" property="userRegtime"/> <result column="user_modtime" property="userModtime"/> </resultMap> <select id="queryUserByName" resultMap="userMap"> select user_id,username,password,nickname,realname,user_img,user_mobile, user_email,user_sex,user_birth,user_regtime,user_modtime from users where username=#{name} </select> </mapper>
4.完成Service业务 5.完成Controller提供接口