转https://url.cn/5sYnG1c
前言:从spring4.0之后,spring官方推荐使用java类替换之前的xml配置文件,今天使用spring最新版本5.1.5来实现零配置文件搭建ssm框架
1.1 创建maven工程,项目结构图
零配置文件实现ssm(spring+springMVC+Mybatis)框架整合
1.2 添加jar包
4.0.0
com.aaa.qy93
ssm
war
0.0.1-SNAPSHOT
ssm Maven Webapp
http://maven.apache.org
5.1.5.RELEASE
junit
junit
4.12
test
org.springframework
spring-test
5.1.5.RELEASE
test
org.springframework
spring-context
${spring.version}
org.springframework
spring-webmvc
${spring.version}
com.fasterxml.jackson.core
jackson-databind
2.9.8
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-aop
${spring.version}
org.aspectj
aspectjrt
1.9.2
org.aspectj
aspectjweaver
1.9.2
mysql
mysql-connector-java
5.1.40
com.alibaba
druid
1.1.10
org.mybatis
mybatis-spring
1.3.2
org.mybatis
mybatis
3.4.6
javax.servlet
jstl
1.2
ssm
org.apache.maven.plugins
maven-war-plugin
2.6
false
org.apache.maven.plugins
maven-compiler-plugin
3.7.0
1.8
1.3 创建配置类
1.3.1 编写applicationContext.xml =>springConfig.java
package com.aaa.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.EnableTransactionManagement;
/**
* @author TeacherChen
* @description spring容器配置,装所有的bean,除了controller包下面的
* @company AAA软件
* @className QY95
* 2019年4月4日上午9:46:16
*/
@Configuration
@ComponentScan(basePackages= {"com.aaa"},excludeFilters={@Filter(type=FilterType.ANNOTATION,value={Controller.class})})
@EnableAspectJAutoProxy
@EnableTransactionManagement
public class SpringConfig {
}
1.3.2 编写xxx-servlet.xml =>springMVCConfig.java
package com.aaa.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
/**
* @author TeacherChen
* @description springmvc 的配置
* @company AAA软件
* @className QY93
* 2019年3月28日上午9:38:11
*/
@Configuration
@ComponentScan(basePackages= {"com.aaa.controller"})
@EnableWebMvc
//WebMvcConfigurer接口,目的是定制springmvc配置
public class SpringMVCConfig implements WebMvcConfigurer {
/**
* 添加视图解析器
*/
@Bean
public InternalResourceViewResolver getInternalResourceViewResolver() {
InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
internalResourceViewResolver.setPrefix("/WEB-INF/classes/views/");
internalResourceViewResolver.setSuffix(".jsp");
return internalResourceViewResolver;
}
}
1.3.3 编写web.xml =>WebConfig.java
package com.aaa.config;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.DispatcherType;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.request.RequestContextListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.DispatcherServlet;
/**
* @author TeacherChen
* @description web 容器的配置
* @company AAA软件
* @className QY93
* 2019年3月28日上午9:43:06
*/
public class WebConfig implements WebApplicationInitializer{
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
//加载配置类
AnnotationConfigWebApplicationContext ac = new AnnotationConfigWebApplicationContext();
ac.register(SpringConfig.class);
ac.register(SpringMVCConfig.class);
ac.setServletContext(servletContext);
//设置前端控制器
Dynamic addServlet = servletContext.addServlet("xxx", new DispatcherServlet(ac));
addServlet.addMapping("*.do");
//设置servlet的启动优先级
addServlet.setLoadOnStartup(1);
//添加servlet上下文监听
servletContext.addListener(new RequestContextListener());
javax.servlet.FilterRegistration.Dynamic addFilter = servletContext.addFilter("filterEncoding", new CharacterEncodingFilter());
/*
*
encoding
UTF-8
forceEncoding
true
*/
Map map = new HashMap();
map.put("encoding", "UTF-8");
map.put("forceEncoding", "true");
addFilter.setInitParameters(map);//添加初始化参数
EnumSet dispatcherTypes = EnumSet
.allOf(DispatcherType.class);
dispatcherTypes.add(DispatcherType.REQUEST);
dispatcherTypes.add(DispatcherType.FORWARD);
//设置过滤请求的范围
addFilter.addMappingForUrlPatterns(dispatcherTypes, true, "/*");
}
}
零配置文件实现ssm(spring+springMVC+Mybatis)框架整合
1.3.4 编写mybatisConfig.xml => MybatisConfig.java
package com.aaa.config;
import java.io.IOException;
import javax.sql.DataSource;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import com.alibaba.druid.pool.DruidDataSource;
/**
* @author TeacherChen
* @description 跟数据库向关的配置
* @company AAA软件
* @className QY93 2019年3月29日上午11:40:33
*/
@Configuration
public class MybatisConfig {
/**
* 获取数据源对象
*/
@Bean
public DataSource getDataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUsername("root");
dataSource.setPassword("root");
dataSource.setUrl(
"jdbc:mysql://localhost:3306/aaa?useUnicode=true&characterEncoding=gbk&zeroDateTimeBehavior=convertToNull&useSSL=false");
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
return dataSource;
}
/**
* 配置事务管理器
*/
@Bean
public DataSourceTransactionManager getDataSourceTransactionManager() {
DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
dataSourceTransactionManager.setDataSource(getDataSource());
return dataSourceTransactionManager;
}
/**
* 配置mybatis SqlSessionFactoryBean
*/
@Bean("MySqlSessionFactoryBean")
public SqlSessionFactoryBean getSqlSessionFactoryBean() {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
// 设置数据源
sqlSessionFactoryBean.setDataSource(getDataSource());
// 设置环境为开发模式
sqlSessionFactoryBean.setEnvironment("development");
// 设置事务管理方式transactionManager
JdbcTransactionFactory jdbcTransactionFactory = new JdbcTransactionFactory();
sqlSessionFactoryBean.setTransactionFactory(jdbcTransactionFactory);
// 设置别名
sqlSessionFactoryBean.setTypeAliasesPackage("com.aaa.entity");
// 扫描mybatis的mapper映射文件
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
// mapper文件所在的路径
String locationPattern = "mapper/*.xml";
try {
sqlSessionFactoryBean.setMapperLocations(resolver.getResources(locationPattern));
} catch (IOException e) {
e.printStackTrace();
}
return sqlSessionFactoryBean;
}
/**
* 自动扫描使用jdk动态代理 将Mapper接口生成代理注入到Spring 容器中
*/
@Bean
public MapperScannerConfigurer getMapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setBasePackage("com.aaa.dao");
mapperScannerConfigurer.setSqlSessionFactoryBeanName("MySqlSessionFactoryBean");
return mapperScannerConfigurer;
}
}
1.4 创建表
零配置文件实现ssm(spring+springMVC+Mybatis)框架整合
1.5 创建dao,service,controller
1.5.1 编写dao接口
@Mapper
public interface DeptDao {
List findAllDept();
}
1.5.2 编写mapper映射文件
deptno,dname,loc
1.5.3 编写service
public interface DeptBiz {
List findAllDept();
}
@Service
public class DeptBizImpl implements DeptBiz {
@Autowired
private DeptDao deptDao;
@Override
public List findAllDept() {
return deptDao.findAllDept();
}
}
1.5.4 编写controller
@Controller
public class DeptController {
@Autowired
private DeptBiz deptBizImpl;
@RequestMapping("/findAllDept.do")
public String findAllDept(HttpServletRequest req) {
List deptList = deptBizImpl.findAllDept();
req.setAttribute("deptList", deptList);
return "showDept";
}
}
1.6 jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@page isELIgnored="false" %>
显示所有的部门
部门编号
部门名称
部门位置
${dept.deptno}
${dept.dname}
${dept.loc}
1.7 测试
零配置文件实现ssm(spring+springMVC+Mybatis)框架整合
至此,ssm框架整合完美结束,虽然很多企业都切换到springboot,但是仍然有在继续使用ssm的企业,一句话,离开了springMVC,springboot也玩不转是吧!