Springboot--整合MyBatis

新建 Module
在这里插入图片描述
在这里插入图片描述
依赖 阿里 Druid 导入依赖
参考springboot–整合JDBC
resources/application.yml 同位置复制到此次新建的项目中
com.iris.springboot.config.DruidConfig.java 同位置复制到此次新建的项目中
新建 com.iris.springboot.mapper.ProviderMapper.java

package com.iris.springboot.mapper;

/*使用Mybatis注解版本*/

import com.iris.springboot.entities.Provider;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

@Repository
@Mapper
public interface ProviderMapper {

    //查询操作
    @Select("select * from provider where pid=#{pid}")
    Provider getProviderByPid(Integer pid);
    //是否使用自增组件,keyProperty 指定哪个属性封装组件值
    @Options(useGeneratedKeys = true,keyProperty = "pid")
    //插入操作
    @Insert("insert into provider(providerName) values(#{providerName}) ")
    int addProvider(Provider provider);

    //删除操作
    @Delete("delete from provider where pid=#{pid}")
    int deleteProviderByPid(Integer pid);

    //更新操作
    @Update("update provider set provider=#{providerName} where pid=#{pid}")
    int updateProvider(Provider provider);
}

新建 控制层 com.iris.springboot.controller.ProviderController.java

package com.iris.springboot.controller;


import com.iris.springboot.entities.Provider;
import com.iris.springboot.mapper.ProviderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ProviderController {
    @Autowired
    ProviderMapper providerMapper;

    @GetMapping("/provider/{pid}")
    public Provider getProvider(@PathVariable("pid") Integer pid){
        Provider provider = providerMapper.getProviderByPid(pid);
        return provider;
    }

    @GetMapping("/provider")
    public Provider addProvider(Provider provider){
        providerMapper.addProvider(provider);
        return provider;
    }
}

查询数据
在这里插入图片描述
解决provider_code 问题 驼峰命名方式
新建 com.iris.springboot.config.MybatisConfig.java

package com.iris.springboot.config;

import org.mybatis.spring.boot.autoconfigure.ConfigurationCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyBatisConfig {
    @Bean
    public ConfigurationCustomizer configurationCustomizer(){
        ConfigurationCustomizer configurationCustomizer = new ConfigurationCustomizer() {
            @Override
            public void customize(org.apache.ibatis.session.Configuration configuration) {
                //开启驼峰命名方式
                configuration.setMapUnderscoreToCamelCase(true);
            }
        };
        return configurationCustomizer;
    }
}

在这里插入图片描述
插入数据
在这里插入图片描述
在这里插入图片描述


使用 @MapperScan() 自动装配指定包下所有Mapper, 省得在每个Mappe接口上写 @Mapper

在服务器启动页面com.iris.springboot.springboot08MybatisApplication.hava

@MapperScan("com.iris.springboot.mapper")

MyBatis官网参考中文文档

新建 com.iris.springboot.mapper.BillMapper.java

package com.iris.springboot.mapper;
import com.iris.springboot.entities.Bill;
import org.springframework.stereotype.Repository;

@Repository
//Mapper 或 MapperScan
public interface BillMapper {
    Bill getBillByBid(Integer bid);

    int addBill(Bill bill);
}

新建 配置文件 resources/mybatis/mybatis-config.xml

<?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>

新建 映射配置文件 resource/mybatis/mapper/BillMapper.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="com.iris.springboot.mapper.BillMapper">

    <select id="getBillByBid" resultType="com.iris.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>

resources/application.yml 中新增 mybatis 路径位置

#配置mybatis相关文件路径
mybatis:
  #映射配置文件路径
  mapper-locations: classpath:mybatis/mapper/*.xml
  #核心配置文件路径
  config_location: classpath:mybatis/mybatis-config.xml

新增 com/iris/springboot/controller/BillController.java 控制层

package com.iris.springboot.controller;

import com.iris.springboot.entities.Bill;
import com.iris.springboot.mapper.BillMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class BillController {

    @Autowired
    BillMapper billMapper;

    @GetMapping("/bill/{bid}")
    public Bill getBill(@PathVariable("bid") Integer bid){
        Bill bill = billMapper.getBillByBid(bid);
        return bill;
    }
    @GetMapping("/bill")
    public Bill addBill(Bill bill){
        int i = billMapper.addBill(bill);
        return bill;
    }
}

在这里插入图片描述
开启驼峰命名规则后
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值