SSM————ssm框架整合(maven开发)
配置文件:参考:https://www.cnblogs.com/wxisme/p/4924561.html
一、整合思路
1、Dao层:
Mybatis的配置文件:
SqlConfig.xml:
不需要配置任何内容,需要有文件头。文件必须存在
applicationContext-dao.xml:
mybatis整合spring,通过由spring创建数据库连接池,spring管理SqlSessionFactory、mapper代理对象。需要mybatis和spring的整合包。
2、Service层
applicationContext-service.xml:
所有的service实现类都放到spring容器中管理。并由spring管理事务。
3、表现层
Springmvc框架,由springmvc管理controller。
Springmvc的三大组件。
二、Dao整合
1、在web组件下创建mybatis和spring的配置文件
、

2、创建SqlMapConfig.xml配置文件
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 7 </configuration>
3、创建applicationContext-dao.xml(Spring整合mybatis)
2 11 <?xml version="1.0" encoding="UTF-8"?> 3 12 <beans xmlns="http://www.springframework.org/schema/beans" 4 13 xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" 5 14 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 6 15 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 7 16 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 8 17 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd 9 18 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 10 19 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd"> 11 20 12 21 <!-- 数据库连接池 --> 13 22 <!-- 加载配置文件 --> 14 23 <context:property-placeholder location="classpath:conf/db.properties" /> 15 24 <!-- 数据库连接池 --> 16 25 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" 17 26 destroy-method="close"> 18 27 <property name="url" value="${jdbc.url}" /> 19 28 <property name="username" value="${jdbc.username}" /> 20 29 <property name="password" value="${jdbc.password}" /> 21 30 <property name="driverClassName" value="${jdbc.driver}" /> 22 31 <property name="maxActive" value="10" /> 23 32 <property name="minIdle" value="5" /> 24 33 </bean> 25 34 <!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 --> 26 35 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 27 36 <!-- 数据库连接池 --> 28 37 <property name="dataSource" ref="dataSource" /> 29 38 <!-- 加载mybatis的全局配置文件 --> 30 39 <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" /> 31 40 </bean> 32 41 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 33 42 <property name="basePackage" value="cn.e3mall.mapper" /> 34 43 </bean> 35 44 </beans>
db.properties

1 jdbc.driver=com.mysql.jdbc.Driver 2 jdbc.url=jdbc:mysql://localhost:3306/e3mall?characterEncoding=utf-8 3 jdbc.username=root 4 jdbc.password=root
备注:
Druid是目前最好的数据库连接池,在功能、性能、扩展性方面,都超过其他数据库连接池,包括DBCP、C3P0、BoneCP、Proxool、JBoss DataSource。
Druid已经在阿里巴巴部署了超过600个应用,经过多年多生产环境大规模部署的严苛考验。
三、Service整合
1、管理Service

创建applicationContext-service
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 5 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 7 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd 8 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 9 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd"> 10 11 <context:component-scan base-package="cn.e3mall.service"/> 12 </beans>
创建接口

配实现类

2、事务管理

创建applicationContext-trans.xml
1 <beans xmlns="http://www.springframework.org/schema/beans" 2 xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" 3 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 6 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd 7 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 8 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd"> 9 <!-- 事务管理器 --> 10 <bean id="transactionManager" 11 class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 12 <!-- 数据源 --> 13 <property name="dataSource" ref="dataSource" /> 14 </bean> 15 <!-- 通知 --> 16 <tx:advice id="txAdvice" transaction-manager="transactionManager"> 17 <tx:attributes> 18 <!-- 传播行为 --> 19 <tx:method name="save*" propagation="REQUIRED" /> 20 <tx:method name="insert*" propagation="REQUIRED" /> 21 <tx:method name="add*" propagation="REQUIRED" /> 22 <tx:method name="create*" propagation="REQUIRED" /> 23 <tx:method name="delete*" propagation="REQUIRED" /> 24 <tx:method name="update*" propagation="REQUIRED" /> 25 <tx:method name="find*" propagation="SUPPORTS" read-only="true" /> 26 <tx:method name="select*" propagation="SUPPORTS" read-only="true" /> 27 <tx:method name="get*" propagation="SUPPORTS" read-only="true" /> 28 </tx:attributes> 29 </tx:advice> 30 <!-- 切面 --> 31 <aop:config> 32 <aop:advisor advice-ref="txAdvice" 33 pointcut="execution(* cn.e3mall.service.*.*(..))" /> 34 </aop:config> 35 </beans
四、表现层整合
1、Springmvc.xml

