帶你逐步深入瞭解SSM框架——淘淘商城專案之框架整合,後臺系統搭建
1. 今日計劃
1、 SSM框架整合
2、 mybatis逆向工程
3、 商品列表
4、 商品列表分頁處理
2. SSM框架整合
2.1. 後臺系統所用的技術
框架:Spring + SpringMVC +Mybatis
前端:EasyUI
資料庫:mysql
2.2. 建立資料庫
1、安裝mysql資料庫
2、在mysql中建立一個taotao資料庫
3、匯入資料庫指令碼。
2.3. Mybatis逆向工程
執行逆向工程
使用官方網站的mapper自動生成工具mybatis-generator-core-1.3.2來生成po類和mapper對映檔案。
2.4. 整合思路
1、Dao層:
mybatis整合spring,通過spring管理SqlSessionFactory、mapper代理物件。需要mybatis和spring的整合包。
整合內容 |
對應工程 |
Pojo |
Taotao-mangaer-pojo |
Mapper對映檔案 |
Taotao-mangaer-mapper |
Mapper介面 |
Taotao-mangaer-mapper |
sqlmapConfig.xml |
Taotao-manager-web |
applicationContext-dao.xml |
Taotao-manager-web |
2、Service層:
所有的實現類都放到spring容器中管理。由spring建立資料庫連線池,並有spring管理實務。
整合內容 |
對應工程 |
Service介面及實現類 |
Taotao-mangaer-service |
applicationContext-service.xml |
Taotao-manager-web |
applicationContext-trans.xml |
Taotao-manager-web |
3、表現層:
Springmvc整合spring框架,由springmvc管理controller。
整合內容 |
對應工程 |
springmvc.xml |
Taotao-manager-web |
Controller |
Taotao-manager-web |
2.5. Dao整合
2.5.1. 建立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> </configuration> |
2.5.2. Spring整合mybatis
建立applicationContext-dao.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 資料庫連線池 --> <!-- 載入配置檔案 --> <context:property-placeholder location="classpath:properties/*.properties" /> <!-- 資料庫連線池 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="driverClassName" value="${jdbc.driver}" /> <property name="maxActive" value="10" /> <property name="minIdle" value="5" /> </bean> <!-- 讓spring管理sqlsessionfactory 使用mybatis和spring整合包中的 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 資料庫連線池 --> <property name="dataSource" ref="dataSource" /> <!-- 載入mybatis的全域性配置檔案 --> <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" /> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.taotao.mapper" /> </bean> </beans> |
db.properties
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/taotao?characterEncoding=utf-8 jdbc.username=root jdbc.password=root |
備註:
Druid是目前最好的資料庫連線池,在功能、效能、擴充套件性方面,都超過其他資料庫連線池,包括DBCP、C3P0、BoneCP、Proxool、JBoss DataSource。
Druid已經在阿里巴巴部署了超過600個應用,經過多年多生產環境大規模部署的嚴苛考驗。
2.6. Service整合
2.6.1. 管理Service實現類
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <context:component-scan base-package="com.taotao.service"/> </beans> |
2.6.2. 事務管理
建立applicationContext-trans.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 事務管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 資料來源 --> <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="add*" propagation="REQUIRED" /> <tx:method name="create*" 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> <!-- 切面 --> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.taotao.service.*.*(..))" /> </aop:config> </beans> |
2.7. 表現層整合
2.7.1. Springmvc.xml
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.taotao.controller" /> <mvc:annotation-driven /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans> |
2.7.2. 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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>taotao-manager-web</display-name> <welcome-file-list> <welcome-file>login.html</welcome-file> </welcome-file-list> <!-- 載入spring容器 --> <context-param> <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> <!-- 解決post亂碼 --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> <!-- <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> --> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- springmvc的前端控制器 --> <servlet> <servlet-name>taotao-manager</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- contextConfigLocation不是必須的, 如果不配置contextConfigLocation, springmvc的配置檔案預設在:WEB-INF/servlet的name+"-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>taotao-manager</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app> |
2.7.3. 整合靜態頁面
靜態頁面位置:02.第二天(三大框架整合,後臺系統搭建)\01.參考資料\後臺管理系統靜態頁面
使用方法:
把靜態頁面新增到taotao-manager-web工程中的WEB-INF下:
由於在web.xml中定義的url攔截形式為“/”表示攔截所有的url請求,包括靜態資源例如css、js等。所以需要在springmvc.xml中新增資源對映標籤:
<mvc:resources location="/WEB-INF/js/" mapping="/js/**"/> <mvc:resources location="/WEB-INF/css/" mapping="/css/**"/> |
2.8. 修改taotao-manager-mapper的pom檔案
在pom檔案中新增如下內容:
<!-- 如果不新增此節點mybatis的mapper.xml檔案都會被漏掉。 --> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build> |
2.9. 整合測試
根據商品id查詢商品資訊。
3. 商品列表查詢
3.1. 商品列表頁面
對應的jsp為:
item-list.jsp
請求的url:
/item/list
請求的引數:
page=1&rows=30
響應的json資料格式:
Easyui中datagrid控制元件要求的資料格式為:
{total:”2”,rows:[{“id”:”1”,”name”,”張三”},{“id”:”2”,”name”,”李四”}]}
3.2. 響應的json資料格式EasyUIResult
public class EasyUIResult { private Integer total; private List<?> rows; public EasyUIResult(Integer total, List<?> rows) { this.total = total; this.rows = rows; } public EasyUIResult(Long total, List<?> rows) { this.total = total.intValue(); this.rows = rows; } public Integer getTotal() { return total; } public void setTotal(Integer total) { this.total = total; } public List<?> getRows() { return rows; } public void setRows(List<?> rows) { this.rows = rows; } } |
3.3. 分頁處理
3.3.1. Mybatis分頁外掛 - PageHelper說明
如果你也在用Mybatis,建議嘗試該分頁外掛,這個一定是最方便使用的分頁外掛。
該外掛目前支援Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六種資料庫分頁。
3.3.2. 使用方法
第一步:在Mybatis配置xml中配置攔截器外掛:
<plugins>
<!-- com.github.pagehelper為PageHelper類所在包名 -->
<plugininterceptor="com.github.pagehelper.PageHelper">
<!-- 設定資料庫型別 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六種資料庫-->
<propertyname="dialect"value="mysql"/>
</plugin>
</plugins>
第二步:在程式碼中使用
1、設定分頁資訊:
//獲取第1頁,10條內容,預設查詢總數count
PageHelper.startPage(1, 10);
//緊跟著的第一個select方法會被分頁
List<Country> list = countryMapper.selectIf(1);
2、取分頁資訊
//分頁後,實際返回的結果list型別是Page<E>,如果想取出分頁資訊,需要強制轉換為Page<E>,
Page<Country> listCountry = (Page<Country>)list;
listCountry.getTotal();
3、取分頁資訊的第二種方法
//獲取第1頁,10條內容,預設查詢總數count
PageHelper.startPage(1, 10);
List<Country> list = countryMapper.selectAll();
//用PageInfo對結果進行包裝
PageInfo page=new PageInfo(list);
//測試PageInfo全部屬性
//PageInfo包含了非常全面的分頁屬性
assertEquals(1, page.getPageNum());
assertEquals(10, page.getPageSize());
assertEquals(1, page.getStartRow());
assertEquals(10, page.getEndRow());
assertEquals(183, page.getTotal());
assertEquals(19, page.getPages());
assertEquals(1, page.getFirstPage());
assertEquals(8, page.getLastPage());
assertEquals(true, page.isFirstPage());
assertEquals(false, page.isLastPage());
assertEquals(false, page.isHasPreviousPage());
assertEquals(true, page.isHasNextPage());
3.4. Mapper
使用逆向工程生成的mapper檔案。
3.5. Service
@Service public class ItemServiceImpl implements ItemService { @Autowired private TbItemMapper itemMapper; @Override public EasyUIResult getItemList(Integer page, Integer rows) throws Exception { TbItemExample example = new TbItemExample(); //設定分頁 PageHelper.startPage(page, rows); List<TbItem> list = itemMapper.selectByExample(example); //取分頁資訊 PageInfo<TbItem> pageInfo = new PageInfo<>(list); long total = pageInfo.getTotal(); EasyUIResult result = new EasyUIResult(total, list); returnresult; } } |
3.6. Controller
@Controller @RequestMapping("/item") public class ItemController { @Autowired private ItemService itemService; @RequestMapping("/list") //設定相應的內容為json資料 @ResponseBody public EasyUIResult getItemlist(@RequestParam(defaultValue="1")Integer page, @RequestParam(defaultValue="30")Integer rows) throws Exception { //查詢商品列表 EasyUIResult result = itemService.getItemList(page, rows); return result; } } |
相關推薦
帶你逐步深入瞭解SSM框架——淘淘商城專案之框架整合,後臺系統搭建
1. 今日計劃 1、 SSM框架整合 2、 mybatis逆向工程 3、 商品列表 4、 商品列表分頁處理 2. SSM框架整合 2.1. 後臺系統所用的技術 框架:Spring + SpringMVC +Mybatis 前端:EasyUI 資料庫:mysql
帶你逐步深入瞭解SSM框架——淘淘商城專案之實現商品新增
1. 課程計劃 完成商品新增功能 1、商品類目選擇 2、圖片上傳 3、圖片伺服器搭建 4、kindEditor富文字編輯器的使用 5、商品新增功能 2. 實現商品類目選擇功能 2.1. 需求
帶你逐步深入瞭解SSM框架——淘淘商城專案之專案總結
1. 專案總結 總結淘淘商城中用到的技術點: 1.1. 專案工程搭建。 1、使用maven構建工程。Maven的繼承、聚合、依賴管理。 2、Svn的使用,svn上傳下載程式碼。 1.2. ssm框
帶你逐步深入瞭解SSM框架——淘淘商城專案之購物車+訂單實現
1 今日大綱 1、 實現淘淘商城的購物車功能 2、 實現訂單系統。 2 購物車功能 2.1 功能說明 1、商品加入購物車時,不是必須要求登入。 2、計算購物車中商品的總價。當商品數量發
帶你逐步深入瞭解SSM框架——淘淘商城專案之redis快取
1. 課程計劃 1、 Redis服務搭建 2、 為功能新增快取功能 2. redis介紹 2.1. 什麼是redis Redis是用C語言開發的一個開源的高效能鍵值對(k
帶你逐步深入瞭解SSM框架——淘淘商城專案之商品詳情頁面實現
1. 課程計劃 l 商品詳情頁實現 1、商品查詢服務事項 2、商品詳情展示 3、新增快取 2. 實現商品詳情頁功能 2.1. 功能分析 1、Taotao-portal
帶你逐步深入瞭解SSM框架——淘淘商城專案之單點登入系統實現
1. 課程計劃 1、 實現單點登入系統 2、 實現使用者的登入功能 3、 實現使用者的註冊功能 2. 單點登入系統分析 2.1. 什麼是SSO SSO英文全稱Single Sign On
帶你逐步深入瞭解SSM框架——SpringMVC框架詳解
1 SpringMVC架構 1.1 Spring web mvc介紹 Spring web mvc和Struts2都屬於表現層的框架,它是Spring框架的一部分,我們可以從Spring的整體結構中看得出來: 1.2
帶你逐步深入瞭解SSH框架——ssh框架整合
. 今天內容介紹 SSH三大框架整合 (1)struts2 (2)hibernate5.x
帶你逐步深入瞭解SSH框架——struts2入門
. 今天內容 1 struts2概述 (1)應用在web層 2 struts2入門案例 3 struts2底層執行過程 4 struts2相關配置 (1)struts.xml配置 - packa
阿里程式設計師帶你全面深入瞭解正則表示式
在日常工作中,經常會用到正則操作。但是對於大多數人來說,操作正則表示式簡直就是抓瞎。 本篇文章主要整理了正則表示式匹配的規則,使用中的一些要點,以及用圖形化的方式列舉出一些常見的正則表示式,希望能給大家帶來一定的幫助,能在以後的工作中,用上正則,愛上正則。 PS:不同語言中的正則表示式的規則
python框架Django實戰商城專案之工程搭建
# 專案說明 該電商專案類似於京東商城,主要模組有驗證、使用者、第三方登入、首頁廣告、商品、購物車、訂單、支付以及後臺管理系統。 專案開發模式採用前後端不分離的模式,為了提高搜尋引擎排名,頁面整體重新整理採用jinja2模板引擎實現,區域性重新整理採用vue.js實現。 **專案執行機制如下:** ![fi
用Unity做遊戲,你需要深入瞭解一下IL2CPP
這次我們翻譯了一篇Unity官方部落格上的文章,原文題目為AN INTRODUCTION TO IL2CPP INTERNALS ,作者是從事Unity軟體開發的Joshua Peterson。文章的看點在於,它是以IL2CPP內部開發人員的角度來講述的,所以對於開發者來說非常有參考價值。 如果
帶你一步步瞭解業務測打款系統的建立
1.專案背景 初始階段 業務方訂單稽核通過後,會有離線任務不斷輪訓向支付中心發起呼叫,支付中心打款處理完成後會返回ifSuccess(是否落庫),state,code,error Message等。如果落庫且code為打款成功,訂單業務狀態會修改為打款成功。 發展階段 為了配合業務發展,會增加各種活動來拉動訂
如果你未曾深入瞭解python,請先看看這篇python簡史!
python與人工智慧緊緊的聯絡在一起,現在很多年輕的開發者都開始學習Python,文章清晰且幽默的講述了python的發展史,希望對還在自學python的你有些幫助,或重拾自學python的激情。 學習Python中有不明白推薦加入交流群 &n
帶你讀深入理解Java虛擬機器
5年碼農一枚,一直在傳統行業,現在的工作輕鬆卻無趣,打算給自己3個月時間年前換個有挑戰性的工作。之前工作中沒有太注重理論知識的學習,對新技術也沒有深入瞭解。以此為界,從《深入理解Java虛擬機器》開始,以換高薪工作為目的,將自己這段時間所學記錄下來,作為一個總
引導你逐步深入學習C++
作者簡介:安曉輝,程式設計師、技術管理者、作家、職業規劃師、在行行家、分答答主,著有《Qt on Android 核心程式設計》、《Qt Quick核心程式設計》和《你好哇,程式設計師》,建立訂閱號“程式視界”。 C++是一門古老而複雜的語言,絕不是一門可以速成的語言
chrome 主頁被篡改為hao123?技術宅帶你層層深入破之 -轉自知乎
作者為:http://www.zhihu.com/people/xiang-liao-hao-jiu 前幾天,突然發現預設瀏覽器的Chrome的主頁被篡改為了hao123。每次第一次開啟,都自動跳轉到http://www.hao123.com/?tn=29065018_59
通過專案逐步深入瞭解Spring MVC(一)
相關閱讀: 如果覺得不錯的話,歡迎給個 star , 如果你想完善這個專案的話,你也可以 fork 後修改然後推送給我。 轉載請註明出處和保留以上文字! 瞭解 Spring: 一個好的東西一般都會有一個好的文件解釋說明,如果你英語還行,建
通過專案逐步深入瞭解Mybatis(四)
延遲載入 什麼是延遲載入? resultMap可以實現高階對映(使用association、collection實現一對一及一對多對映),association、collection具備延遲載入功能。 需求: 如果查詢訂單並且關聯查詢使用者資訊。如果先查詢訂單