1、配置文件yml
除了11中的 基本datasource 和 druid 配置外
需要指定mybatis的基本配置
mapper 以及 config 的文件位置
mybatis:
config-location: classpath:mybatis/mybatis-config.xml
mapper-locations: classpath:mybatis/mapper/*.xml
2、mapper接口
/**
* @author AnQi
* @date 2020/3/8 10 30:46
* @description
*/
@Mapper
public interface BillMapper {
Bill getBillById(Integer bid);
int addBill(Bill bill);
}
3、然后在resources下创建 mybatis相关的 config 和 mapper.xml 文件
4、写mapper.xml 以及 对 mybatis 进行配置(例:开启驼峰命名)
config配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--核心配置文件-->
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
</configuration>
mapper.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="cn.qqqking.springboot.mapper.BillMapper">
<select id="getBillById" resultType="cn.qqqking.springboot.entities.Bill">
select * from bill where bid=#{bid}
</select>
<insert id="addBill">
insert into bill(bill_code,bill_name,bill_com,bill_num)values
(#{billCode},#{billName},#{billCom},#{billNum})
</insert>
</mapper>
5、controller层
/**
* @author AnQi
* @date 2020/3/8 10 44:11
* @description
*/
@RestController
public class BillController {
@Autowired
BillMapper billMapper;
@GetMapping("/bill/{bid}")
public Bill getBill(@PathVariable("bid") Integer bid){
Bill bill = billMapper.getBillById(bid);
return bill;
}
@GetMapping("/bill")
public Bill addBill(Bill bill){
int i = billMapper.addBill(bill);
return bill;
}
}
OK