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

9 篇文章 0 订阅
5 篇文章 0 订阅

前言:公司开发的框架基于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> {
}

所有配置全部完毕。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值