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
