您的位置:首页 > 编程语言 > Java开发

【SSM-SpringMVC框架】SpringMVC和MyBatis的整合

2016-04-14 13:13 627 查看

1.整合思路:

以一个商品列表的查询为例。

springmvc+mybatis的系统架构:



第一步:整合Dao:

mybatis和spring整合,通过spring管理mapper接口。使用mapper的扫描器自动扫描mapper接口,在spring中进行注册。

第二步:整合services:

通过spring管理service接口,使用配置方式将service配置在spring配置文件中。

第三步:整合springmvc:

springmvc是spring的模块,不需要整合。

工程架构:



2.整合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>

<!-- 别名 -->
<typeAliases><package name="cn.edu.hpu.ssm.po"/></typeAliases>
<!-- 由于spring管理mbatis,所以这里不用扫描mapper -->
<!--  <mappers></mappers>-->
</configuration>


2.applicationContext-dao.xml:

配置数据源,sqlSessionFactory,mapper扫描器。

<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 ">

<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:db.properties"/>

<!-- 数据库连接池 -->
<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>
<!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的org.mybatis.spring.SqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据库连接池 -->
<property name="dataSource" ref="dataSource" />
<!-- 加载mybatis的全局配置文件 -->
<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
</bean>

<!-- 批量扫描mapper,在这里设置之后,sqlMapperConfig则不需要扫描mapper,但是引用时,必须用首字母小写 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.edu.hpu.ssm.mapper"></property>
<property name="sqlSessionFactoryBeanName"  value="sqlSessionFactory" />
</bean>
</beans>


3.逆向工程生成pojo类和mapper(单表的增删改查):



将生成的文件拷贝到工程中:

4.手动定义商品查询的Mapper:

ItemsMapperCustom.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="cn.edu.hpu.ssm.mapper.ItemsMapperCustom" >

<sql id="queryByname">
<if test="itemsCustom !=null">
<if test="itemsCustom.name != null and itemsCustom.name !='' ">
NAME LIKE '%${itemsCustom.name}%'
</if>
</if>
</sql>
<!-- 多表查询 -->
<select id="findItemsList" parameterType="cn.edu.hpu.ssm.po.ItemsQueryVo"
resultType="cn.edu.hpu.ssm.po.ItemsCustom">
SELECT * FROM items
<where>
<include refid="queryByname"></include>
</where>
</select>
</mapper>


ItemsMapperCustom.java:

public interface ItemsMapperCustom {

//查询相信信息
public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception;
}



3.整合service:

让spring管理service接口:

1.ItemsServices.java:

public interface ItemsServices {
//查询相信信息
public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception;
}

2.ItemsServicesImpl.java

public class ItemsServicesImpl implements ItemsServices {

@Autowired
private ItemsMapperCustom itemsMapperCustom;

@Autowired
private ItemsMapper itemsMapper;
/**
* 查询多条记录(模糊查询)
*/
@Override
public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo)
throws Exception {

return itemsMapperCustom.findItemsList(itemsQueryVo);
}
}


3.在spring容器中配置service:

创建applicationContext-services.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-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 ">

<!-- 商品管理的services -->
<bean id="itemsServices" class="cn.edu.hpu.ssm.services.impl.ItemsServicesImpl" />
</beans>


4.事务控制:

创建applicationContext-transaction.xml ,在applicationContext-transaction.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-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 ">

<!-- 在applicationContext-transaction.xml中使用spring声明式事务控制方法。 -->

<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 数据源   来自于applicationContext-dao.xml中的dataSource-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 传播行为 -->
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="delete*" 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:attributes>
</tx:advice>

<!-- 切面 aop -->
<aop:config>
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* cn.edu.hpu.ssm.services.impl.*.*(..))"/>
</aop:config>

</beans>



4.整合springmvc:

1.springmvc.xml:

创建springmvc.xml,配置Handler,处理器映射器,处理器适配器,视图解析器。

<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 ">

<!-- controller配置 (Handler处理器配置)-->

<context:component-scan base-package="cn.edu.hpu.ssm.controller"></context:component-scan>

<mvc:annotation-driven conversion-service="conversionService"
validator="validator"> <!-- 配置校验 -->
</mvc:annotation-driven>

<!-- 视图解析器 解析jsp视图的解析器,默认使用jstl-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>


2.配置前端控制器:

<!-- springmvc的前端控制器 -->
<servlet>

<servlet-name>springmvc</servlet-name>
<!-- 手动加载DispatcherServlet的源代码 -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- contextConfigLocation配置springmvc的加载文件(配置处理器映射器,适配器)
如果不配置contextConfigLocation,默认加载的是/WEB-INF/servlet名称-servlet.xml(springmvc-servlet.xml) -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- 1 *.action,访问.action结尾由DispatcherServlet进行解析
2	/ ,所有访问的地址由DispatcherServlet进行解析,对于静态的文件需要配置不让DispatcherServlet解析
此种方式可以实现RESULTfull风格的url
3 /* ,此证方式不正确 -->
<url-pattern>*.action</url-pattern>
</servlet-mapping>


5.编写Controller:

@Controller
public class ItemsController {

@Autowired
private ItemsServices itemsServices;

/**
* 根据查询条件,查询Items。
* @param itemsQueryVo
* @return
* @throws Exception
*/
@RequestMapping("/queryItems.action")
public ModelAndView queryItems() throws Exception{

//商品列表
List<ItemsCustom> itemsList =itemsServices.findItemsList(null);

//创建modelAndView准备填充数据、设置视图
ModelAndView modelAndView = new ModelAndView();

//填充数据,,相当于request.setAttribut(); 在jsp页面中通过itemsList取数据
modelAndView.addObject("itemsList", itemsList);
//视图
modelAndView.setViewName("/itemsList.jsp");

return modelAndView;
}


6.编写jsp:

<table width="100%" border=1>
<tr>
<td>	商品名称:<input type="text"  name="itemsCustom.name" />
<input type="button" value="查询" onclick="queryItems()" />
<input type="button" value="批量删除" onclick="deleteItems()"/>
</td>
</tr>
</table>
商品列表:
<table width="100%" border=1>
<tr>
<td>选择</td>
<td>商品名称</td>
<td>商品价格</td>
<td>生产日期</td>
<td>商品描述</td>
<td>操作</td>
</tr>
<c:forEach items="${itemsList }" var="item">
<tr>
<td><input type="checkbox"  name="items_id" value="${item.id }"/></td>
<td>${item.name }</td>
<td>${item.price }</td>
<td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
<td>${item.detail }</td>
<td><a href="${pageContext.request.contextPath }/editItem.action?id=${item.id}">修改</a></td>
</tr>
</c:forEach>

</table>


7.加载spring容器:

将mapper,service,controller加载到spring容器:

<!-- 向spring容器中加载文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/spring/applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: