6.1. 數據庫

使用 mysql 數據,要求版本在 5.5 以上。
1、 在數據庫中創建庫 egoubuy
2、 通過腳本將數據導入數據庫

6.2. Mybatis 逆向工程
使用 mybatis 官方提供的逆向工程生產對應的 pojo、mapper 等文件。

generatorConfig.xml 配置
~~~
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<context id="testTables" targetRuntime="MyBatis3">
<commentGenerator>
<!-- 是否去除自動生成的注釋 true:是 : false:否 -->
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--數據庫連接的信息:驅動類、連接地址、用戶名、密碼 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/egoubuy"
userId="root"
password="root">
</jdbcConnection>
<!-- 默認false,把JDBC DECIMAL 和 NUMERIC 類型解析為 Integer,為 true
時把JDBC DECIMAL 和
NUMERIC 類型解析為java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- targetProject:生成PO類的位置 -->
<javaModelGenerator targetPackage="com.igeek.pojo"
targetProject=".\src">
<!-- enableSubPackages:是否讓schema作為包的后綴 -->
<property name="enableSubPackages" value="false" />
<!-- 從數據庫返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- targetProject:mapper映射文件生成的位置 -->
<sqlMapGenerator targetPackage="com.igeek.mapper"
targetProject=".\src">
<!-- enableSubPackages:是否讓schema作為包的后綴 -->
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- targetPackage:mapper接口生成的位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.igeek.mapper"
targetProject=".\src">
<!-- enableSubPackages:是否讓schema作為包的后綴 -->
<property name="enableSubPackages" value="false" />
</javaClientGenerator>
<!-- 指定數據庫表 -->
<table schema="" tableName="tb_content"></table>
<table schema="" tableName="tb_content_category"></table>
<table schema="" tableName="tb_item"></table>
<table schema="" tableName="tb_item_cat"></table>
<table schema="" tableName="tb_item_desc"></table>
<table schema="" tableName="tb_item_param"></table>
<table schema="" tableName="tb_item_param_item"></table>
<table schema="" tableName="tb_order"></table>
<table schema="" tableName="tb_order_item"></table>
<table schema="" tableName="tb_order_shipping"></table>
<table schema="" tableName="tb_user"></table>
</context>
</generatorConfiguration>
~~~
執行 main 方法

注意:main 方法只能執行一次,如果要執行第二次一定要將原來生成的文件刪
除。
將生成的 pojo 文件拷貝到 pojo 工程中。mapper 文件拷貝到 dao 工程中。
6.3. 整合思路
mybatis 整合 spring,通過由 spring 創建數據庫連接池,spring 管
理 SqlSessionFactory、mapper 代理對象。需要 mybatis 和 spring
的整合包。
1、 Dao 層需求
a) Mybatis 的配置文件: SqlMapperConfig.xml。
該文件不需要任何配置,需要文件頭。 但是文件必須存在。
(spring 會加載該文件)
b) Spring 的文件:applicationContext-dao.xml
2、 service 層
a) spring 配置:applicationContext-service.xml
配置掃描包,將所有的service所有的 bean放在 spring容器中。
b) 事務配置:applicationContext-trans.xml
3、 表現層
a) springMVC 框架配置。
b) springMVC 的三大組件。
所有的配置文件全部放在 web 中,因為其他的項目都會打包到 web
工程中。

6.4. DAO 層的配置
6.4.1. 配置 SqlMapperConfig.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>
~~~
6.4.2. 配 置 spring 整 合 mybatis 的 配 置 文 件 :
applicationContext-dao.xml

~~~
<?xml version="1.0" encoding="UTF-8"?>
<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.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<!-- 數據庫連接池 -->
<!-- 加載配置文件 -->
<context:property-placeholder
location="classpath:conf/db.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/SqlMapperConfig.xml" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.igeek.egobuy.mapper" />
</bean>
</beans>
~~~
6.4.3. 添加 db.properties 配置文件

~~~
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/egoubuy?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root
~~~
6.5. Service 整合
6.5.1. 管理 service 的 spring 配置文件:application-service.xml
~~~

