spring boot 集成mybatis

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/bgdg
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.initialize=false
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
mybatis.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>
       *
       */
      @Mapper
      public interface AfterSaleMapper {
      	
      	public int selectCategory();
      	
      	
      }

      • 创建Spring Boot主类
      package com.didispace;
      
      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;
      
      
      /**
       *@author 无鳍之鲨
       *
       */
      @SpringBootApplication
      public 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 无鳍之鲨
       *
       */
      @RestController
      public class HelloController {
      	
      	@Autowired
      	AfterSaleMapper afterSaleMapper;
      	
          @GetMapping("/aftersale")
          public int selectCategory() {
          	return afterSaleMapper.selectCategory();
          }
      }




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



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值