spring boot 集成mybatis

来源:互联网 发布:最新域名升级 编辑:程序博客网 时间:2024/06/06 14:12

Spring中整合MyBatis,自己整理了下Spring Boot中整合MyBatis的步骤。下面就来详细介绍如何在Spring Boot中整合MyBatis,并通过注解方式实现映射。

pom.xml中引入依赖

  • 这里用到spring-boot-starter
  • 引入连接mysql的必要依赖mysql-connector-java
  • 引入整合MyBatis的核心依赖mybatis-spring-boot-starter
  • 这里不引入spring-boot-starter-jdbc依赖,是由于mybatis-spring-boot-starter中已经包含了此依赖

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.1.1</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.21</version></dependency></dependencies>


在application.properties中配置mysql的连接配置

# DataSource=========================================================================spring.datasource.url= jdbc:mysql://localhost/bgdgspring.datasource.username=rootspring.datasource.password=123456spring.datasource.initialize=falsespring.datasource.driver-class-name=com.mysql.jdbc.Drivermybatis.mapperLocations=classpath:mapper/*.xml



一定要指定xml文件所在位置!!!

  • 在Mysql中创建aftersale表,。同时,创建映射对象aftersale
  • package com.didispace.entity;import java.math.BigDecimal;import java.util.Date;/** * <p> * 售后信息表 * </p> */public class AfterSale{    /**     * 主键自增     */private Integer id;    /**     * order_goods表主键     */private Integer orderGoodsId;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public Integer getOrderGoodsId() {return orderGoodsId;}public void setOrderGoodsId(Integer orderGoodsId) {this.orderGoodsId = orderGoodsId;}}
    • 创建aftersale映射的操作aftersaleMapper
    • package com.didispace.mapper;import org.apache.ibatis.annotations.Mapper;/** * <p>  * 售后信息表 Mapper 接口 * </p> * */@Mapperpublic interface AfterSaleMapper {public int selectCategory();}

      • 创建Spring Boot主类
      package com.didispace;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;/** *@author 无鳍之鲨 * */@SpringBootApplicationpublic class springbootApplication {public static void main(String[] args) {SpringApplication.run(springbootApplication.class, args);}}

    • 创建一个控制器,注入aftersalemapper,测试一下可不可以查询数据了:
    • package com.didispace.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import com.didispace.entity.AfterSale;import com.didispace.exception.MyException;import com.didispace.mapper.AfterSaleMapper;/** * * @author 无鳍之鲨 * */@RestControllerpublic class HelloController {@AutowiredAfterSaleMapper afterSaleMapper;    @GetMapping("/aftersale")    public int selectCategory() {    return afterSaleMapper.selectCategory();    }}




    • 运行Application.class,启动成功后访问:http://localhost:8080/aftersale,如果数据库有数据就访问成功了