一, 简单测试工程搭建
1,Mybatis 所需要的的 jar 包(包含数据库驱动包和相关的日志包),SpringMVC 和 Spring 的 jar 包
2, 然后构建一个基本的工程, 这里我们使用 mapper 代理的方式进行 Mybatis 的编写, 关于 mapper 代理请参考 Mybatis 简单入门中的 Mybatis 开发 dao 方法简介中讲到的 mapper 代理方式, 所以在项目中我们不建立 dao 包, 需要建立 mapper 包用来存放 mapper 接口和相应的 mapper 配置文件.
二, 配置 Mybatis 和 Spring 整合
1, 配置 Mybatis 的核心配置文件, 因为是和 Spring 整合, 所以数据库的配置交给 Spring 管理由 Spring 进行数据源的配置.
<?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>
- <!-- 批量别名定义: Mybatis 在定义别名的时候会自动扫描包中的 po 类, 自动的将别名定义为类名(首字母大写或者小写都可以)-->
- <package name="cn.test.ssm.mapper"></package>
- </typeAliases>
- </configuration>
2, 下来是 Spring 和 Mybatis 的整合, 可以参考前面的 Mybatis 和 Spring 整合篇中的 mapper 代理方式. 到这里我们就需要配置 Spring 整合 Mybatis 的配置文件了, 在 Spring 和 Mybatis 的整合文件 applicationContext-dao.xml 配置文件中我们需要配置数据源 (dataSource), 会话工厂(sqlSessionFactory) 和 Mapper 扫描器
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- 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.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx.xsd">
- <!-- 加载数据库信息的配置文件 -->
- <context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
- <!-- 配置数据源 -->
- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
- <property name="driverClass" value="${jdbc.driver}" />
- <property name="jdbcUrl" value="${jdbc.url}" />
- <property name="user" value="${jdbc.username}" />
- <property name="password" value="${jdbc.password}" />
- </bean>
- <!-- 配置 SqlSessionFactory-->
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <!-- 加载 Mybatis 的配置文件 -->
- <property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml"></property>
- <!-- 配置数据源 -->
- <property name="dataSource" ref="dataSource"></property>
- </bean>
- <!-- 配置 mapper 扫描器 -->
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="basePackage" value="cn.test.ssm.mapper"></property>
- <property name="sqlSessionTemplateBeanName" value="sqlSessionFactory"></property>
- </bean>
- </beans>
3, 接下来我们就开始编写一个简单测 mapper 测试配置文件, 只完成一个小功能(查询一个列表集合) , 在里面使用一些简单的动态 sql 进行判断避免异常
<?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 指定了命名空间 -->
- <mapper namespace="cn.test.ssm.mapper.ProductDemo">
- <!-- 实现一个简单的列表查询的功能(使用动态 sql 和 sql 片段便于扩展, 虽然这只是个小的 demo 并没有做其他的扩展, 但是可以养成一种习惯)-->
- <!--sql 片段 + 动态 sql-->
- <sql id="queryListCondition">
- <where>
- <if test="productExtend != null">
- <if test="productExtend.name != null and productExtend.name !=''">
- product.pname LIKE '%${productExtend.name}%'
- </if>
- </if>
- </where>
- </sql>
- <!-- 为了便于扩展, 使用 ProductExtent 类作为输出映射, 这样除了可以查询 Product 之外还可以扩展其他的字段 -->
- <select id="findProductListByName" parameterType="cn.test.ssm.po.ProductQueryVo" resultType="cn.test.ssm.po.ProductExtend">
- SELECT product.* FROM product
- <where>
- <include refid="queryListCondition"></include>
- </where>
- </select>
- </mapper>
4, 写完 mapper 配置文件之后就写一个接单的接口程序, 其中只包含一个方法就是查询列表信息.
- package cn.test.ssm.mapper;
- import cn.test.ssm.po.ProductExtend;
- import cn.test.ssm.po.ProductQueryVo;
- import java.util.List;
- public interface ProductDemo {
- public List<ProductExtend> findProductListByName(ProductQueryVo productQueryVo) throws Exception;
- }
三, 配置 Spring 和 Service 层整合
1, 一般情况下都是定义 service 接口和对应的实现类, 这里我们也定义一个简单的 ProductService 接口和其实现类作为 service 层的主要类
1Product Service 接口: 主要就是要调用 mapper 接口中定义的那一个查询列表的方法
- package cn.test.ssm.service;
- import cn.test.ssm.po.ProductExtend;
- import cn.test.ssm.po.ProductQueryVo;
- import java.util.List;
- public interface ProductService {
- public List<ProductExtend> findProductListByName(ProductQueryVo productQueryVo) throws Exception;
- }
2ProductServiceImpl 实现类, 实现上面接口中的方法, 由于要和 Mybatis 和 Spring 已经整合(采用 mapper 代理的方式), 并且在 applicationContext-dao 配置文件中配置了 mapper 扫描器, 所以我们可以使用注解的方式注入 Mapper 接口然后在 service 中调用接口中的方法
- package cn.test.ssm.service.impl;
- import cn.test.ssm.mapper.ProductDemo;
- import cn.test.ssm.po.ProductExtend;
- import cn.test.ssm.po.ProductQueryVo;
- import cn.test.ssm.service.ProductService;
- import org.springframework.beans.factory.annotation.Autowired;
- import java.util.List;
- public class ProductServiceImpl implements ProductService {
- @Autowired
- private ProductDemo productDemo; // 自动注入 mapper 接口, 然后在实现 service 的方法中调用 mapper 接口中的方法
- @Override
- public List<ProductExtend> findProductListByName(ProductQueryVo productQueryVo) throws Exception {
- return productDemo.findProductListByName(productQueryVo);
- }
- }
2, 上面写好了接口和实现类, 然后就是将 service 交给 Spring 进行管理, 配置 applicationContext-service.xml 对 service 进行整合. 对 service 整合主要包括: service 本身接口实现类的 bean 配置, 事务控制等
1管理 service 本身的接口实现类的 bean
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- 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.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx.xsd">
- <!-- 对 service 本身的接口实现类的 bean 配置 -->
- <bean id="productService" class="cn.test.ssm.service.impl.ProductServiceImpl">
- </bean>
- </beans>
2进行事务控制的配置
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- 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.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx.xsd">
- <!--
- 事务控制的配置
- 对数据库操作 Mybatis 的事务控制使用 spring 的 jdbc 事务管理控制类
- -->
- <!-- 事务管理器 -->
- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <!-- 添加对数据源的控制 -->
- <property name="dataSource" ref="dataSource"></property>
- </bean>
- <!-- 通知 -->
- <tx:advice id="txAdvice">
- <tx:attributes>
- <!-- 配置传播行为 -->
- <!-- 配置必须进行事务控制的方法 -->
- <tx:method name="save*" propagation="REQUIRED"/>
- <tx:method name="delete*" propagation="REQUIRED"></tx:method>
- <tx:method name="insert*" propagation="REQUIRED"></tx:method>
- <tx:method name="update*" propagation="REQUIRED"></tx:method>
- <!-- 配置支持事务的方法 -->
- <tx:method name="find*" propagation="SUPPORTS" read-only="true"></tx:method>
- </tx:attributes>
- </tx:advice>
- <!-- 配置 aop 去调用通知 -->
- <aop:config>
- <aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.test.ssm.service.impl.*.*(..))"></aop:advisor>
- </aop:config>
- </beans>
四, 配置整合 springmvc 和 spring
1, 首先配置 springmvc 的配置文件, 其中包括处理器映射器, 处理器适配器, 视图解析器的配置和对 controller 层包自动扫描的配置
- <?xml version="1.0" encoding="UTF-8"?>
- <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 的扫描 -->
- <context:component-scan base-package="cn.test.ssm.controller"></context:component-scan>
- <!-- 配置 mvc:annotation 代替基于注解方式的处理器映射器和适配器的配置 -->
- <mvc:annotation-driven></mvc:annotation-driven>
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>
- </beans>
2, 下来在 Web.xml 中配置 springmvc 的前端控制器, 里面主要包括 DispatcherServlet 的配置以及 springmvc 配置文件的路径配置.
- <?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_4_0.xsd"
- version="4.0">
- <!-- 配置前端控制器 -->
- <servlet>
- <servlet-name>SpringMvc</servlet-name>
- <servlet-class>
- org.springframework.Web.servlet.DispatcherServlet
- </servlet-class>
- <!--
- 配饰 SpringMVC 的配置文件(处理器映射器, 适配器等)
- 注明需要这样配置的原因: 自己配置 contextConfigLocation, 就不会自己默认加载 / WEB-INF / 下面的 dispatch-servlet.xml
- -->
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:spring/applicationContext-springmvc.xml</param-value>
- </init-param>
- </servlet>
- <servlet-mapping>
- <servlet-name>SpringMvc</servlet-name>
- <url-pattern>*.do</url-pattern>
- </servlet-mapping>
- </Web-App>
五, 在 controller 层写 handler 程序
这里实现的功能也比较简单, 由于只是为了测试整个整合流程的正确, 所以依旧是按照查询列表进行编写, 然后从 service 调用方法, 返回模型视图,
- package cn.test.ssm.controller;
- import cn.test.ssm.po.ProductExtend;
- import cn.test.ssm.service.ProductService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.Web.bind.annotation.RequestMapping;
- import org.springframework.Web.servlet.ModelAndView;
- import java.util.List;
- @Controller
- public class ProductController {
- @Autowired
- private ProductService productService;
- @RequestMapping("/queryList.do")
- public ModelAndView queryList() throws Exception{
- // 从 service 层调用方法
- List<ProductExtend> productExtendList = productService.findProductListByName(null);
- // 返回 ModelandView
- ModelAndView modelAndView = new ModelAndView();
- modelAndView.addObject(productExtendList);
- modelAndView.setViewName("/WEB-INF/items/itemsList.jsp");
- return modelAndView;
- }
- }
六, 配置 Spring 容器
到这里, 我们还需要配置 spring 容器的监听和相应配置文件 (applicationContext-dao.xml......) 的加载. 在配置文件中我们需要在 IDEA 中修改 class 文件的输出路径(本来默认是自动建立 out 文件, 然后将 class 文件输出进去), 参考这篇博客. 至此, 所有的配置都已经完成, 下面就开始测试
- <!-- 配置 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>
七, 使用简单的 jsp 视图进行测试
1, 数据库中的 Product 表信息:
- CREATE TABLE `product` (
- `pid` INT(11) NOT NULL AUTO_INCREMENT,
- `pname` VARCHAR(255) DEFAULT NULL,
- `shop_price` DOUBLE DEFAULT NULL,
- PRIMARY KEY (`pid`)
- ) ENGINE=INNODB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8
product 表创建的 sql
2, 然后在浏览器中输入 http://localhost:8080/TestSSM2/queryList.do 测试得到下面的结果信息
来源: https://www.cnblogs.com/fsmly/p/10393507.html