Java-based configuration SSM, eclipse
New maven, web project
....
Project structure:
jar 包
spring and DispatcherServlet context
public class DemoWebApplicationInitializer
extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
// TODO Auto-generated method stub
return new Class<?>[] {RootConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
// TODO Auto-generated method stub
return new Class<?>[] {WebConfig.class};
}
@Override
protected String[] getServletMappings() {
// TODO Auto-generated method stub
return new String[] {"/"};
}
}
DispatcherServlet
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.getword.controller"})
public class WebConfig extends WebMvcConfigurerAdapter {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
resolver.setExposeContextBeansAsAttributes(true);
return resolver;
}
/**
* 静态资源
*/
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
// TODO Auto-generated method stub
configurer.enable();
}
}
spring context
@Configuration
@ComponentScan(basePackages = {"com.getword"},
excludeFilters = {@Filter(type = FilterType.ANNOTATION, value = EnableWebMvc.class)})
@Import(DataSourceConfig.class)
public class RootConfig {
@Bean
public BeanNameAutoProxyCreator autoProxyCreator() {
BeanNameAutoProxyCreator autoProxyCreator = new BeanNameAutoProxyCreator();
autoProxyCreator.setProxyTargetClass(true);
// 设置要创建代理的那些Bean的名字
autoProxyCreator.setBeanNames("*Service");
autoProxyCreator.setInterceptorNames("transactionInterceptor");
return autoProxyCreator;
}
}
DataSourceConfig
@Configuration
@MapperScan("com.getword.dao")
public class DataSourceConfig {
private final static Logger LOG = Logger.getLogger(DataSourceConfig.class);
private String driver = "com.mysql.jdbc.Driver";;
private String url = "jdbc:mysql://localhost:3306/spring?characterEncoding=UTF-8&serverTimezone=UTC";
private String username = "root";
private String password = "123";
@Bean
public BasicDataSource dataSource() {
LOG.info("Initialize the BasicDataSource...");
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
// mybatis的配置
@Bean
public SqlSessionFactoryBean sqlSessionFactoryBean() throws IOException {
ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource());
sqlSessionFactoryBean.setMapperLocations(resourcePatternResolver.getResources("classpath*:mappers/*.xml"));
sqlSessionFactoryBean.setTypeAliasesPackage("com.getword.domain");// 别名,让*Mpper.xml实体类映射可以不加上具体包名
return sqlSessionFactoryBean;
}
// 事务管理器 对mybatis操作数据库事务控制,spring使用jdbc的事务控制类
@Bean(name = "transactionManager")
public DataSourceTransactionManager dataSourceTransactionManager() {
DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
dataSourceTransactionManager.setDataSource(dataSource());
return dataSourceTransactionManager;
}
@Bean(name = "transactionInterceptor")
public TransactionInterceptor interceptor() {
TransactionInterceptor interceptor = new TransactionInterceptor();
interceptor.setTransactionManager(dataSourceTransactionManager());
Properties transactionAttributes = new Properties();
transactionAttributes.setProperty("save*", "PROPAGATION_REQUIRED");
transactionAttributes.setProperty("del*", "PROPAGATION_REQUIRED");
transactionAttributes.setProperty("update*", "PROPAGATION_REQUIRED");
transactionAttributes.setProperty("get*", "PROPAGATION_REQUIRED,readOnly");
transactionAttributes.setProperty("find*", "PROPAGATION_REQUIRED,readOnly");
transactionAttributes.setProperty("*", "PROPAGATION_REQUIRED");
interceptor.setTransactionAttributes(transactionAttributes);
return interceptor;
}
}
Note: mapper namespace must be the full path to a consistent interface! ! !
idea, turn web projects from simple java maven project
New maven project
- New maven project
- Fill out the group id and artifictId, next
- Enter a project name, finish
- Configuring maven, the last step before the second new project
Project results are as follows:
Add the web module
- Project structure -> Modules-> add-> web
- Delete web.xml
- Set up a web project root path
- Adding artifact
- Configuring Tomcat
- At this point the project structure
At this point you can still access the files in the webapp
- jar package, pom.xml
Note: also like to add servlet dependent on the use maven, pay attention to the scope. At this point you can use the servlet
spring configuration
With the eclipse.