springboot整合mybatis的两种方式

https://blog.csdn.net/qq_32719003/article/details/72123917

springboot通过java bean集成通用mapper的两种方式

前言:公司开发的框架基于springboot深度封装,只能使用java bean的方式进行项目配置。

第一种:

1.引入POM坐标,需要同时引入通用mapper和jpa

<dependency>  
            <groupId>tk.mybatis</groupId>  
            <artifactId>mapper</artifactId>  
            <!-- 建议使用最新版本,最新版本请从项目首页查找 -->  
            <version>3.4.0</version>  
        </dependency>  
        <dependency>  
            <groupId>javax.persistence</groupId>  
            <artifactId>persistence-api</artifactId>  
            <version>1.0</version>  
        </dependency>  

2.将自己的mapper文件继承通用mapper的BaseMapper

@Repository  
public interface RatWaiterHitRewardMapper extends BaseMapper<RatWaiterHitReward>{  
}  

3.编写JAVA BEAN配置类

package com.eparty.ccp.rate.config;  
  
import org.springframework.context.annotation.Bean;  
import org.springframework.context.annotation.Configuration;  
import tk.mybatis.mapper.code.Style;  
import tk.mybatis.mapper.common.BaseMapper;  
import tk.mybatis.spring.mapper.MapperScannerConfigurer;  
  
import java.util.Properties;  
  
/** 
 * @auther 张斌 
 * 时间: 2017-5-12 
 */  
@Configuration  
public class TkMybatisConfig {  
  
        @Bean(name="mapperHelper")  
        public MapperScannerConfigurer mapperHelper(){  
            Properties properties = new Properties();  
            properties.setProperty("mappers",BaseMapper.class.getName());  
            properties.setProperty("IDENTITY","MYSQL"); // 数据库方言(主要用于:取回主键的方式)  
            properties.setProperty("notEmpty","false"); // insert和update中,是否判断字符串类型!='',少数方法会用到  
            properties.setProperty("style", Style.camelhump.name());  
  
            MapperScannerConfigurer scan = new MapperScannerConfigurer();  
            scan.setSqlSessionFactoryBeanName("sqlSessionFactory"); // 多数据源时,必须配置  
            scan.setBasePackage("com.eparty.ccp.rate.mapper");//mapper.java文件的路径  
            scan.setMarkerInterface(BaseMapper.class); // 直接继承了BaseDao接口的才会被扫描,basePackage可以配置的范围更大。  
            scan.setProperties(properties);  
  
            return scan;  
        }  
  /*  }*/  
  
}  

配置完毕。

第二种(推荐):

1、配置mybatis 

application.properties(配置文件)
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&rewriteBatchedStatements=false&autoReconnect=true  
spring.datasource.username=root  
spring.datasource.password=root  
spring.datasource.driver-class-name=com.mysql.jdbc.Driver  
  
##指向mapper的xml文件位置  
mybatis.mapper-locations=classpath*:mapper/*Mapper.xml  

2、引入依赖(springboot专用)

<dependency>  
           <groupId>tk.mybatis</groupId>  
           <artifactId>mapper-spring-boot-starter</artifactId>  
           <version>RELEASE</version>  
       </dependency>  

3、配置启动类

package com.eparty.fuxi.abner;  
  
import org.mybatis.spring.annotation.MapperScan;  
import org.springframework.boot.SpringApplication;  
import org.springframework.boot.autoconfigure.SpringBootApplication;  
  
@SpringBootApplication  
@MapperScan(basePackages = "com.eparty.fuxi.abner.mapper")//mapper接口的路径  
public class BootApplication {  
  
    public static void main(String[] args) {  
        SpringApplication.run(BootApplication.class, args);  
    }  
  
}  

到此为止基本配置已经完毕。


4、model类的配置(补充)

类名上加注解



@Table(name = "tb_user")

类的主键上加注解

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)

5、mapper接口的配置(补充)

package com.eparty.fuxi.abner.mapper;  
  
import com.eparty.fuxi.abner.model.auth.user.User;  
import org.springframework.stereotype.Repository;  
import tk.mybatis.mapper.common.Mapper;  
  
@Repository  
public interface UserMapper extends Mapper<User> {  
} 

  所有配置全部完毕。

 

原文地址:https://www.cnblogs.com/a8457013/p/8855648.html