<?xml version="1.0" encoding="UTF-8"?>
<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.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<context:component-scan base-package="com.igeek.egobuy.service"/>
</beans>
~~~
6.5.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.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<!-- 事務管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionMana
ger">
<!-- 數據源 -->
<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.igeek.egobuy.service..*.*(..))" />
</aop:config>
</beans>
~~~
6.6. 表現層整合
6.6.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-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<context:component-scan base-package="com.igeek.egobuy.controller" />
<mvc:annotation-driven />
<!--視圖解析器-->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewReso
lver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
~~~
6.6.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"
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>buy-manager</display-name>
<welcome-file-list>
<welcome-file>index.jsp</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.ContextLoaderListene
r</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>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- springmvc的前端控制器 -->
<servlet>
<servlet-name>buy-manager</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</se
rvlet-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>buy-manager</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
~~~
實體類實現序列化
public class TbItem implements Serializable{}
- spring
- 1.spring第一天
- 1.1 安裝spring插件(spring tool suite)
- 1.2 spring概述
- 1.3 控制反轉&依賴注入
- 1.4 springIOC容器
- 1.5 依賴注入的四種方式
- 1.6 配置bean的細節
- 1.7 bean之間的關系
- 1.8 bean作用域
- 1.9 補充:創建對象的幾種方法
- 1源代碼位置
- 2.spring第二天
- 2.1 使用外部屬性文件
- 2.2 spEL
- 2.3 bean的生命周期
- 2.4 通過工廠方式配置bean
- 2.5 基于注解的方式配置bean
- 2.6 組件裝配
- 2.7 靜態代理
- 2.8 動態代理
- 2.9 Cglib代理
- 2源代碼位置
- 3. spring第三天
- 3.1 springAOP
- 3.1.1 AOP簡介
- 3.1.2 為什么使用AOP
- 3.1.3 AOP關鍵術語
- 3.1.4 AOP圖解
- 3.1.5 springAOP實現步驟
- 3.1.6 SpringAOP實現原理:
- 3.1.7 AOP的好處
- 3.1.8 AOP在實際項目中的主要應用
- 3代碼地址
- 3.1.9 純注解版配置aop的方式
- 3.2 maven環境搭建
- 附IDEA激活碼
- 4. spring第四天
- 4.1 c3p0事務
- 4.2 命令窗口事務
- 4.3 c3p0連接池設置
- 4.4 事務中的一些基本概念
- 4.5 事務的傳播行為
- 4.6 自定義異常
- 4.7 spring整合Junit單元測試
- 4.8 JdbcTemplate(附源代碼)
- 事務源代碼
- 4.9 純注解tx
- 4.10 基于xml配置事務
- 0. jsp頁面修改編碼方式
- 0.1 eclipse配置tomcat
- 0.單例模式-飽漢模式
- 0.單例模式-饑漢模式
- springMVC
- 1. springmvc第一天
- 1.1 springMVC概述
- 1.2 springmvc框架搭建及第一個應用程序
- 1.3 @RequestMapping
- 1.4 RequestMapping修飾類
- 1.5 RequestMapping精準化映射
- 1.6 Ant風格URL
- 1.7 帶有占位符的url映射
- 1.8 REST風格
- 1.9 RequerstParam獲取請求正文
- 2. springmvc第二天
- 2.1 優化
- 2.2 POJO綁定請求參數
- 2.3 RequestHeader獲取請求報頭信息
- 2.4 CookieValue獲取Cookie信息
- 2.5 獲取原生ServletAPI
- 2.6 ModelAndView處理模型數據
- 2.7 Map、Model、ModelMap處理模型數據
- 2.8 @SessionAttributes注解
- 2.9 @ModelAttribute無返回值方法及方法入參
- 2.10 @ModelAttribute修飾有返回值類型的方法
- 代碼地址
- 3. springmvc補充
- 3-1 springmvc工作原理
- 3-2 springmvc form表單提交中文亂碼
- 3-3 數據的格式化
- 3-4 自定義類型轉換器
- 3-5 其他知識點
- 3-6 crud代碼
- 3-7 @DateTimeFormat日期格式化
- 3-8 數據驗證的概念及JSR303驗證
- 3-9 Hibernate-Validator驗證框架
- 3-10 Controller捕獲錯誤消息
- 3-11 errors標簽在頁面中獲取錯誤消息
- 3-12 錯誤消息的定制及國際化
- 3-13 自定義攔截器
- 3-14 Java代碼中獲取國際化信息
- 3-15 超級鏈接設置國際化
- 3-16 AJAX支持之@RequestBody
- mybatis
- 1. mybatis第一天
- 1. 為什么使用mybatis
- 2. 下載地址
- 3. hello
- 4. mybatis三種開發模式
- 5. 全局配屬屬性內容
- 6. DTD設置
- 7. Mapper中的CRUD
- 8. 8.mybatis使用主鍵自增
- 9. #{}中的參數處理
- 10. #{}與${}區別
- 11. 集合數據的查詢
- 12 動態sql
- 12.1 if
- 12.2 choose, when, otherwise
- 12.3 trim, where, set
- 12.4 foreach
- 代碼位置
- 2. mybatis第二天
- 1.封裝map類型的數據
- 2. resultMap自定義封裝規則
- 0代碼位置
- 3. mybatis緩存機制
- ssm整合
- 1.maven
- 2.ssm基礎環境搭建
- 2-1 引入項目依賴的jar包
- 2-2 引入bootstrap,jquery
- 2-3 創建項目包結構
- 2-4 編寫web.xml配置文件
- 2-5 編寫sping,springmvc,mybatis配置文件
- 2-6 逆向工程mbg.xml
- shiro安全框架
- 1.shiro簡介
- 易購Buy商城
- 第一天
- 1.課程計劃
- 2.電商行業背景
- 3.易購Buy介紹
- 4.易購Buy架構
- 5.工程搭建
- 6.工程啟動和測試
- 7.ssm框架整合
- 8.整合測試
- 9.svn
- 9.1 svn服務端
- 9.2 svn客戶端
- 第二天
- 1.SOA架構分析
- 2.dubbo使用方法
- 3.注冊中心
- 4.工程改造
- 5.easyUI
- maven
- 1.maven介紹
- 2.idea配置maven和服務器
- 3.創建web工程
- 4.分模塊構建工程
- 5. 代碼位置
- 6. nexus
- Luence搜索
- 1.了解搜索技術
- 2.Lucene的基本使用
- solr
- SolrCloud