springboot通過java bean整合通用mapper的兩種方式
阿新 • • 發佈:2019-01-02
前言:公司開發的框架基於springboot深度封裝,只能使用java bean的方式進行專案配置。
2.將自己的mapper檔案繼承通用mapper的BaseMapper
配置完畢。
到此為止基本配置已經完畢。
所有配置全部完畢。
第一種:
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> {
}
所有配置全部完畢。