喜欢用新版,不喜欢xml配置,所以就有了这个东西…除了Maven的pom.xml和Mybatis的mapper是xml的以外,没有xml配置。
所用到的东西:Eclipse JavaEE Luna Service Release 2 (4.4.2),内置Maven。Tomcat 7.0.54、JDK 7u71、Spring 4.1.5、Mybatis 3.2.8
步骤:
一、创建Maven项目
二、修改pom.xml,引入相关包
三、通过Mybatis Generator生成mapper相关文件并引入
四、创建Config相关配置类,实现无xml配置
前三步网上都很容易找到相关的文章,暂时省略。
目前只说第四步,网上绝大部分都是使用传统xml配置的,所以采用java config方式之后,不停的出错,最后终于解决了…
1、CommonInitializer 实现 WebApplicationInitializer,采用java config的第一步
@Order(1)
public class CommonInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) {
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.register(AppConfig.class);
container.addListener(new ContextLoaderListener(rootContext));
AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
dispatcherContext.register(DispatcherConfig.class);
ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
2、AppConfig类,用来设置数据库、扫描mapper等。dbconfig.properties为数据库连接配置,放在resources目录下
@Configuration
@ComponentScan(basePackages = "com.eien.web.service")
@PropertySource("classpath:dbconfig.properties")
@MapperScan("com.eien.dao")
public class AppConfig {
@Autowired
Environment env;
@Bean
public DataSource dataSource() throws SQLException {
BasicDataSource dSource = new BasicDataSource();
dSource.setDriverClassName(env.getProperty("driverClassName"));
dSource.setUrl(env.getProperty("jdbc_url"));
dSource.setUsername(env.getProperty("jdbc_username"));
dSource.setPassword(env.getProperty("jdbc_password"));
dSource.setInitialSize(0);
dSource.setMaxActive(20);
dSource.setMaxIdle(20);
dSource.setMinIdle(0);
dSource.setMaxWait(60000);
return dSource;
}
@Bean
public SqlSessionFactoryBean sqlSessionFactory() throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:com/eien/mapping/*.xml"));
return sessionFactory;
}
@Bean
public DataSourceTransactionManager txManager() throws SQLException {
return new DataSourceTransactionManager(dataSource());
}
}
3、DispatcherConfig,代替Spring MVC中的设置
@Configuration
@ComponentScan(basePackages = "com.eien.web.controller")
public class DispatcherConfig {
@Bean
ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("WEB-INF/jsp/");
resolver.setSuffix(".jsp");
return resolver;
}
}
4、pom.xml中需要配置plugin,否则maven install的时候会提示没有web.xml
<build>
<finalName>maventest</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
最重要的就是这几点配置,后续再详细补充…