【SpringBoot学习】14-整合Mybatis3.x(配置文件版)

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值