SpringMVC框架(1)之(1.2 入門程式—SpringMVC與Mybatis整合)
一、整合思路:
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
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】:
(eg:下方 5.2 中定義 service檔案,service介面中方法可以與 mapper介面中方法一致; service介面的實現類中有 mapper屬性,由 spring容器生成(applicationContext-dao.xml中的 mapper掃描器生成)mapper物件,再使用@Autowired註解注入進來; spring容器中生成 service物件要在 applicationContext-service.xml中配置才能生成;)
【 5.3 controller】:
【 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>