1.配置文件
# 服务
server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3306/springbootdatabase
spring.datasource.username=***
spring.datasource.password=***
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5
# 搜索指定包别名
mybatis.typeAliasesPackage: com.example.springboot_and_mybatis.*.mapper
# 配置mapper的扫描,找到所有的mapper.xml映射文件
mybatis.mapperLocations: classpath*:mapper/**/*Mapper.xml
# 加载全局的配置文件
mybatis.configLocation: classpath:mapper/mybatis-config.xml
2.创建:控制层,服务层,实现层,接口层,实体类。
控制层:UserController.class
/**
* 接口控制类
*/
@RestController
@RequestMapping("/requestUser")
public class UserController {
@Autowired
private UserService userService;
/**
* 用户注册
*/
@RequestMapping(value = "rEgister.do", method = RequestMethod.POST)
public Map<String, Object> rEgister(String openid, String name, String phone, String password, String code) throws Exception {
return userService.rEgister(openid, name, phone, password, code);
}
}
服务层:UserService.class
/**
* 接口服务类
*/
public interface UserService {
/**
* 用户注册
*/
Map<String, Object> rEgister(String openid, String name, String phone, String password, String code) throws Exception;
}
接口层:UserMapper.class
public interface UserMapper {
int selectCount(@Param(value = "openid") String openid) throws Exception;
/**
* 用户注册
*/
void rEgister(@Param(value = "openid") String openid, @Param(value = "name") String name,
@Param(value = "phone") String phone,
@Param(value = "password") String password, @Param(value = "code") String code);
}
与接口层对应名字的布局: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.example.spingboot_and_mybatis.user.mapper.UserMapper">
<resultMap id="BaseResultMap" type="com.example.spingboot_and_mybatis.user.entity.user">
<!-- <id column="OPENID" property="openid" jdbcType="VARCHAR"/>-->
<!-- <result column="NAME" property="name" jdbcType="VARCHAR"/>-->
<!-- <result column="PHONE" property="phone" jdbcType="VARCHAR"/>-->
<!-- <result column="PASSWORD" property="password" jdbcType="VARCHAR"/>-->
<!-- <result column="CODE" property="code" jdbcType="VARCHAR"/>-->
</resultMap>
<!-- 基础方法 结束 -->
<!-- 扩展方法 开始 -->
<insert id="rEgister" parameterType="com.example.spingboot_and_mybatis.user.entity.user">
INSERT INTO USER (openid,name,phone,password,code) values(#{openid},#{name},#{phone},#{password},#{code})
</insert>
<select id="selectCount" parameterType="String" resultType="int">
SELECT
COUNT(1)
FROM
USER
WHERE
OPENID = #{openid}
</select>
<!-- 扩展方法 结束 -->
</mapper>
实现层:UserServiceImpl.class
/**
* 接口实现类
*/
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public Map<String, Object> rEgister(String openid, String name, String phone, String password, String code) throws Exception {
if (userMapper.selectCount(openid) > 0) {
return Message.getMessage("10", "该微信号已经注册");
}
user u = new user();
u.setOpenid(openid);
u.setName(name);
u.setPhone(phone);
u.setPassword(password);
u.setCode(code);
userMapper.rEgister(openid, name, phone, password, code);
return Message.getMessage("0", "注册成功");
}
}
运行层:
@SpringBootApplication @MapperScan(basePackages = {"com.example.spingboot_and_mybatis.*.mapper"}) public class SpingbootAndMybatisApplication { public static void main(String[] args) { SpringApplication.run(SpingbootAndMybatisApplication.class, args); System.out.println("启动成功!"); } }
注释:记录搭建过程并不全面