springboot整合mybatis

springboot整合mybatis

      • 准备:
      • 1.数据源配置见上篇:[SpringBoot2.0x整合Druid数据源以及Druid监控](https://blog.csdn.net/qq_43640120/article/details/106529603)
      • 2.建立数据库表
      • 3.创建Javabean
      • 整合场景搭建
      • 1.环境搭建:
        • 1.1启动场景选:
        • 1.2配置文件yml基本配置:
      • 2.注解开发
        • 开启驼峰式命名规则的扫描
        • 使用MapperScan批量扫描所有的Mapper接口
      • 3.配置文件开发
        • Yml文件配置
        • **提示一下IDEA建立mapper和config文件模板可以参我的另一篇博文:**
        • EmployeeMapper.xml
        • Mybatis-config.xml
        • Controller层调用测试

准备:

1.数据源配置见上篇:SpringBoot2.0x整合Druid数据源以及Druid监控

2.建立数据库表

springboot整合mybatis_第1张图片
去除注释#键运行。数据库建表成功后注释掉

3.创建Javabean

//1.Employee 
public class Employee {
    private Integer id;
    private String lastName;
    private  Integer gender;
    private  String email;
    private  Integer dId;
    
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Integer getGender() {
        return gender;
    }

    public void setGender(Integer gender) {
        this.gender = gender;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Integer getdId() {
        return dId;
    }

    public void setdId(Integer dId) {
        this.dId = dId;
    }
}
//2.Department 
public class Department {
    private Integer id;
    private String departmentName;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getDepartmentName() {
        return departmentName;
    }

    public void setDepartmentName(String departmentName) {
        this.departmentName = departmentName;
    }
}

整合场景搭建

1.环境搭建:

简单介绍一下

1.1启动场景选:

jdbc(链接场景),mybatis,数据源配置druid
基本pom文件中依赖


    
        org.springframework.boot
        spring-boot-starter-thymeleaf
    
    
        org.springframework.boot
        spring-boot-starter-web
    

   
    
        org.mybatis.spring.boot
        mybatis-spring-boot-starter
        2.1.2
    
    
    
        mysql
        mysql-connector-java
        5.1.6
    
    
    
        com.alibaba
        druid
        1.0.9
    

1.2配置文件yml基本配置:

见SpringBoot2.0x整合Druid数据源以及Druid监控

2.注解开发



//指定这是一个操作数据库的mapper
//@Mapper
public interface DepartmentMapper {

    @Select("select * from department where id=#{id}")
    public Department getDeptById(Integer id);

    @Delete("delete from department where id=#{id}")
    public int deleteDeptById(Integer id);

    //开启自动使用自动生成的主键
    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into department(department_name) values(#{departmentName})")
    public int insertDept(Department department);

    @Update("update department set department_name=#{departmentName} where id=#{id}")
    public int updateDept(Department department);
}

开启驼峰式命名规则的扫描

自定义MyBatis的配置规则;给容器中添加一个ConfigurationCustomizer;

@org.springframework.context.annotation.Configuration
public class MybatisConfig {

    @Bean
    public ConfigurationCustomizer configurationCustomizer(){

        return new ConfigurationCustomizer(){

            @Override
            public void customize(Configuration configuration) {
                configuration.setMapUnderscoreToCamelCase(true);
            }
        };
    }
}

使用MapperScan批量扫描所有的Mapper接口

@MapperScan(value = "com.nadou.springboot.mapper")
@SpringBootApplication
public class SpringBoot06MybatisApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBoot06MybatisApplication.class, args);
    }

}

3.配置文件开发

Yml文件配置

mybatis:
 config-location: classpath:mybatis/mybatis-config.xml   指定全局配置文件位置
 mapper-locations: classpath:mybatis/mapper/*.xml    指定sql映射文件位置

springboot整合mybatis_第2张图片

提示一下IDEA建立mapper和config文件模板可以参我的另一篇博文:

IDEA中resources包下mybatis主配置文件config与映射配置文件mapper:
https://blog.csdn.net/qq_43640120/article/details/105788116

EmployeeMapper.xml





    
    
    
    


Mybatis-config.xml

开启驼峰式命名规则的映射




    
        
    


Controller层调用测试

@GetMapping("/dept")
public Department insertDept(Department department){
    departmentMapper.insertDept(department);
    return department;
}

你可能感兴趣的:(SpringBoot)