新建 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")
新建 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;
}
}
开启驼峰命名规则后