【SSM的系統(tǒng)架構(gòu)】
目前創(chuàng)新互聯(lián)建站已為1000+的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬空間、網(wǎng)站托管、服務(wù)器托管、企業(yè)網(wǎng)站設(shè)計(jì)、南康網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。
【整合概述】
第一步:
MyBatis和Spring整合,通過Spring管理mapper接口。
使用mapper的掃描器自動(dòng)掃描mapper接口在Spring中進(jìn)行注冊(cè)。
第二步:
通過Spring管理Service接口。
使用配置方式將Service接口配置在Spring配置文件中。
實(shí)現(xiàn)事務(wù)控制。
第三步:
由于SpringMVC是Spring的模塊,無需整合這兩個(gè)。
【工程截圖】
【數(shù)據(jù)庫(kù)的items】
[ 表結(jié)構(gòu) ]
[ 表內(nèi)數(shù)據(jù) ]
【1.整合dao】
將Mybatis和Spring進(jìn)行整合
【1.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配置 ,根據(jù)需要再添加--> <!-- 配置別名 --> <typeAliases> <!-- 批量掃描別名 --> <package name="cn.Higgin.ssm.po"/> </typeAliases> <!-- 配置mapper 由于使用spring和mybatis的整合包進(jìn)行整合,這了無需配置 但必須遵循:mapper.xml和mapper.xml文件同名且在同一目錄--> <!--<mappers></mappers> --> </configuration>
【1.2 db.properties】數(shù)據(jù)庫(kù)配置文件
jdbc.driver=com.MySQL.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/mybatis jdbc.username=root jdbc.password=
【1.3 applicationContext-dao.xml】
需要配置:數(shù)據(jù)源、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 "> <!-- 加載db.properties文件中的內(nèi)容,db.properties文件中key命名要有一定的特殊規(guī)則 --> <context:property-placeholder location="classpath:db.properties"/> <!-- 配置數(shù)據(jù)源,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="30"/> <property name="maxIdle" value="5"/> </bean> <!-- sqlSessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 數(shù)據(jù)庫(kù)連接池 --> <property name="dataSource" ref="dataSource" /> <!-- 加載mybatis的全局配置文件 --> <property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml" /> </bean> <!-- mapper掃描器--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 掃描包路徑,如果需要掃描多個(gè)包,中間使用半角逗號(hào)隔開 --> <property name="basePackage" value="cn.Higgin.ssm.mapper"></property> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> </beans>
【1.4 逆向工程生成PO類以及mapper】(對(duì)應(yīng)表的增刪改查)
【1.5 手動(dòng)定義商品查詢mapper】
針對(duì)綜合查詢mapper,一般情況下會(huì)有關(guān)聯(lián)查詢,建議 自定義 mapper。
【1.5.1 ItemsMapperCustom.java】
package cn.Higgin.ssm.po; /** * 商品信息的擴(kuò)展 */ public class ItemsCustom extends Items{ //添加商品信息擴(kuò)展的屬性 }
【1.5.2 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.Higgin.ssm.mapper.ItemsMapperCustom" > <!-- 定義商品查詢的sql片段,就是商品查詢條件 --> <sql id="query_items_where"> <!-- 使用動(dòng)態(tài)sql,通過if判斷,滿足條件進(jìn)行sql拼接 --> <!-- 商品查詢條件通過ItemsQueryVo包裝對(duì)象 中itemsCustom屬性傳遞 --> <if test="itemsCustom!=null"> <if test="itemsCustom.name!=null and itemsCustom.name!=''"> items.name LIKE '%${itemsCustom.name}%'</if> </if> </sql> <!-- 商品列表查詢 --> <!-- parameterType:傳入包裝對(duì)象(包裝了查詢條件) resultType:建議使用擴(kuò)展對(duì)象 --> <select id="findItemsList" parameterType="cn.Higgin.ssm.po.ItemsQueryVo" resultType="cn.Higgin.ssm.po.ItemsCustom"> SELECT items.* FROM items<where> <include refid="query_items_where"></include> </where> </select> </mapper>
【2.整合service】
讓spring來管理service接口。
【2.1 ItemsService.java】定義service接口
package cn.Higgin.ssm.service; import java.util.List; import cn.Higgin.ssm.po.ItemsCustom; import cn.Higgin.ssm.po.ItemsQueryVo; public interface ItemsService{ /** * 通過ItemsMapperCustomer查詢數(shù)據(jù)庫(kù) */ public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo)throws Exception; }
【2.2 ItemsServiceImpl.java】service實(shí)現(xiàn)接口
package cn.Higgin.ssm.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import cn.Higgin.ssm.mapper.ItemsMapperCustom; import cn.Higgin.ssm.po.ItemsCustom; import cn.Higgin.ssm.po.ItemsQueryVo; import cn.Higgin.ssm.service.ItemsService; public class ItemsServiceImpl implements ItemsService{ @Autowiredprivate ItemsMapperCustom itemsMapperCustom; //通過ItemsMapperCustom查詢數(shù)據(jù)庫(kù)public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception { return itemsMapperCustom.findItemsList(itemsQueryVo); } }
【2.3 applicationContext-service.xml】
在spring容器中配置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="itemService" class="cn.Higgin.ssm.service.impl.ItemsServiceImpl" /></beans>
【2.4 applicationContext-transaction.xml】事務(wù)控制
在applicationContext-transaction.xml中使用spring聲明 事務(wù)控制方法。
<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 "> <!-- 事務(wù)管理器 對(duì)mybatis操作數(shù)據(jù)庫(kù)進(jìn)行事務(wù)控制,spring使用jdbc的事務(wù)控制類 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 數(shù)據(jù)源 dataSource在applicationContext-dao.xml中已經(jīng)配置 --> <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(* cn.Higgin.ssm.service.impl.*.*(..))"/> </aop:config> </beans>
【3.整合springmvc】
【3.1 springmvc.xml】
在springmvc中配置 處理映射器、適配器、視圖解析器
<?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、service、... 這里讓掃描controller,指定controller的包 --> <context:component-scan base-package="cn.Higgin.ssm.controller"></context:component-scan> <!-- 使用 mvc:annotation-driven代替注解映射器和注解適配器配置 mvc:annotation-driven默認(rèn)加載很多的參數(shù)綁定方法, 比如json轉(zhuǎn)換解析器就默認(rèn)加載了,如果使用mvc:annotation-driven不用配置上邊的RequestMappingHandlerMapping和RequestMappingHandlerAdapter 實(shí)際開發(fā)時(shí)使用mvc:annotation-driven --> <mvc:annotation-driven ></mvc:annotation-driven> <!-- 視圖解析器 解析jsp解析,默認(rèn)使用jstl標(biāo)簽,classpath下的得有jstl的包 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 配置jsp路徑的前綴 --> <property name="prefix" value="/WEB-INF/jsp/"/> <!-- 配置jsp路徑的后綴 --> <property name="suffix" value=".jsp"/> </bean> </beans>
【3.2 在web.xml中】 在web.xml中配置前端控制器的代碼
<!-- springMVC前端控制器 --> <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> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping>
【3.3 ItemsController.java】
編寫Controller(即Handler)
package cn.Higgin.ssm.controller; import java.util.List; 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 cn.Higgin.ssm.po.ItemsCustom; import cn.Higgin.ssm.service.ItemsService; @Controllerpublic class ItemsController{ @Autowiredprivate ItemsService itemsService; //商品查詢 @RequestMapping("/queryItems")public ModelAndView queryItems() throws Exception{ //調(diào)用Service查找數(shù)據(jù)庫(kù),查詢商品列表,這里使用靜態(tài)數(shù)據(jù)模擬 List<ItemsCustom> itemsList=itemsService.findItemsList(null); //返回ModelAndView ModelAndView modelAndView=new ModelAndView(); //相當(dāng)于request的setAttribute,在jsp頁面中通過itemList來獲取 modelAndView.addObject("itemsList",itemsList); //指定視圖 modelAndView.setViewName("items/itemsList"); System.out.println("注解方式:ItemsComtroller......"); return modelAndView; } }
【4.前端jsp頁面 itemsList.jsp】
位置:WEB-INF/jsp/items/itemsList.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>商品價(jià)格</td> <td>生產(chǎn)日期</td> <td>商品描述</td> <td>操作</td> </tr> <c:forEach items="${itemsList }" var="item"> <tr> <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 }/item/editItem.action?id=${item.id}">修改</a></td> </tr> </c:forEach> </table> </form> </body> </html>
【5. 加載spring容器】
將mapper、service、controller都加載到spring容器中。
建議使用通配符加載上邊的配置文件。
在web.xml中,添加Spring容器監(jiān)聽器,加載spring容器。
【web.xml完整】
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>Spring_SpringMVC_MyBatis</display-name> <!-- 加載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><!-- springMVC前端控制器 --> <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> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
源碼來源:http://minglisoft.cn/technology
朋友需要請(qǐng)加QQ:2042849237
當(dāng)前文章:SSM框架整合(Spring+SpringMVC+MyBatis)
當(dāng)前路徑:http://bm7419.com/article30/gipsso.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、網(wǎng)站制作、網(wǎng)站營(yíng)銷、移動(dòng)網(wǎng)站建設(shè)、建站公司、網(wǎng)站設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)