SSM整合之环境配置和测试开发
2017-06-26 21:46
393 查看
需求
使用springmvc和mybatis完成商品列表的查询整合思路
springmvc+mybatis(ssm)的系统架构图解:第一步:整合DAO层(持久层)
mybatis和spring整合,通过spring管理mapper接口。使用mapper的扫描器自动扫描mapper接口,在spring容器中自动注入
第二步:整合Service层(业务层)
通过spring管理service接口(spring特性:依赖注入)使用配置的方式,将service接口配置在spring的配置文件中。(两种方式,注解与配置)
实现事务的控制
第三步:整合SpringMVC(表现层)
由于springmvc是spring的一个模块,不需要整合。环境准备
在开始之前要确定数据库版本、jdk版本、IDE版本、tomcat版本等数据库:MySQL 5.1.38
jdk (jvm): 1.8.0_60
IDE : intelliJ idea 2017 1.4
tomcat : 8.5.11
spring : 4.3.9
mybatis:3.4.4
所需要的jar包(包含mybatis+spring+依赖扩展包):
数据库驱动包
mybatis3.4.4的jar包
mybatis和spring的整合包1.3.0
log4j包
数据库连接池包(c3p0 or dbcp)
spring4.3.9的所有jar包
jstl包
junit单元测试包
其他扩展依赖包
新建工程,上述所有jar包全部导入:
新建config的source文件夹,加入:
1、log4j.properties
# Global logging configuration 建议开发环境中使用debug模式 log4j.rootLogger=DEBUG, stdout # Console output... log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
2、db.properties(可选)
隔离数据库连接配置文件,优化配置
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/test jdbc.username=root jdbc.password=root
3、包目录的创建
4、工程结构
整合DAO(持久层)
mybatis和spring进行整合,需要的配置文件1、sqlMapConfig.xml
mybatis自己的配置文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 全局setting配置,根据需要再加 --> <!--<settings>--> <!--<setting name="" value=""/>--> <!--</settings>--> <!-- 加载映射文件 与spring整合后这里不需要配置了 必须遵循mapper.xml和mapper.java文件同名且在一个目录的规范 --> <!--<mappers>--> <!-- 批量加载mapper 指定mapper接口类的包名 mybatis自动扫描该包目录下所有的mapper接口类 需要遵循一些规范:需要将mapper接口类名和mapper.xml的文件名称保持一致,且在一个目录 使用的前提:使用mapper代理的方法开发DAO 和spring整合后,在spring使用了MapperScannerConfigurer后,可以去掉该配置 --> <!--<package name="alex.ssm.mapper" />--> <!--</mappers>--> <!--别名定义--> <typeAliases> <!-- 针对批量别名的定义 指定报名:mybatis会自动的扫描包中的pojo类,别名就是类名,首字母大写或小写都可以 --> <package name="com.alex.ssm.po"/> </typeAliases> </configuration>
2、applicationContext-dao.xml
配置SQLSessionFactory,数据源,mapper扫描器,
一定要注意此处,不要把加载mybatis配置文件的路径写错了,在前面需要加上classpath!!要不会在启动时报错!
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!-- 加载db.properties 数据库连接配置文件 key命名的要有一定的特殊规则 --> <context:property-placeholder location="classpath:db.properties" /> <!-- 数据源,这里使用DBCP --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="maxActive" value="10" /> <property name="maxIdle" value="5" /> </bean> <!-- 配置SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 加载mybatis的配置文件 --> <property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml" /> <!-- 数据源配置 --> <property name="dataSource" ref="dataSource" /> </bean> <!-- 配置mapper的扫描器 MapperScannerConfigurer mapper 的批量扫描 , 从mapper的包中扫描出mapper的接口,自动创建代理对象,并在spring 的容器中注入 需要遵循一些规范:需要将mapper.java名和mapper.xml的文件名称保持一致,且在一个目录 自动扫描出来的mapper的bean的id为mapper类名(首字母小写) --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 指定扫描的包名,需要扫描多个包,使用半角逗号隔开 --> <property name="basePackage" value="com.alex.ssm.mapper" /> <!-- 获取sqlSessionFactory--> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean> <!-- 原始DAO接口 --> <!--<bean id="userDao" class="alex.ssm.dao.UserDaoImpl">--> <!--<property name="sqlSessionFactory" ref="sqlSessionFactory" />--> <!--</bean>--> <!-- mapper的配置 MapperFactoryBean:根据mapper接口生成代理对象 --> <!--<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">--> <!--<!– mapperInterface 指定mapper接口类 –>--> <!--<property name="mapperInterface" value="alex.ssm.mapper.UserMapper"/>--> <!--<property name="sqlSessionFactory" ref="sqlSessionFactory" />--> <!--</bean>--> </beans>
3、使用mybatis的逆向工程生成与数据库对应的po类和mapper(单表增删改查操作)
使用逆向工程教程请看另一篇博文:逆向工程
修改部分代码后,生成后的目录:
请注意:在此处需要使用mybatis整合spring的jar包版本为1.3.0 ,低于此版本会报一个名为java.lang.AbstractMethodError: org.mybatis.spring.transaction.SpringManagedTransaction.getTimeout()L的错误
参考:参考文献
4、手动定义商品查询的mapper
针对综合查询的mapper,一般情况会有关联查询,建议自定义mapper1.ItemsMapperCustom.xml
sql语句:
SELECT * FROM items WHERE NAME LIKE '% %'statement和sql片段的配置:
<!-- 商品查询的SQL片段 ,就是商品的查询条件--> <sql id="query_items_where"> <!-- 使用动态sql,通过if判断,满足条件进行sql拼接 --> <!-- 商品的查询条件,要通过itemsQueryVo包装对象中的itemsCustom属性传递 --> <if test="itemsCustom != null"> <if test="itemsCustom.name!= null and itemsCustom.name != '' "> items.name LIKE '%${itemsCustom.name}%' </if> </if> </sql> <!-- 商品列表查询 --> <!-- parameterType传入包装对象 resultType建议使用扩展对象 --> <select id="findItemsList" parameterType="com.alex.ssm.po.ItemsQueryVo" resultType="com.alex.ssm.po.ItemsCustom" > SELECT * FROM items <where> <include refid="query_items_where"/> </where> </select>
2.ItemsMapperCustom.java
package com.alex.ssm.mapper; import com.alex.ssm.po.ItemsCustom; import com.alex.ssm.po.ItemsQueryVo; public interface ItemsMapperCustom { //商品查询列表 public ItemsCustom findItemsList(ItemsQueryVo itemsQueryVo) throws Exception; }
整合service(业务层)
让spring来管理service接口1、定义service接口
/** * Created by Alex on 2017/6/27. * 商品管理的service */ public interface itemsService { //商品查询列表 public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception; }
因为我们的mapper遵循了开发规范,可以被spring的mapper扫描器自动扫描,所以这里我们不需要再在spring中注日,实现类:
/** * Created by Alex on 2017/6/27. * 商品管理接口实现类 */ public class itemsServiceImpl implements itemsService { @Autowired private ItemsMapperCustom itemsMapperCustom; @Override public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception { return itemsMapperCustom.findItemsList(itemsQueryVo); } }
2、在spring容器中配置service
创建appilicationContext-service.xml 文件中配置service:<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!-- 定义商品管理的service --> <bean id="itemsService" class="com.alex.ssm.service.impl.itemsServiceImpl"/> </beans>
3、创建事物控制(applicationContext-transaction)
在applicationContext-transaction.xml中,使用spring的声明式事物控制方法<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!-- 事物管理器 对mybatis操作数据库的控制,spring使用jdbc的事物控制类 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 数据源 在applicationContext-dao.xml 中的配置 --> <property name="dataSource" ref="dataSource"/> </bean> <!-- 通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 传播行为 --> <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="insert*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <!-- 查行为,为只读,不能修改 --> <tx:method name="find*" propagation="SUPPORTS" read-only="true" /> <tx:method name="get*" propagation="SUPPORTS" read-only="true" /> <tx:method name="select*" propagation="SUPPORTS" read-only="true" /> </tx:attributes> </tx:advice> <!-- AOP 设置切入点 --> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.alex.ssm.service.impl.*.*(..))" /> </aop:config> <!-- 导入applicationContext-dao.xml --> <import resource="applicationContext-dao.xml"/> </beans>
整合springmvc(表现层)
创建springmvc的xml文件,配置处理器映射器、适配器,视图解析器1、springmvc.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd "> <!-- Handler组件扫描 可以扫描Controller、service... 这里可以扫描Controller,指定Controller包 --> <context:component-scan base-package="com.alex.ssm.controller"></context:component-scan> <!-- 处理器映射器和适配器的扫描器 使用mvc:annotation-driven 代替上面两个注解映射器和适配器的配置 默认加载了很多参数绑定方法,比如说json转换的解析器,就默认加载了RequestMappingHandlerMapping和RequestMappingHandlerAdapter了 如果使用了这个,就不用配置上面的 实际开发使用mvc:annotation-driven --> <mvc:annotation-driven></mvc:annotation-driven> <!-- 视图解析器 解析jsp视图,默认使用jstl,classpath要有jstl的包 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 配置视图解析器的jsp路径的前缀和后缀 在handler内可精简代码 prefix:前缀 suffix:后缀 --> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp" /> </bean> </beans>
2、配置前端控制器
可以参考之前配置的文件<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <welcome-file-list> <welcome-file>/WEB-INF/jsp/items/itemsList.jsp</welcome-file> </welcome-file-list> <!-- SrpingMVC前端控制器 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- contextConfigLocation配置springmvc加载的配置文件(需要配置处理器映射器、适配器等) 如果不配置contextConfigLocation , 摸人家在的是/WEB-INFO/servlet名称-servlet。xml(springmvc-servlet.xml) --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!-- 第一种配置方式: *.action 访问以 .action结尾的 由DispatcherServlet进行解析 第二种配置方式:/ , 所有访问的地址 都由DispatcherServlet进行解析,若有图片或者其他的静态文件的解析,需要配置不让DispatcherServlet进行解析 使用这种方式可以实现RESTful风格的url 第三种配置模式: /* , 这样配置不对,使用该配置,最终要转发到一个JSP页面时,仍然会由dispaerServlet解析JSP,不能根据jsp页面找到handler,会报错的 --> <url-pattern>*.action</url-pattern> </servlet-mapping> </web-app>
3、编写Controller(Handler)
/** * Created by Alex on 2017/6/27. * 商品的controller */ @Controller public class itemsController { @Autowired private ItemsService itemsService; //商品查询 @RequestMapping("/queryItems") public ModelAndView queryItems() throws Exception{ //调用service查找数据库,查询商品列表 List<ItemsCustom> itemsList = itemsService.findItemsList(null); //返回ModelAndView ModelAndView modelAndView = new ModelAndView(); //相当于request的setAttribute,在jsp页面中通过itemsList取数据 modelAndView.addObject("itemsList",itemsList); //指定视图 modelAndView.setViewName("items/itemsList"); return modelAndView; } //商品修改 }
4、编写jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>查询商品列表</title> </head> <body> <form action="${pageContext.request.contextPath }/item/queryItem.action" method="post"> 查询条件: <table width="100%" border=1> <tr> <td><input type="submit" value="查询"/></td> </tr> </table> 商品列表: <table width="100%" border=1> <tr> <td>商品名称</td> <td>商品价格</td> <td>商品描述</td> <td>操作</td> </tr> <c:forEach items="${itemsList }" var="item"> <tr> <td>${item.name }</td> <td>${item.price }</td> <td>${item.detail }</td> <td><a href="${pageContext.request.contextPath }/item/editItem.action?id=${item.id}">修改</a></td> </tr> </c:forEach> </table> </form> </body> </html>
加载spring容器
将mapper、service、controller加载到spring 的容器中建议使用通配符的方法进行加载
在web.xml中添加spring容器的监听器,加载spring的容器
<!-- 加载spring容器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/applicationContext-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
测试
访问地址:http://localhost:8080/queryItems.action出现下页面即成功:
相关文章推荐
- win2k环境下基于JBOSS的J2EE开发实践-----之二、数据库连接池的配置与测试
- J2ME开发环境配置与测试:Eclips3.2+EclipseMe1.7+WTK2.2
- hadoop+hive+hbase的整合开发(配置加测试)
- PHP5.3.6和Apache2.2整合开发环境配置
- Cocos2d-X 游戏开发之 android 移植环境 cygwin 配置、测试安装正确 (十七)
- win2k环境下基于JBOSS的J2EE开发实践----之一初始基础环境的配置与测试
- Flex2与Struts整合配置-续搭建免费的flex开发环境flashdevelop
- SSH全注解整合开发环境安装配置
- SharePoint Server 2013开发之旅(四):配置工作流开发和测试环境
- 适应各种开发,测试,线上,线下环境的Spring配置方式
- Flex2与Struts整合配置 -续搭建免费的flex开发环境flashdevelop
- 自己泣血总结:Myeclipse8.5-Android开发环境配置(经测试成功)
- PHP 开发环境配置(测试开发环境)
- eclipse里配置solr开发测试环境
- DirectShow开发环境配置及测试例程(VC6)
- PHP 开发环境配置(测试开发环境)
- GO语言初探(一) 开发环境配置及测试 .
- Eclipse配合Resin开发web,附加mysql数据库连接池配置设置整合与测试
- Xcode5 + phoneGap2.9搭建ios开发环境-配置-测试-归档上传/phoneG...
- Maven环境下测试库和开发库的配置策略以及支持工具