- 引入依赖(pom.xml导包)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.oracle</groupId>
<artifactId>ssm-shop</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<!-- 自定义属性 标签名 自定义 标签值 属性的值
后面的配置需要用到该属性的值 ${spring.version}
-->
<spring.version>5.1.6.RELEASE</spring.version>
<mybatis.version>3.4.6</mybatis.version>
</properties>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.9</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<!-- 作用域
compile:编译,测试,运行
test:测试
provided:编译,测试
runtime:运行
system:编译,测试,同provided,但必须指定systemPath,慎用
-->
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- servelt 文件上传-->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
<!-- json转换 -->
<!-- jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<!-- spring和mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!-- aop和事务 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.6</version>
</dependency>
<!--事务 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- log4J-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.16</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- mybatis的分页助手 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.6</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
</dependency>
</dependencies>
</project>
- mybatis.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">
<!-- xml的约束文件一般都是dtd,这个约束文件就约束了你使用哪些标签 ,再配置自动提示,
方便开发
-->
<!-- 核心配置文件 -->
<configuration>
<!-- 起别名,可以在mapper.xml文件中使用实体类的类名-->
<typeAliases>
<package name="com.oracle.shop.po"/>
</typeAliases>
</configuration>
- 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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd" >
<!-- 引入spring相关的约束,dtd、xsd,这是一个网络地址 -->
<!-- 开启springmvc的注解支持
默认自带了 基于注解的处理器映射器和基于注解的处理器适配器
-->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 配置一个扫包器,去扫描指定包下的所有注解(会扫到controller注解,实例化处理器对象,requestMapping等也会扫到) -->
<context:component-scan base-package="com.oracle.shop.controller"></context:component-scan>
<!-- 思考:springmvc需要5大组件一块工作,但是我好像只配了dispatcherServlet和handler,为什么? -->
<!-- 视图解析器如果不配,会默认提供一个基于jsp的视图解析器
springmvc推荐视图的位置存储在不公开的路径下比如WEB-INF
-->
<bean id="viewResover" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 可以给视图设置前缀和后缀
前缀是/WEB-INF/pages/
后缀是.jsp
-->
<property name="prefix" value="/WEB-INF/pages/"/>
<!-- 配置后缀 -->
<property name="suffix" value=".jsp" />
</bean>
<mvc:resources location="/static/" mapping="/static/**"></mvc:resources>
<!-- 配置文件上传解析器 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
</bean>
</beans>
- spring-mybatis.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd" >
<!-- 加载jdbc的配置文件 -->
<context:property-placeholder location="classpath:jdbc/jdbc.properties" />
<!-- spring实例化数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- spring实例化sqlSessionFactory以及mapper接口的代理对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 加载mybatis的核心配置文件 -->
<property name="configLocation" value="classpath:mybatis/mybatis.xml" />
<!-- 数据源 -->
<property name="dataSource" ref="dataSource"/>
<!-- 映射文件位置 -->
<property name="mapperLocations" value="classpath:mappers/*.xml"></property>
<!-- pageHelper分页 -->
<!-- 配置分页插件 -->
<!-- 指定分页助手的数据库方言是mysql
mysql和oracle的区别:
1:mysql是支持主键自增
oracle通过sequence来实现主键自增
2:mysql是支持limit语句,
oracle不支持limit语句,如果oracle进行分页查询
需要借助于rownum(伪列)进行两次子查询才能实现分页
3:mysql和oracle所能使用的函数不同
4:数据类型轻微不同,oracle当中vachar2
数据库方言
dialect=mysql
分页合理为true
reasonable=true
-->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageHelper">
<property name="properties">
<value>
dialect=mysql
reasonable=true
</value>
</property>
</bean>
</array>
</property>
</bean>
<!--mapper扫描-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.oracle.shop.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<!-- 配置扫包器,扫描service autoWired等注解 -->
<context:component-scan base-package="com.oracle.shop">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 在service层 哪些方法 是需要提交事务
增删改
add insert delete update 手动提交事务 其实就是后置增强
aop配置一个增强类,后置增强提交事务的逻辑
配置切面
-->
<!--
spring提供了基于aop实现的事务管理器
项目当中事务是怎么控制的?
我们采用spring的声明式的事务管理,设置切面在service的实现类这一层,
配置事务的传播特性,对于增删改开头的方法,使用事务进行控制,对于查询开头的方法,设置成只读
你们的项目当中的事务控制在哪一层?
service实现类这一层
-->
<!-- 待定:事务管理 aop待完成 -->
<!-- spring提供了声明式的事务管理机制 -->
<!-- 配置事务管理器,让spring的事务管理器去对事务进行统一管理 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 切面增强 spring提供了自带的事务增强类 tx
设置一下tx事务增强类的传播特性
-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 传播行为
tx method指定 切面路径下 方法名 name=xxx 此次的事务传播行为
required 需要使用事务进行统一管理
read-only=true 只读模式,查询行为,不需要事务进行管理
对于增删改开头的方法,使用事务进行管理
对于查询开头的方法,只读
propagion_XXX :事务的传播行为
* 保证同一个事务中
propagion_required: 支持当前事务,如果不存在 就新建一个(默认)
propagion_supports: 支持当前事务,如果不存在,就不使用事务
propagion_mandatory: 支持当前事务,如果不存在,抛出异常
* 保证没有在同一个事务中
propagion_requires_new: 如果有事务存在,挂起当前事务,创建一个新的事务
propagion_not_supported: 以非事务方式运行,如果有事务存在,挂起当前事务
propagion_never: 以非事务方式运行,如果有事务存在,抛出异常
propagion_nested: 如果当前事务存在,则嵌套事务执行
https://www.cnblogs.com/dwxt/p/8807899.html
-->
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="select*" propagation="SUPPORTS" read-only="true" />
<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 设置切面 把事务管理的切面设置在service实现类这一层-->
<aop:config>
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* com.oracle.shop.service.impl.*.*(..))" />
</aop:config>
</beans>
- spring-redis.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd" >
<bean id="poolconfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="5"></property>
<property name="maxTotal" value="10"></property>
</bean>
<bean id="jediscluster" class="redis.clients.jedis.JedisCluster">
<constructor-arg index="0" >
<set>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg index="0" value="192.168.192.3"></constructor-arg>
<constructor-arg index="1" value="7001"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg index="0" value="192.168.192.3"></constructor-arg>
<constructor-arg index="1" value="7002"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg index="0" value="192.168.192.3"></constructor-arg>
<constructor-arg index="1" value="7003"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg index="0" value="192.168.192.3"></constructor-arg>
<constructor-arg index="1" value="7004"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg index="0" value="192.168.192.3"></constructor-arg>
<constructor-arg index="1" value="7005"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg index="0" value="192.168.192.3"></constructor-arg>
<constructor-arg index="1" value="7006"></constructor-arg>
</bean>
</set>
</constructor-arg>
<constructor-arg index="1" ref="poolconfig"></constructor-arg>
</bean>
</beans>
- 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_3_0.xsd" id="WebApp_ID" version="3.0">
<!-- 配置全局的spirng-session过滤器,重写httpSession,这段配置放在web.xml最前面-->
<servlet>
<servlet-name>dispatcherServlet</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>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 统一编码的过滤器 -->
<!-- 统一编码的过滤器 -->
<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>
<!-- 配置spring ioc的监听器,监听ioc容器的创建 -->
<!-- 该监听器会监听ioc容器的创建工作 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring/spring-*.xml
</param-value>
</context-param>
</web-app>