table of Contents
1. Project integration of resources to achieve
Preface:
笔者是基于注解方式实现的SSM整合
Required for the project depend on:
the required jar package can go https://mvnrepository.com/ download.
1.1 Configuration Architecture Analysis
FIG architecture configuration:
● SpringRepositoryConfig layer is responsible for data configuration
● SpringServiceConfig layer is responsible for service configuration
● SpringWevConfig layer is responsible for handling the request (control and view layers) disposed
● WebInitializer is responsible for initiating the initialization (corresponding web.xml file)
created configuration class :
//数据层配置类
package com.wtl.common.config;
@Configuration
@MapperScan("com.wtl.goods.dao")//扫描dao
public class SpringRepositoryConfig {
}
//业务层配置类
package com.wtl.common.config;
@Configuration
@ComponentScan("com.wtl.goods.service")
public class SpringServiceConfig {
}
//控制层配置类
package com.wtl.common.config;
@Configuration
@EnableWebMvc
@ComponentScan("com.wtl.goods.controller")
public class SpringWebConfig implements WebMvcConfigurer{
}
//启动类配置->web.xml
package com.wtl.common.config;
public class WebInitializer extends
AbstractAnnotationConfigDispatcherServletInitializer {
//Service,Repository
@Override
protected Class<?>[] getRootConfigClasses() {
System.out.println("getRootConfigClasses()");
return new Class[] {SpringRepositoryConfig.class,
SpringServiceConfig.class};
}
//View,Controller
@Override
protected Class<?>[] getServletConfigClasses() {
System.out.println("getServletConfigClasses()");
return new Class[] {SpringWebConfig.class};
}
//过滤
@Override
protected String[] getServletMappings() {
System.out.println("getServletMappings()");
return new String[] {"/"};
}
}
1.2 Integration object connection pool
ps: I used this connection pool is DruidDataSource
Add dataSource in SpringRepositoryConfig class () method, create DruidDataSource objects in the method, and the object is to manage this spring.
@Bean(value="druid",initMethod="initing",destroyMethod="closed")
public DruidDataSource dataSource() {
DruidDataSource ds=new DruidDataSource();
ds.setUrl("jdbc:mysql:///dbgoods?serverTimezone=GMT%2B8");
ds.setUsername("root");
ds.setPassword("root");
return ds;
}
1.3 integration framework mybatis
Add to create a method SqlSessionFactory objects created in SpringRepositoryConfig class.
@Bean("sqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(
DataSource dataSource)
throws Exception {
//创建SqlSessionFactoryBean对象
SqlSessionFactoryBean factoryBean =
new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource);
//调用FactoryBean的getObject方法创建SqlSessionFactory
//底层会使用SqlSessionFactoryBuilder创建
return factoryBean.getObject();
}
1.4 Spring MVC integration module
Add disposed SpringWebConfig view resolution configuration class, the default configuration servlet processing.
package com.wtl.common.config;
@Configuration
@ComponentScan("com.wtl.goods.controller")
@EnableWebMvc //<mvc:annotation-driven/>
public class SpringWebConfig implements WebMvcConfigurer{
//<mvc:default-servlet-handler/>
@Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Override
public void configureViewResolvers(
ViewResolverRegistry registry) {
registry.jsp("/WEB-INF/pages/",".html");
}
}
SSM integration is complete, the next small business partners since you can write a module, I wish you would become a great God.