不想做伸手党,这次实在没办法;
新项目需要用到mybatis,之前公司一直用springMvc+hibernate,
都是用一个WebApplicationInitializer实现类+WebMvcConfiguration实现类
用纯注解方式配置。
然而自己只会用xml配置mybatis,找得到注解写mybatis的sql语句,
但实在找不到SpringMVC+MyBatis的配置注解,百度的净是些挂羊头卖狗肉的
xml配置,
只能求助于各位道友了?
public class MybatisConfiguration {
public Configuration mybatisConfig() {
Configuration configuration = new Configuration();
configuration.setCacheEnabled(true);
configuration.setLazyLoadingEnabled(true);
configuration.setAggressiveLazyLoading(true);
configuration.setMultipleResultSetsEnabled(true);
configuration.setUseColumnLabel(true);
configuration.setUseGeneratedKeys(false);
configuration.setAutoMappingBehavior(AutoMappingBehavior.PARTIAL);
configuration.setDefaultExecutorType(ExecutorType.SIMPLE);
configuration.setMapUnderscoreToCamelCase(true);
configuration.setLocalCacheScope(LocalCacheScope.SESSION);
configuration.setJdbcTypeForNull(JdbcType.NULL);
configuration.getTypeAliasRegistry().registerAlias("Page", Page.class);
// PageHelper pager = new PageHelper();
PageInterceptor pageInterceptor = new PageInterceptor();
Properties p = new Properties();
p.put("helperDialect", "mysql");
p.put("offsetAsPageNum", "true");
p.put("rowBoundsWithCount", "true");
p.put("pageSizeZero", "true");
p.put("reasonable", "true");
pageInterceptor.setProperties(p);
configuration.addInterceptor(pageInterceptor);
return configuration;
}
}
/**
* ClassName: DataSourceConfiguration <br/>
* Function: TODO ADD FUNCTION. <br/>
* Reason: TODO ADD REASON(可选). <br/>
* date: 2016年4月7日 下午1:50:50 <br/>
*
* @author zengjing
* @version
* @since JDK 1.7
*/
@Configuration
@MapperScan(basePackages={"com.changan.anywhere.showcase"}, annotationClass = MyBatisDao.class)
public class DataSourceConfiguration implements TransactionManagementConfigurer {
@Value("${spring.datasource.driverClassName}")
private String driverClassName;
@Value("${spring.datasource.url}")
private String url;
@Value("${spring.datasource.username}")
private String username;
@Value("${spring.datasource.password}")
private String password;
private static Logger log = LoggerFactory.getLogger(DataSourceConfiguration.class);
//@ConfigurationProperties(prefix="spring.datasource")
public DataSource datasource() {
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName(driverClassName);
druidDataSource.setUrl(url);
druidDataSource.setUsername(username);
druidDataSource.setPassword(password);
try {
druidDataSource.setFilters("stat, wall");
} catch (SQLException e) {
e.printStackTrace();
}
return druidDataSource;
}
@Bean(name = "dynamicDataSource")
public DynamicDataSource dynamicDataSource() {
DynamicDataSource dynamicDataSource = new DynamicDataSource();
dynamicDataSource.setMasterDataSource(datasource());
return dynamicDataSource;
}
@Bean(name = "sqlSessionFactory")
public SqlSessionFactory sqlSessionFactoryBean() throws Exception {
log.info("sqlSessionFactory");
SqlSessionFactoryBean sessionBean = new SqlSessionFactoryBean();
sessionBean.setDataSource(dynamicDataSource());
sessionBean.setTypeAliasesPackage("com.changan.anywhere.showcase");
sessionBean.setTypeAliasesSuperType(BaseEntity.class);
// SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
sessionBean.setMapperLocations(resolver.getResources("classpath:/mappings/*.xml"));
// SqlSessionFactory sessionFactory = builder.build(new MybatisConfiguration().mybatisConfig());
sessionBean.setConfiguration(new MybatisConfiguration().mybatisConfig());
SqlSessionFactory sessionFactory = sessionBean.getObject();
return sessionFactory;
}
@Bean(name = "transactionManager")
public DataSourceTransactionManager transactionManager() {
DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
transactionManager.setDataSource(dynamicDataSource());
return transactionManager;
}
@Bean
@Override
public PlatformTransactionManager annotationDrivenTransactionManager() {
return transactionManager();
}
}
http://blog.csdn.net/leemllian/article/details/24801489
用纯注解方式,你自己要写很多代码,比如扫描注解之类的,所以还是会配置少许的。
这篇文章是基于Spring boot的纯注解方式引入mybatis,你可以参考哈,原理应该都差不多吧。
http://blog.csdn.net/sun1021873926/article/details/75138982
http://blog.csdn.net/hj7jay/article/details/51907652 看看这篇
这个好评很高,,可以试试:
http://blog.csdn.net/yakson/article/details/44832967