sin_404
sin_404
采纳率83.3%
2017-08-16 03:06 阅读 2.1k
已采纳

求,SpringMVC+MyBatis免xml的纯java注解配置方式?

25

不想做伸手党,这次实在没办法;
新项目需要用到mybatis,之前公司一直用springMvc+hibernate,
都是用一个WebApplicationInitializer实现类+WebMvcConfiguration实现类
用纯注解方式配置。
然而自己只会用xml配置mybatis,找得到注解写mybatis的sql语句,
但实在找不到SpringMVC+MyBatis的配置注解,百度的净是些挂羊头卖狗肉的
xml配置,
只能求助于各位道友了?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

6条回答 默认 最新

  • 已采纳
    sun1021873926 夕阳雨晴 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();
        }
    
    }
    
    点赞 评论 复制链接分享
  • qq_35728177 Tsui丶 2017-08-16 03:09
  • bz151531223 牛坛子 2017-08-16 03:15

    用纯注解方式,你自己要写很多代码,比如扫描注解之类的,所以还是会配置少许的。

    点赞 评论 复制链接分享
  • sun1021873926 夕阳雨晴 2017-08-16 03:28

    这篇文章是基于Spring boot的纯注解方式引入mybatis,你可以参考哈,原理应该都差不多吧。
    http://blog.csdn.net/sun1021873926/article/details/75138982

    点赞 评论 复制链接分享
  • q690080900 举头三尺有宋浩 2017-08-16 07:20
    点赞 评论 复制链接分享
  • Small_Mouse0 鼠小 2017-08-16 11:41

    这个好评很高,,可以试试:
    http://blog.csdn.net/yakson/article/details/44832967

    点赞 评论 复制链接分享

相关推荐