最近在给学生们讲Spring+Mybatis整合,根据有的学生反映还是基于注解实现整合便于理解,毕竟在先前的工作中团队里还没有人完全舍弃配置文件进行项目开发,由于这两个原因,我索性参考spring官方文档研究出完全基于注解整合ssm框架。毕竟无配置化也是Spring官方所推行的,要不SpringBoot存在的意义为何嘛。
一。整合思路
1)目标:毫无保留的将配置文件的所有配置项改变注解加创建对象的方式实现
2)Spring提供的 @Bean @Configuration @ComponentScan @EnableTransactionManagement @EnableWebMvc 等 需要知道其含义
二。创建spring-mvc的web项目
1) 项目结构目录:
在这里web.xml里不写任何配置
三。在config包下分别创建配置类与属性文件
1. AppConfig.Java
package com.bdqn.lyrk.ssm.study.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
@Configuration
@ComponentScan("com.bdqn")
@EnableTransactionManagement
@MapperScan("com.bdqn.lyrk.ssm.study.mapper")
public class AppConfig {
@Bean
public DataSource druidDataSource(@Autowired PropertyConfig propertyConfig) {
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setUsername(propertyConfig.getUser());
druidDataSource.setPassword(propertyConfig.getPassword());
druidDataSource.setUrl(propertyConfig.getUrl());
druidDataSource.setDriverClassName(propertyConfig.getDriver());
return druidDataSource;
}
@Bean
public SqlSessionFactoryBean sqlSessionFactoryBean(@Autowired DataSource dataSource,@Autowired PropertyConfig propertyConfig) {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setTypeAliasesPackage(propertyConfig.getMybatisTypeAliasPackage());
return sqlSessionFactoryBean;
}
@Bean
public PlatformTransactionManager transactionManager(@Autowired DataSource dataSource) {
PlatformTransactionManager platformTransactionManager = new DataSourceTransactionManager(dataSource);
return platformTransactionManager;
}
}
没什么好说的,这里主要创建Spring与Mybatis整合的相关对象以及声明式事务切面,我们把配置文件中的东西通通用java代码创建,注意@Bean注解的使用
2.DispatcherConfig
package com.bdqn.lyrk.ssm.study.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
import java.util.Properties;
@Configuration
@EnableWebMvc
public class DispatcherConfig extends WebMvcConfigurerAdapter {
@Autowired
private PropertyConfig propertyConfig;
@Bean
public InternalResourceViewResolver internalResourceViewResolver() {
InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
internalResourceViewResolver.setViewClass(JstlView.class);
internalResourceViewResolver.setPrefix(propertyConfig.getWebViewPrefix());
internalResourceViewResolver.setSuffix(propertyConfig.getWebViewSuffix());
return internalResourceViewResolver;
}