55.SpringBoot學習筆記--SpringData-整合MyBatis-註解版MyBatis

註解版 MyBatis

demo.yangxu.springboot.mapper.DepartmentMapper

package demo.yangxu.springboot.mapper;

import demo.yangxu.springboot.bean.Department;
import org.apache.ibatis.annotations.*;

//指定這是一個操作數據庫的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);

    //useGeneratedKeys = true使用自動生成的主鍵
    //keyProperty = "id" Department中的id屬性是用來封裝的主鍵
    @Options(useGeneratedKeys = true,keyProperty = "id")
    @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);
}

demo.yangxu.springboot.controller.DeptController

package demo.yangxu.springboot.controller;

import demo.yangxu.springboot.bean.Department;
import demo.yangxu.springboot.mapper.DepartmentMapper;
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 DeptController {

    @Autowired
    DepartmentMapper departmentMapper;

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

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

}

訪問以下網址進行測試:

http://localhost:8080/dept?departmentName=AA
http://localhost:8080/dept/1

自定義 MyBatis 的配置規則,給容器中添加一個 ConfigurationCustomizer

demo.yangxu.springboot.config.MyBatisConfig

package demo.yangxu.springboot.config;

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

@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 接口

demo.yangxu.springboot.Springboot05DataMybatisApplication

package demo.yangxu.springboot;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@MapperScan(value = "demo.yangxu.springboot.mapper")
@SpringBootApplication
public class Springboot05DataMybatisApplication {

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

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章