一、整合思路:
1. jar包:
mybatis包、spring包、mybatis和spring整合包、数据库驱动包、日志包;
2. Spring管理:
SpringMVC中编写的 Handler(即Controller)、Mybatis的 SqlSessionFactory、mapper;
3. 工程结构:
第一步:整合 dao、spring、mybatis;
第二步:整合 service;
第三部:整合 controller
4. 配置文件:
1. applicationContext-dao.xml —配置数据源、SqlSessionFactory、mapper
2. applicationContext-service.xml —配置 service接口
3. applicationContext-transaction.xml —配置事务管理
4. springmvc.xml —配置映射器、适配器、视图解析器
5. SqlMapConfig.xml(mybatis的配置文件) —别名、setting、(mapper)
6. web.xml —配置 DispatcherServlet前端控制器
5. 编写:
dao(即mapper)、service、controller、jsp:
商品列表开发:查询商品、根据条件查询
【 5.1 mapper 】: 一般情况下,针对查询 mapper需要自定义 mapper
(eg:下方 5.1 中mapper文件:selct * from items where name like‘’ and price)
【 5.2 service】:service可以调用spring容器中的dao(即mapper); 再在对应的 service.xml文件中进行配置;
(eg:下方 5.2 中定义 service文件,service接口中方法可以与 mapper接口中方法一致; service接口的实现类中有 mapper属性,由 spring容器生成(applicationContext-dao.xml中的 mapper扫描器生成)mapper对象,再使用@Autowired注解注入进来; spring容器中生成 service对象要在 applicationContext-service.xml中配置才能生成;)
【 5.3 controller】:类上使用 @Controller注解、类的方法上使用 @RequestMapping注解映射其 url、类中使用@Autowired注解注入 itemsService;
【 5.4 jsp】:jsp页面中显示出 controller类中 modelAndView中的 itemsList;
(项目中这些配置文件可一起也可放在对应分类的文件夹中:)
4.0. db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=mysql:jdbc://localhost:3306/test
jdbc.username=root
jdbc.password=12345
4.1. applicationContext-dao.xml
(配置配置数据源、SqlSessionFactory、mapper(即 mapper接口mapper的.java 文件))
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 1. 配置数据源 -->
<!-- 加载属性文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 数据库连接池 -->
<bean id="dataSource" class="com.mchange.v2.com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="maxActive" value="10"></property>
<property name="maxIdle" value="5"></property>
</bean>
<!-- 2. SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 配置数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 加载mybatis的配置文件 -->
<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"></property>
</bean>
<!-- 3. Mapper扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.asd.mapper"/>
</bean>
</beans>
4.2. applicationContext-service.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 商品管理的service -->
<bean id="itemsService" class="com.asd.service.ItemsServiceImpl"></bean>
</beans>
4.3. applicationContext-transaction.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 1.事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 2.事务通知 (即如何管理) -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<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="select" propagation="SUPPORTS" read-only="true"/>
<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 3.AOP,拦截什么方法(切入点表达式)+增强 -->
<aop:config>
<aop:pointcut id="pt" expression="execution(* com.asd.ssm.service.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
</aop:config>
</beans>
4.4. springmvc.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- (开启spring注解扫描)3.注解开发的Handler -->
<context:component-scan base-package="com.asd"></context:component-scan>
<!-- 1.注解映射器 -->
<bean name="" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingInfoHandlerMapping">
</bean>
<!-- 2.注解适配器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
<!-- 4.视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
4.5. SqlMapConfig.xml
<?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>
<typeAliase>
<package name="com.iotek.po"/>
</typeAliase>
</configuration>
4.6. web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd"
version="3.0">
<!-- 1.配置DiapatcherServlet前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<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>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<!-- 2.加载启动Spring框架 -->
<context-param> // 加载spring容器
<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>
<welcome-file-list>
</welcome-file-list>
</web-app>
【 5.1 开发mapper即 dao层:】
5.1.1 ItemsCustomMapper.xml(mapper映射文件)
(mpper的 namespace是 对应mapper接口的全限定名; < sql >片段中从 parameterType输入类型 itemsQueryVo中拿 itemsCustom,)
<?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="com.asd.mapper.ItemsCustomMapper">
<!-- 商品查询的sql片段,建议:以单表为单位 -->
<sql id="query_items_where">
<if test="itemsCustom!=null">
<if test="itemsCustom.name!=null and itemsCustom.name!=''">
and name like '%${itemsCustom.name}%'
</if>
<if test="itemsCustom.id!=null">
and id=#{itemsCustom.id}
</if>
</if>
</sql>
<!-- 根据条件查找 -->
<select id="findItemsList" parameterType="itemsQueryVo" resultType="itemsCustom">
select * from items
<where>
<include refid="query_items_where"/>
</where>
</select>
</mapper>
5.1.1’ ItemsCustomMapper.java(mapper接口)
public interface ItemsCustomMapper{
public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo)throws Exception;
}
5.1.2 ItemsCustom.java
// Items扩展类
public class ItemsCustom extends Items{
}
5.1.3 com.asd.po包中有:Items.java、ItemsQueryVo.java
ItemsQueryVo.java
// 商品的包装类
public class ItemsQueryVo{
private ItemsCustom itemsCustom;
set、get();
}
5.1.4 com.asd.mapper包中有:ItemsMapper.xml、ItemsMapper.java
【 5.2 开发 service:】
5.2.1 ItemsService.java
(接口,与 mapper接口中方法可保持一致;)
// 商品列表的service
public interface ItemsService{
public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo)throws Exception;
}
5.2.1’ ItemsServiceImpl.java
(商品列表的service的实现类; 再在 applicationContext-service.xml中进行配置。)
public class ItemsServiceImpl implements ItemsService{
@Autowired //注入mapper
private ItemsCustomMapper itemsCustomMapper;
public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo)throws Exception{
return itemsCustomMapper.findItemsList(itemsQueryVo);
}
}
【 5.3 开发 controller:】
5.3.1 ItemsController.java
(Controller上有 @Controller注解;类中的方法上使用 @RequestMapping注解映射其 url; 方法中要调用 service查询出商品,要使用到 itemsService,使用@Autowired注解注入 itemsService;)
@Controller
public class ItemsController{
@Autowired
private ItemsService itemsService;
@RequestMapping("/queryItems")
public ModelAndView queryItems() throws Exception{
//调用service查询出的商品,要用到itemsService
List<ItemsCustom> itemList=itemsService.findItemsList(null);
ModelAndView modelAndView=new ModelAndView();
modelAndView.addObject("items",itemsList);
modelAndView.setViewName("itemsList");
return modelAndView;
}
}
【 5.4 开发 jsp:】
5.4.1 items.jsp
(< c:foreach>显示要用到 jstl标签,所以文本头要添加一行:)
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<body>
<table width="100%" border="1">
<tr><td>商品名称</td><td>商品价格</td><td>商品详情</td></tr>
<c:foreach items="${items}" var="item">
<tr><td>${item.name}</td><td>${item.price}</td><td>${item.detail}</td></tr>
</c:foreach>
</table>
</body>