Spring Boot 整合 MyBatis

Spring Boot 整合 MyBatis:

     1,导入 mybatis-spring-boot-starter 依赖:

		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.3.1</version>
		</dependency>

     2,配置数据源相关属性:Spring Boot 整合Druid配置数据源监控

     3,给数据库建表:

   spring:
    datasource:  
        schema:
          - classpath:sql/department.sql
          - classpath:sql/employee.sql

     4,运行项目,确定表已经创建成功!然后将schema属性注释掉(避免项目运行重复创建覆盖数据):

 

     5,创建JavaBean:

public class Employee {

    private Integer id;
    private String lastName;
    private Integer gender;
    private String email;
    private Integer dId;

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

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

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

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

    public void setdId(Integer dId) {
        this.dId = dId;
    }

    public Integer getId() {
        return id;
    }

    public String getLastName() {
        return lastName;
    }

    public Integer getGender() {
        return gender;
    }

    public String getEmail() {
        return email;
    }

    public Integer getdId() {
        return dId;
    }
}
public class Department {

    private Integer id;
    private String departmentName;

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

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

    public Integer getId() {
        return id;
    }

    public String getDepartmentName() {
        return departmentName;
    }
}

     6,创建一个操作数据库的mapper接口加上:@Mapper(或者在项目启动类上加上:@MapperScan(value = "com.atguigu.springboot.mapper"),可以使用MapperScan批量扫描所有的Mapper接口)

//指定这是一个操作数据库的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);

    //是否使用自动生成的主键,哪个属性是封装主键的;刚插入的数据id会封装到department对象返回
    @Options(useGeneratedKeys = true,keyProperty = "id")//否则刚插入的数据id显示为null
    @Insert("insert into department(departmentName) values(#{departmentName})")
    public int insertDept(Department department);

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

or

     7,编写一个测试用的Controller

@RestController
public class DeptController {

    @Autowired
    DepartmentMapper departmentMapper;



    @GetMapping("/dept/{id}")
    public Department getDepartment(@PathVariable("id") Integer id){
        return departmentMapper.getDeptById(id);
    }

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


}

     8,在地址栏插入数据:localhost:8080/dept?departmentName=AA

OK,整合完成了!

将数据库属性大写 N 改成 _n 后,该属性就封装不上了,因为JavaBean里还是大写的N:

     9,自定义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);
            }
        };
    }
}

     10,然后在访问:localhost:8080/dept/3,开启驼峰命名法规则后,又可以封装对应的数据了!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值