1. SSM整合理解
1.1. IOC两个容器对应组件
容器名 | 盛放组件 |
web容器 | web相关组件(controller,springmvc核心组件) |
root容器 | 业务和持久层相关组件(service,aop,tx,dataSource,mybatis,mapper等) |
- 配置文件
配置名 | 对应内容 | 对应容器 |
WebJavaConfig | controller,springmvc相关 | web容器 |
ServiceJavaConfig | service,aop,tx相关 | root容器 |
MapperJavaConfig | mapper,datasource,mybatis相关 | root容器 |
1.2. IoC初始化方式和配置位置
在web项目下,可以选择web.xml和配置类方式进行ioc配置,推荐配置类。
对于使用基于 web 的 Spring 配置的应用程序,这样做
public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {//指定root容器对应的配置类//root容器的配置类@Overrideprotected Class<?>[] getRootConfigClasses() {return new Class<?>[] { ServiceJavaConfig.class, MapperJavaConfig.class };}//指定web容器对应的配置类 webioc容器的配置类@Overrideprotected Class<?>[] getServletConfigClasses() {return new Class<?>[] { WebJavaConfig.class };}//指定dispatcherServlet处理路径,通常为 / @Overrideprotected String[] getServletMappings() {return new String[] { "/" };}
}
2. SSM整合配置实战
2.1. 控制层配置编写(SpringMVC整合)
/*** projectName: com.wyb.config* * 1.实现Springmvc组件声明标准化接口WebMvcConfigurer 提供了各种组件对应的方法* 2.添加配置类注解@Configuration* 3.添加mvc复合功能开关@EnableWebMvc* 4.添加controller层扫描注解* 5.开启默认处理器,支持静态资源处理*/
@Configuration
@EnableWebMvc
@ComponentScan("com.wyb.controller")
public class WebJavaConfig implements WebMvcConfigurer {//开启静态资源@Overridepublic void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {configurer.enable(); }
}
2.2. 业务层配置编写(AOP / TX整合)
/*** projectName: com.wyb.config* * 1. 声明@Configuration注解,代表配置类* 2. 声明@EnableTransactionManagement注解,开启事务注解支持* 3. 声明@EnableAspectJAutoProxy注解,开启aspect aop注解支持* 4. 声明@ComponentScan("com.atguigu.service")注解,进行业务组件扫描* 5. 声明transactionManager(DataSource dataSource)方法,指定具体的事务管理器*/
@EnableTransactionManagement
@EnableAspectJAutoProxy
@Configuration
@ComponentScan("com.wyb.service")
public class ServiceJavaConfig {@Beanpublic DataSourceTransactionManager transactionManager(DataSource dataSource){DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();transactionManager.setDataSource(dataSource);return transactionManager;}
}
2.3. 持久层配置编写(MyBatis整合)
- mybatis整合思路总结:
- 需要将SqlSessionFactory和Mapper实例加入到IoC容器
- 使用mybatis整合包提供的FactoryBean快速整合
@Bean
public SqlSessionFactory sqlSessionFactory(){//1.读取外部配置文件InputStream ips = Resources.getResourceAsStream("mybatis-config.xml");//2.创建sqlSessionFactorySqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(ips);return sqlSessionFactory;
}
过程比较繁琐,为了提高整合效率,mybatis提供了提供封装SqlSessionFactory和Mapper实例化的逻辑的FactoryBean组件,只需要声明和指定少量的配置即可
SqlSessionFactoryBean源码展示(mybatis提供):
package org.mybatis.spring;
public class SqlSessionFactoryBeanimplements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ContextRefreshedEvent> {//封装了实例化流程public SqlSessionFactory getObject() throws Exception {if (this.sqlSessionFactory == null) {//实例化对象逻辑afterPropertiesSet();}//返回对象逻辑return this.sqlSessionFactory;}
}
- 准备外部配置文件
jdbc.user=root
jdbc.password=root
jdbc.url=jdbc:mysql:///mybatis-example
jdbc.driver=com.mysql.cj.jdbc.Driver
- 整合方式1 (保留mybatis-config.xml)
缺点:依然需要mybatis-config.xml文件,进行xml文件解析,效率偏低!
- mybatis配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><settings><!-- 开启驼峰式映射--><setting name="mapUnderscoreToCamelCase" value="true"/><!-- 开启logback日志输出--><setting name="logImpl" value="SLF4J"/><!--开启resultMap自动映射 --><setting name="autoMappingBehavior" value="FULL"/></settings><typeAliases><!-- 给实体类起别名 --><package name="com.wyb.pojo"/></typeAliases><plugins><plugin interceptor="com.github.pagehelper.PageInterceptor"><!--helperDialect:分页插件会自动检测当前的数据库链接,自动选择合适的分页方式。你可以配置helperDialect属性来指定分页插件使用哪种方言。配置时,可以使用下面的缩写值:oracle,mysql,mariadb,sqlite,hsqldb,postgresql,db2,sqlserver,informix,h2,sqlserver2012,derby(完整内容看 PageAutoDialect) 特别注意:使用 SqlServer2012 数据库时,https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/HowToUse.md#%E5%A6%82%E4%BD%95%E9%85%8D%E7%BD%AE%E6%95%B0%E6%8D%AE%E5%BA%93%E6%96%B9%E8%A8%80--><property name="helperDialect" value="mysql"/></plugin></plugins>
</configuration>
- mybatis和持久层配置类
@Configuration
@PropertySource("classpath:jdbc.properties")
public class MapperJavaConfig {@Value("${jdbc.user}")private String user;@Value("${jdbc.password}")private String password;@Value("${jdbc.url}")private String url;@Value("${jdbc.driver}")private String driver;//数据库连接池配置@Beanpublic DataSource dataSource(){DruidDataSource dataSource = new DruidDataSource();dataSource.setUsername(user);dataSource.setPassword(password);dataSource.setUrl(url);dataSource.setDriverClassName(driver);return dataSource;}/*** 配置SqlSessionFactoryBean,指定连接池对象和外部配置文件即可* @param dataSource 需要注入连接池对象* @return 工厂Bean*/@Beanpublic SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){//实例化SqlSessionFactory工厂SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();//设置连接池sqlSessionFactoryBean.setDataSource(dataSource);//设置配置文件//包裹外部配置文件地址对象Resource resource = new ClassPathResource("mybatis-config.xml");sqlSessionFactoryBean.setConfigLocation(resource);return sqlSessionFactoryBean;}/*** 配置Mapper实例扫描工厂,配置 <mapper <package 对应接口和mapperxml文件所在的包* @return*/@Beanpublic MapperScannerConfigurer mapperScannerConfigurer(){MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();//设置mapper接口和xml文件所在的共同包mapperScannerConfigurer.setBasePackage("com.atguigu.mapper");return mapperScannerConfigurer;}
}
问题:@Value注解读取不到值为null的问题
解决:分成两个配置类独立配置,互不影响,数据库提取一个配置类,mybatis提取一个配置类即可解决!
- 整合方式2
不在保留mybatis的外部配置文件(xml), 所有配置信息(settings、插件、别名等)全部在声明SqlSessionFactoryBean的代码中指定,数据库信息依然使用DruidDataSource实例替代
- mapper配置类
/*** projectName: com.wyb.config* description: 持久层配置和Druid和Mybatis配置 使用一个配置文件*/
@Configuration
public class MapperJavaConfigNew {/*** 配置SqlSessionFactoryBean,指定连接池对象和外部配置文件即可* @param dataSource 需要注入连接池对象* @return 工厂Bean*/@Beanpublic SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){//实例化SqlSessionFactory工厂SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();//设置连接池sqlSessionFactoryBean.setDataSource(dataSource);org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();configuration.setMapUnderscoreToCamelCase(true);configuration.setLogImpl(Slf4jImpl.class);configuration.setAutoMappingBehavior(AutoMappingBehavior.FULL);sqlSessionFactoryBean.setConfiguration(configuration);//typeAliasessqlSessionFactoryBean.setTypeAliasesPackage("com.atguigu.pojo");//分页插件配置PageInterceptor pageInterceptor = new PageInterceptor();Properties properties = new Properties();properties.setProperty("helperDialect","mysql");pageInterceptor.setProperties(properties);sqlSessionFactoryBean.addPlugins(pageInterceptor);return sqlSessionFactoryBean;}/*** 配置Mapper实例扫描工厂,配置 <mapper <package 对应接口和mapperxml文件所在的包* @return*/@Beanpublic MapperScannerConfigurer mapperScannerConfigurer(){MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();//设置mapper接口和xml文件所在的共同包mapperScannerConfigurer.setBasePackage("com.wyb.mapper");return mapperScannerConfigurer;}
}
2.4. 容器初始化配置类
public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {//指定root容器对应的配置类@Overrideprotected Class<?>[] getRootConfigClasses() {return new Class<?>[] {MapperJavaConfig.class, ServiceJavaConfig.class, DataSourceJavaConfig.class };}//指定web容器对应的配置类@Overrideprotected Class<?>[] getServletConfigClasses() {return new Class<?>[] { WebJavaConfig.class };}//指定dispatcherServlet处理路径,通常为 / @Overrideprotected String[] getServletMappings() {return new String[] { "/" };}
}
2.5. 整合测试
- 需求:查询所有员工信息,返回对应json数据
- controller
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {@Autowiredprivate EmployeeService employeeService;@GetMapping("list")public List<Employee> retList(){List<Employee> employees = employeeService.findAll();log.info("员工数据:{}",employees);return employees;}
}
- service
@Service
public class EmployeeServiceImpl implements EmployeeService {@Autowiredprivate EmployeeMapper employeeMapper;/*** 查询所有员工信息*/@Overridepublic List<Employee> findAll() {List<Employee> employeeList = employeeMapper.queryAll();return employeeList;}
}
- mapper
public interface EmployeeMapper {List<Employee> queryAll();
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace等于mapper接口类的全限定名,这样实现对应 -->
<mapper namespace="com.wyb.mapper.EmployeeMapper"><select id="queryAll" resultType="employee"><!-- #{empId}代表动态传入的参数,并且进行赋值!后面详细讲解 -->select emp_id empId,emp_name empName, emp_salary empSalary from t_emp</select>
</mapper>