本文整合mybatis根据传统方式,基于注解的方式在上篇博客中
添加依赖
配置build,解决打包时没有xml文件的问题
在pom.xml文件中添加如下内容
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
- 配置文件
#mybatis的配置
# 需要在resources目录下新建mapper文件夹存放xml
mybatis:
mapper-locations: classpath:mapper/*.xml
- mapper接口
Account findAccountById(int id);
对应的xml
<select id="findAccountById" resultType="com.bean.Account" parameterType="int">
SELECT * from account where id= #{id}
</select>
- controller
@RequestMapping("/findAccountById")
public Account findAccountById(int id){
return accountService.findAccountById(id);
}
结果:{“id”:2,”name”:”bbb”,”money”:1000.0}