代码及配置整合
创建实体类,与数据库对应
创建 mapper、service 和 controller
@AutowiredUserService userService;@ResponseBody@GetMapping("/user")public com.vazquez.bootstudy.model.User getById(@RequestParam("id") Long id) {return userService.getById(id);}
创建 sql xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.vazquez.bootstudy.mapper.UserMapper"><select id="getUser" resultType="com.vazquez.bootstudy.model.User">select * from user where id = #{id}</select>
</mapper>
修改 mybatis 配置
设置开始驼峰命名,不然数据会显示不出来
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><settings><setting name="mapUnderscoreToCamelCase" value="true"/></settings>
</configuration>