引入依赖
<!--整合mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
在application.yml添加配置
# 配置mybatis规则
mybatis:
# config-location: classpath:mybatis/mybatis-config.xml #config-location与configuration配置不能同时存在
mapper-locations: classpath:mybatis/mapper/*.xml
configuration: #指定mybatis全局配置文件中的相关配置项
map-underscore-to-camel-case: true #开启驼峰命名规则
测试访问
准备测试代码
@Data
public class Account {
private Integer id;
private String contact;
private String addressDesc;
private String postCode;
private String tel;
}
@Mapper
public interface AccountMapper {
public Account getAcct(Integer id);
}
<?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.atguigu.admin.mapper.AccountMapper">
<select id="getAcct" resultType="com.atguigu.admin.bean.Account">
select * from user_account where id = #{id}
</select>
</mapper>
@Service
public class AccountService {
@Autowired
AccountMapper accountMapper;
public Account getAcctById(Integer id){
return accountMapper.getAcct(id);
}
}
@ResponseBody
@GetMapping("/acct")
public Account getById(@RequestParam("id") Integer id){
return accountService.getAcctById(id);
}
localhost:8080/acct