代码及配置整合
创建实体类,与数据库对应
创建 mapper、service 和 controller
@Autowired
UserService 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 mapper
PUBLIC "-//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 配置
设置开始驼峰命名,不然数据会显示不出来,以下两种方式选择其一
- 在 mybatis 的 xml 配置文件中修改
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
</configuration>
- 在 application.yml 中修改
mybatis