对于maven的项目构建和依赖管理在这里先不做详细配置介绍,之后应该会做详细的补充,今天先写写在maven聚合工程中对于SSM的配置整合(提到的其他配置暂不做说明),这里也会贴出整合之后的配置代码,整合过程一笔带过:
整合思路:1、Dao层的整合 2、service层的整合 3、表现层的整合
1、对于Dao层的整合,无非就是对数据库的操作配置,整合之前,在mybatis操作数据库的配置文件中(SqlMapConfig.xml)可以配置通过db.propertie配置数据库连接池(JDBC DBCP C3P0 Druid等),扫描mapper配置文件,
配置SqlSessionFactory,对pojo起别名等(使用逆向工程生成pojo和mapper不必要再起别名)
在spring整合mybatis后,对于mybatis的配置,我们只需在构建项目的dao层中的resourses中配置SqlMapConfig.xml文件即可(是否起别名看个人喜好或者使用逆向工程没必要起),因为对于数据库的操作被spring接管,所以我们在dao层中的resourses中配置applicationContext-dao.xml文件,在此文件中配置dataSource(通过db.properties),sqlSessionFactory,扫描mapper。到此,Dao层的整合大致完善。
SqlMapConfig.xml文件
applicationContext-dao.xml文件
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd ">
2、对于Service层的整合,就是通过spring对service的扫描,但为了脉络的清晰,我们在聚合工程中service层的sources中配置applicationContext-service.xml,在此配置文件中除了约束就是我们对service层的扫描。
其外由于service层是我们操作CURD的层面,所以在service层我们需要一个事务,进而我们就再次配置applicationContext-tx.xml(配置事务管理器,通知,切面等)
applicationContext-service.xml文件
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd ">
applicationContext-tx.xml文件
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
< aop:advisor advice-ref="txAdvice" pointcut="execution(* com.xxx.xxx.service.*.*(..))" />
3、对于表现层的整合,我们体现在springmvc.xml中,其中有对controller的扫描;注解驱动加载处理器映射器,处理器适配器,视图解析器(配置前后缀可简化action返回某个jsp代码),配置拦截与放行,异常处理器,上传文件解析器等
springmvc.xml文件
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd ">
< property name="prefix" value="/WEB-INF/">
最后就是web.xml的配置,作为项目的核心配置,需要配置springmvc前端控制器DispatcherServlet,再通过指定springmvc在classpath的路径加载springmvc;还需加载spring容器并指定applicationContext在classpath下的路径;
另外,web.xml中还需配置解决post乱码的filter
web.xml文件