1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 7 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd 8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"> 9 10 <context:component-scan base-package="cn.e3mall.controller" /> 11 <mvc:annotation-driven /> 12 <bean 13 class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 14 <property name="prefix" value="/WEB-INF/jsp/" /> 15 <property name="suffix" value=".jsp" /> 16 </bean> 17 </beans>
2、Web.xml

1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns="http://java.sun.com/xml/ns/javaee" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 5 id="WebApp_ID" version="2.5"> 6 <display-name>e3-manager</display-name> 7 <welcome-file-list> 8 <welcome-file>index.jsp</welcome-file> 9 </welcome-file-list> 10 11 <!-- 加载spring容器 --> 12 <context-param> 13 <param-name>contextConfigLocation</param-name> 14 <param-value>classpath:spring/applicationContext*.xml</param-value> 15 </context-param> 16 <listener> 17 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 18 </listener> 19 20 <!-- 解决post乱码 --> 21 <filter> 22 <filter-name>CharacterEncodingFilter</filter-name> 23 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 24 <init-param> 25 <param-name>encoding</param-name> 26 <param-value>utf-8</param-value> 27 </init-param> 28 </filter> 29 <filter-mapping> 30 <filter-name>CharacterEncodingFilter</filter-name> 31 <url-pattern>/*</url-pattern> 32 </filter-mapping> 33 34 35 <!-- springmvc的前端控制器 --> 36 <servlet> 37 <servlet-name>e3-manager</servlet-name> 38 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 39 <!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation, springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" --> 40 <init-param> 41 <param-name>contextConfigLocation</param-name> 42 <param-value>classpath:spring/springmvc.xml</param-value> 43 </init-param> 44 <load-on-startup>1</load-on-startup> 45 </servlet> 46 <servlet-mapping> 47 <servlet-name>e3-manager</servlet-name> 48 <url-pattern>/</url-pattern> 49 </servlet-mapping> 50 </web-app>
五、整合测试
1、需求
根据商品id查询商品信息,返回json数据。
2、Dao层
由于是单表查询可以使用逆向工程生成的代码。
3、Service层
参数:商品id
返回值:TbItem
业务逻辑:根据商品id查询商品信息。
1 /** 2 * 商品管理Service 3 * <p>Title: ItemServiceImpl</p> 4 * <p>Description: </p> 5 * <p>Company: www.itcast.cn</p> 6 * @version 1.0 7 */ 8 @Service 9 public class ItemServiceImpl implements ItemService { 10 11 @Autowired 12 private TbItemMapper itemMapper; 13 14 @Override 15 public TbItem getItemById(long id) { 16 TbItem item = itemMapper.selectByPrimaryKey(id); 17 return item; 18 } 19 20 }
4、Controller
1 /** 2 * 商品管理Controller 3 * <p>Title: ItemController</p> 4 * <p>Description: </p> 5 * <p>Company: www.itcast.cn</p> 6 * @version 1.0 7 */ 8 @Controller 9 public class ItemController { 10 11 @Autowired 12 private ItemService itemService; 13 14 @RequestMapping("/item/{itemId}") 15 @ResponseBody 16 private TbItem getItemById(@PathVariable Long itemId) { 17 TbItem tbItem = itemService.getItemById(itemId); 18 return tbItem; 19 } 20 }
六、解决mappe绑定异常
原因:

此异常的原因是由于mapper接口编译后在同一个目录下没有找到mapper映射文件而出现的。由于maven工程在默认情况下src/main/java目录下的mapper文件是不发布到target目录下的。
解决方法:
在e3-manager-dao工程的pom文件中添加如下内容:
1 <!-- 如果不添加此节点mybatis的mapper.xml文件都会被漏掉。 --> 2 <build> 3 <resources> 4 <resource> 5 <directory>src/main/java</directory> 6 <includes> 7 <include>**/*.properties</include> 8 <include>**/*.xml</include> 9 </includes> 10 <filtering>false</filtering> 11 </resource> 12 </resources> 13 </build>

浙公网安备 33010602011771号