springboot整合MyBatis(手動註解版)

Spring_Boot專欄
上一篇 主目錄 下一篇

【前言】
本文springboot整合mybatis示例使用阿里的druid連接池(數據源),使用mysql數據庫。具體的配置druid方法見:《springboot2.X配置阿里druid數據源》


1 添加依賴

springboot整合mybatis需要添加mybatis的依賴,在pom.xml文件中添加mybatis的場景啓動器:mybatis-spring-boot-starter(springboot官方的場景啓動器是以spring-boot-starter開頭的,而mybatis的場景啓動器有mybatis提供,以mybatis開頭)

        <!--mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.1</version>
        </dependency>
  • 【注意】配置好druid連接池後,pom.xml有mysql-connector-java、spring-boot-starter-jdbc、druid的依賴。

使用lombok插件自動生成setter()\getter()\toString(),配置lombok依賴(還需要安裝lombok插件):

        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
            <scope>provided</scope>
        </dependency>

2 創建實體類

entities/Department.class

@Data
public class Department {
    private Integer id;
    private String departmentName;
}

3 mapper、service和controller

mapper/DepartmentMapper.java

//@Mapper註解表明這是一個操作數據庫的mapper
@Mapper
@Component
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(departmentName) values(#{departmentName})")
    public int insertDept(Department department);

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

service/DeptService.class

@Service
public class DeptService {
    @Autowired
    DepartmentMapper departmentMapper;

    public Department getDepartment(Integer id){
        return departmentMapper.getDeptById(id);
    }

    public int insertDept(Department department){
        int insertDept = departmentMapper.insertDept(department);
        return insertDept;
    }
}

controller/DeptController.class
@Controller和@RestController的區別?

//@RestController註解相當於@ResponseBody + @Controller合在一起的作用,返回數據不返回頁面
@RestController
public class DeptController {

    @Autowired
    DeptService deptService;

    @GetMapping("/dept/{id}")
    public Department getDepartment(@PathVariable("id") Integer id){
        Department department = deptService.getDepartment(id);
        return department;
    }

    @PostMapping("/dept")
    public int insertDept(Department department){
        int insertDept = deptService.insertDept(department);
        return insertDept;
    }
}

4 postman測試

更多關於postman

在這裏插入圖片描述

在這裏插入圖片描述

5 配置駝峯命名

Department、DepartmentMapper中屬性名稱與數據庫的字段名稱完全一致,但有時有可能不一致,如:departmentName與department_name。這時可以設置開啓駝峯命名的配置,設置方法如下:

config/MyBatisConfig.class中給容器添加ConfigurationCustomizer組件,重寫該組件的customize()方法來設置開啓駝峯命名規則:

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

    @Bean
    public ConfigurationCustomizer configurationCustomizer(){
        return new ConfigurationCustomizer(){
            @Override
            public void customize(Configuration configuration) {
                configuration.setMapUnderscoreToCamelCase(true);
            }
        };
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章