不想做伸手党,这次实在没办法;
新项目需要用到mybatis,之前公司一直用springMvc+hibernate,
都是用一个WebApplicationInitializer实现类+WebMvcConfiguration实现类
用纯注解方式配置。
然而自己只会用xml配置mybatis,找得到注解写mybatis的sql语句,
但实在找不到SpringMVC+MyBatis的配置注解,百度的净是些挂羊头卖狗肉的
xml配置,
只能求助于各位道友了?
求,SpringMVC+MyBatis免xml的纯java注解配置方式?
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
6条回答 默认 最新
- 夕阳雨晴 2017-08-16 03:35关注
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(); } }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 有没有整苹果智能分拣线上图像数据
- ¥20 有没有人会这个东西的
- ¥15 cfx考虑调整“enforce system memory limit”参数的设置
- ¥30 航迹分离,航迹增强,误差分析
- ¥15 Chrome Manifest扩展引用Ajax-hook库拦截请求失败
- ¥15 用Ros中的Topic通讯方式控制小乌龟的速度,走矩形;编写订阅器代码
- ¥15 LLM accuracy检测
- ¥15 pycharm添加远程解释器报错
- ¥15 如何让子窗口鼠标滚动独立,不要传递消息给主窗口
- ¥15 如何能达到用ping0.cc检测成这样?如图