您的位置:首页 > 移动开发

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

2017-05-15 11:37 561 查看
前言:公司开发的框架基于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
@Repositorypublic 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*/@Configurationpublic 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=truespring.datasource.username=rootspring.datasource.password=rootspring.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;@Repositorypublic interface UserMapper extends Mapper<User> {}所有配置全部完毕。
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息