A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

    之前写过一篇简单的免XML的SpringMVC配置,今天补充完整,写一个简单的免XML配置的SSM整合    首先还是写一个基本的javaConfig类
[Java] 纯文本查看 复制代码
@Configuration
@EnableWebMvc  //启动Spring MVC相关配置,相当于继承了WebMvcConfigurationSupport
@ComponentScan(basePackages = { "www.leeao"})  //配置包扫描
@Import(MyServiceConfig.class)  //导入其他配置类
@MapperScan("www.leeao.mapper") //mybatis包扫描
public class MyMvcConfig{

}


   这里引入了另一个配置类MyServiceConfig,这个配置类配置了数据源和mybatis相关配置
[Java] 纯文本查看 复制代码
@Configuration
@PropertySource("classpath:db.properties")
public class MyServiceConfig {
  @Autowired
  private Environment env;

  /**
   * 数据源
   * @return
   */
  @Bean(name = "dataSource", initMethod = "init" , destroyMethod = "close")
  public DataSource getDateSource(){
    DruidDataSource druidDataSource = new DruidDataSource();
    druidDataSource.setUrl(env.getProperty("jdbc_url"));
    druidDataSource.setUsername(env.getProperty("jdbc_user"));
    druidDataSource.setPassword(env.getProperty("jdbc_password"));
    druidDataSource.setInitialSize(env.getProperty("initialSize", Integer.class));
    druidDataSource.setMinIdle(env.getProperty("minIdle", Integer.class));
    druidDataSource.setMaxActive(env.getProperty("maxActive", Integer.class));
    druidDataSource.setMaxWait(env.getProperty("maxWait", Long.class));
    return druidDataSource;
  }

  /**
   *sqlSessionFactory
   * @return
   * @throws Exception
   */
  @Bean(name = "sqlSessionFactory")
  public SqlSessionFactory getSqlSessionFactory() throws Exception {
    ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
    SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
    factoryBean.setDataSource(getDateSource());
    factoryBean.setMapperLocations(resourcePatternResolver.getResources("classpath:mybatis/mapper/*.xml"));
    factoryBean.setTypeAliasesPackage("www.leeao.domain");
    return factoryBean.getObject();
  }

  /**
   * 配置mapper接口的扫描
   * @return
   */
  @Bean
  public static MapperScannerConfigurer setMapperScannerConfigurer(){
    MapperScannerConfigurer scannerConfigurer = new MapperScannerConfigurer();
    scannerConfigurer.setBasePackage("www.leeao.mapper");
    scannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
    return scannerConfigurer;
  }


    其他的action、entity、dao这里就不赘述了,这样,一个简单的免XML的SSM整合就完成啦

1 个回复

倒序浏览
我来占层楼啊
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马