当前位置: 首页 > >

spring boot进行mybatis和JPA的整合

发布时间:

[size=large][color=red][b]初识spring boot[/b][/color][/size]

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
this.propertyResolver = new RelaxedPropertyResolver(env, "spring.datasource.");
@Bean(name="dataSource", destroyMethod = "close", initMethod="init")
@Primary
public DataSource dataSource() {
DruidDataSource datasource = new DruidDataSource();
datasource.setUrl(propertyResolver.getProperty("url"));
datasource.setDriverClassName(propertyResolver.getProperty("driverClassName"));
datasource.setUsername(propertyResolver.getProperty("username"));
datasource.setPassword(propertyResolver.getProperty("password"));
datasource.setValidationQuery("select count(1) from dual");
return datasource;
}
@ConditionalOnMissingBean
public SqlSessionFactory sqlSessionFactory() {
try {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setTypeAliasesPackage("com.xxx.entity");
//添加XML目录
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
sessionFactory.setMapperLocations(resolver.getResources("classpath:com/xxx/**/mapper/*.xml"));
return sessionFactory.getObject();
} catch (Exception e) {
return null;
}
}
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
@Bean
@ConditionalOnMissingBean
public DataSourceTransactionManager transactionManager() {
return new DataSourceTransactionManager(dataSource());
}
@Select("select count(1) from xx ")
int xxxxxxx();
@Select("select * from xx where bui_id=#{xxxx}")
xxx xxxxxxxx(@Param("xxx") String xxx);
private static final Logger logger = LoggerFactory.getLogger(TestController.class);
@Resource
private MyService myService ;
@Resource
private xxxMapper xxxx;
@Resource
private xxxRepository xxxRepository;
@RequestMapping("/xxx")
public xxxx findzCC(@RequestParam String buiId){
System.out.println(xxxx.xxxx(buiId));
return xxx.findLanwx(buiId);
}

}





相关资源:springboot同时整合JPAmybatis的demo



友情链接: