SpringBoot框架Day04之jdbc和Mybatis配置注解的纯、混整合

JDBC

  1. 导入jdbc和mysql依赖
  2. 写配置application.yml
#username=root
#password=123456
#url=jdbc:mysql://192.168.58.129:3306/mybatis_0105
#driver=com.mysql.cj.jdbc.Driver
spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://192.168.58.129:3306/mybatis_0105
    driver: com.mysql.cj.jdbc.Driver
  1. 测试jdbcTemplate:

Druid数据源

  1. 导包 引入依赖
		<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.17</version>
        </dependency>
  1. 有点麻烦,未来补。

springboot整合Mybatis操作(yml配置)

  1. 导包 引入依赖
		<dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>
  1. 编写接口

  2. 编写sql映射文件
    注意:此处大坑,resources此根文件创建子包时必须逐层创建,不可用.来进行创建,否规则会报异常

org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.lwt.admin.mapper.EmployeeMapper.save

  1. 在application.yml中指定Mapper配置文件位置,且不需要mybatis.xml全局配置文件,直接配置在configuration下

springboot整合Mybatis操作(纯注解)

@Mapper // 证明是Mapper接口
public interface EmployeeMapper {
    
    

    @Select("select * from t_emp where id=#{id}")
    Employee getById(Integer id);
# 配置mybatis规则
  mybatis:
#    config-location: classpath:mybatis/mybatis-config.xml  #全局配置文件位置,啥时候都可以不写
#    mapper-locations: classpath:mybatis/mapper/*.xml  #sql映射文件位置,使用纯注解时不写
#    在这写
    configuration:
      map-underscore-to-camel-case: true
@Service
public class EmployeeService {
    
    

    @Autowired
    EmployeeMapper employeeMapper;

    public Employee getEmpById(Integer id){
    
    
        return employeeMapper.getById(id);
    }
@Controller
public class MybatisController {
    
    

    @Autowired
    EmployeeService employeeService;
    
    @ResponseBody
    @GetMapping("/emp")
    public Employee getEmpById(@RequestParam("id") Integer id){
    
    
        return employeeService.getEmpById(id);
    }

springboot整合Mybatis操作(混合注解与配置)

application.yml

# 配置mybatis规则
  mybatis:
#    config-location: classpath:/mapper/mybatis-config.xml  #全局配置文件位置,啥时候都可以不写
    mapper-locations: classpath:/mapper/*.xml  #sql映射文件位置,使用纯注解时不写
#    在这写
    configuration:
      map-underscore-to-camel-case: true

dao/mapper

@Mapper // 证明是Mapper接口
public interface EmployeeMapper {
    
    
//    @Insert("insert into t_emp(id,empName,email) values(#{id},#{empName},#{email})")
//    @Options(useGeneratedKeys = true,keyProperty = "id")
    int save(Employee employee);
}

service

public void saveEmpl(Employee employee) {
    
    
        int save = employeeMapper.save(employee);
        System.out.println(save);
    }

handler/controller

	@ResponseBody
    @PostMapping("/emps")
    public Employee saveEmpl(Employee employee){
    
    
        employeeService.saveEmpl(employee);
        return employee;
    }

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="com.lwt.admin.mapper.EmployeeMapper">
    <insert id="save" useGeneratedKeys="true" keyProperty="id">
        insert into t_emp(`empName`,`email`) values(#{empName},#{email})
    </insert>
</mapper>

猜你喜欢

转载自blog.csdn.net/m0_47119598/article/details/113446984