首页 技术 正文
技术 2022年11月15日
0 收藏 426 点赞 3,461 浏览 2985 个字

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.</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;
}
/* }*/ }

配置完毕。

第二种(推荐):、配置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> {
}

  所有配置全部完毕。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,105
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,582
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,429
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,836
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,919