我之所以写一个快速搭建的demo,主要想做一些容器的demo,所以为了方便大家,所以一切从简,简单的3层架构

先用mysql的ddl,后期不上oracle的ddl

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `sys_com_code`
-- ----------------------------
DROP TABLE IF EXISTS `sys_com_code`;
CREATE TABLE `sys_com_code` (
`CODE_TYPE` varchar(100) COLLATE utf8_bin DEFAULT NULL,
`CODE_C_TYPE` varchar(100) COLLATE utf8_bin DEFAULT NULL,
`CODE_VALUE` varchar(100) COLLATE utf8_bin DEFAULT NULL,
`CODE_NAME` varchar(100) COLLATE utf8_bin DEFAULT NULL,
`STATUS_CD` varchar(100) COLLATE utf8_bin DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin; -- ----------------------------
-- Records of sys_com_code
-- ----------------------------
INSERT INTO `sys_com_code` VALUES ('status', '', '', '无效', '');
INSERT INTO `sys_com_code` VALUES ('status', '', '', '有效', ''); -- ----------------------------
-- Table structure for `sys_resource`
-- ----------------------------
DROP TABLE IF EXISTS `sys_resource`;
CREATE TABLE `sys_resource` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`pid` int(10) DEFAULT NULL,
`name` varchar(100) COLLATE utf8_bin DEFAULT NULL,
`url` varchar(100) COLLATE utf8_bin DEFAULT NULL,
`code` varchar(100) COLLATE utf8_bin DEFAULT NULL,
`type` int(2) DEFAULT NULL,
`sorts` int(2) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 COLLATE=utf8_bin; -- ----------------------------
-- Records of sys_resource
-- ----------------------------
INSERT INTO `sys_resource` VALUES ('', '-1', '系统设置', 'system', 'system', '', '');
INSERT INTO `sys_resource` VALUES ('', '-1', '管理首页', 'home', 'home', '', '');
INSERT INTO `sys_resource` VALUES ('', '', '用户授权', 'user', 'user', '', '');
INSERT INTO `sys_resource` VALUES ('', '', '角色管理', '/role/list.do', 'role:list.do', '', '');
INSERT INTO `sys_resource` VALUES ('', '', '用户管理', '/user/list.do', 'user:list.do', '', '');
INSERT INTO `sys_resource` VALUES ('', '', '资源管理', '/resource/list.do', 'resource:list.do', '', '');
INSERT INTO `sys_resource` VALUES ('', '', '知识案例', 'demo', 'demo', '', '');
INSERT INTO `sys_resource` VALUES ('', '', 'freemarker案例', '/freemarker/list.do', 'freemarker:list.do', '', ''); -- ----------------------------
-- Table structure for `sys_role`
-- ----------------------------
DROP TABLE IF EXISTS `sys_role`;
CREATE TABLE `sys_role` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(50) COLLATE utf8_bin DEFAULT NULL,
`status` int(2) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_bin; -- ----------------------------
-- Records of sys_role
-- ----------------------------
INSERT INTO `sys_role` VALUES ('', '超级管理员', '');
INSERT INTO `sys_role` VALUES ('', '普通管理员', '');
INSERT INTO `sys_role` VALUES ('', '普通用户', ''); -- ----------------------------
-- Table structure for `sys_role_resource`
-- ----------------------------
DROP TABLE IF EXISTS `sys_role_resource`;
CREATE TABLE `sys_role_resource` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`role_id` int(10) DEFAULT NULL,
`resource_id` int(10) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=56 DEFAULT CHARSET=utf8 COLLATE=utf8_bin; -- ----------------------------
-- Records of sys_role_resource
-- ----------------------------
INSERT INTO `sys_role_resource` VALUES ('', '', '-1');
INSERT INTO `sys_role_resource` VALUES ('', '', '');
INSERT INTO `sys_role_resource` VALUES ('', '', '');
INSERT INTO `sys_role_resource` VALUES ('', '', '');
INSERT INTO `sys_role_resource` VALUES ('', '', '');
INSERT INTO `sys_role_resource` VALUES ('', '', '');
INSERT INTO `sys_role_resource` VALUES ('', '', '-1');
INSERT INTO `sys_role_resource` VALUES ('', '', '');
INSERT INTO `sys_role_resource` VALUES ('', '', '');
INSERT INTO `sys_role_resource` VALUES ('', '', '');
INSERT INTO `sys_role_resource` VALUES ('', '', '');
INSERT INTO `sys_role_resource` VALUES ('', '', '-1');
INSERT INTO `sys_role_resource` VALUES ('', '', '');
INSERT INTO `sys_role_resource` VALUES ('', '', '');
INSERT INTO `sys_role_resource` VALUES ('', '', '');
INSERT INTO `sys_role_resource` VALUES ('', '', '');
INSERT INTO `sys_role_resource` VALUES ('', '', '-1');
INSERT INTO `sys_role_resource` VALUES ('', '', '');
INSERT INTO `sys_role_resource` VALUES ('', '', '');
INSERT INTO `sys_role_resource` VALUES ('', '', '');
INSERT INTO `sys_role_resource` VALUES ('', '', '');
INSERT INTO `sys_role_resource` VALUES ('', '', '');
INSERT INTO `sys_role_resource` VALUES ('', '', '');
INSERT INTO `sys_role_resource` VALUES ('', '', '');
INSERT INTO `sys_role_resource` VALUES ('', '', ''); -- ----------------------------
-- Table structure for `sys_user`
-- ----------------------------
DROP TABLE IF EXISTS `sys_user`;
CREATE TABLE `sys_user` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`user_name` varchar(50) COLLATE utf8_bin DEFAULT NULL,
`password` varchar(500) COLLATE utf8_bin DEFAULT NULL,
`age` int(2) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8 COLLATE=utf8_bin; -- ----------------------------
-- Records of sys_user
-- ----------------------------
INSERT INTO `sys_user` VALUES ('', 'admin', '53d8c7cee066fec7840bcbbfb1733192', '');
INSERT INTO `sys_user` VALUES ('', 'superadmin', '53d8c7cee066fec7840bcbbfb1733192', '');
INSERT INTO `sys_user` VALUES ('', 'loafer', '53d8c7cee066fec7840bcbbfb1733192', ''); -- ----------------------------
-- Table structure for `sys_user_role`
-- ----------------------------
DROP TABLE IF EXISTS `sys_user_role`;
CREATE TABLE `sys_user_role` (
`id` int(30) NOT NULL AUTO_INCREMENT,
`role_id` int(30) DEFAULT NULL,
`user_id` int(30) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COLLATE=utf8_bin; -- ----------------------------
-- Records of sys_user_role
-- ----------------------------
INSERT INTO `sys_user_role` VALUES ('', '', '');
INSERT INTO `sys_user_role` VALUES ('', '', '');
INSERT INTO `sys_user_role` VALUES ('', '', '');

sql-ddl.sql

预先准备:用插件自动生成自动生成DAO层,链接在(后期补上)

第一步:创建maven工程,导入依赖包,目前我的工程暂时叫MyArtifact

<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>ssmtest</groupId>
<artifactId>ssmtest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- spring 相关jar -->
<!-- Spring 4.3.3 -->
<!-- Core Container 核心容器 -->
<!-- 核心工具类,Spring其它模块大量使用Spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<!-- 运行时Spring容器 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<!-- Spring容器对第三方包的集成 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.3.RELEASE</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<!-- Spring定义Bean的支持 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<!-- 使用表达式语言在运行时查询和操作对象 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<!-- End Core Container 核心容器 --> <!-- AOP -->
<!-- 基于代理的AOP支持 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<!-- 基于AspectJ的AOP支持 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<!-- aspectj -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.5</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.5</version>
</dependency>
<!-- End AOP --> <!-- web -->
<!-- 提供基础的web集成的功能,在web项目中提供Spring容器 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<!-- 提供基于Servlet的SpringMVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<!-- 提供WebSocket功能 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<!-- 提供Portlet环境支持 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc-portlet</artifactId>
<version>4.3.3.RELEASE</version>
</dependency> <!-- json --> <dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-lgpl</artifactId>
<version>1.9.6</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.5</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-lgpl</artifactId>
<version>1.9.6</version>
</dependency> <!-- End web --> <!-- DataAccess/Integration 数据访问/集成 -->
<!-- 提供以JDBC访问数据库的支持 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<!-- 提供编程式和声明式的事务支持 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<!-- 提供对 对象/关系映射技术的支持 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<!-- 提供对JMS的支持 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<!-- End DataAccess/Integration 数据访问/集成 -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<!-- End Spring 4.0 --> <!-- mybatis jar -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.0.1</version>
</dependency>
<!-- 数据源驱动包 -->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
<type>jar</type>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.1.1</version>
</dependency> <!-- 打印日志 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.22</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.22</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency> <!-- jsp jar -->
<!-- JSP相关 --> <dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency> <!-- 单元测试 -->
<dependency>
<groupId>org.unitils</groupId>
<artifactId>unitils-core</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>org.unitils</groupId>
<artifactId>unitils-dbunit</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>org.unitils</groupId>
<artifactId>unitils-io</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>org.unitils</groupId>
<artifactId>unitils-database</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>org.unitils</groupId>
<artifactId>unitils-spring</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>org.dbunit</groupId>
<artifactId>dbunit</artifactId>
<version>2.5.3</version>
</dependency> <dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<version>3.5.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<version>RELEASE</version>
</dependency> <dependency>
<groupId>org.unitils</groupId>
<artifactId>unitils-core</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>org.unitils</groupId>
<artifactId>unitils-dbunit</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>org.unitils</groupId>
<artifactId>unitils-io</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>org.unitils</groupId>
<artifactId>unitils-database</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>org.unitils</groupId>
<artifactId>unitils-spring</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>org.dbunit</groupId>
<artifactId>dbunit</artifactId>
<version>2.5.3</version>
</dependency> </dependencies> <build> <finalName>MyArtifact</finalName> <plugins>
<!-- For Maven Tomcat Plugin -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<!-- or if you want to use tomcat 6.x <artifactId>tomcat6-maven-plugin</artifactId> -->
<version>2.2</version>
<configuration>
<server>Tomcat7</server> <!-- 与{tomcatHome}/conf/setting.xml 中的serverID相同 -->
<url>http://localhost:8080/manager/text</url><!-- tomcat管理路径 -->
<!-- http port -->
<port>8080</port>
<!-- application path always starts with / -->
<path>/MyArtifact</path>
<!-- 可以在该命令下启动tomcat时自动加载修改的class,而不用重新发布。 -->
<contextReloadable>true</contextReloadable>
</configuration> </plugin> </plugins>
</build>
</project>

pom

第二步:

创建3个资源文件(最简化的)

mybatis-config.xml          (一些mybatis 的设置,和插件,例:pagehelper)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC
"-//mybatis.org//DTD Config 3.0//EN"
"WEB-INF/dtd/mybatis-3-config.dtd">
<configuration> <!-- 分页插件 -->
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<property name="helperDialect" value="oracle" />
<property name="offsetAsPageNum" value="true" />
<property name="rowBoundsWithCount" value="true" />
<property name="pageSizeZero" value="true" />
<property name="reasonable" value="true" />
<property name="params"
value="pageNum=start;pageSize=limit;pageSizeZero=zero;reasonable=heli;count=contsql" />
</plugin>
</plugins>
</configuration>

mybatis-config.xml

第二个配置文件 applicationContext-*.xml(或者取其他名字)

作用:1>.数据源(两个例子)

<!-- 数据库连接池 :dataSource -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl"
value="jdbc:mysql://localhost:3306/play?useUnicode=true&amp;characterEncoding=UTF-8" />
<property name="user" value="root" />
<property name="password" value="root" />
<property name="minPoolSize" value="1" />
<property name="maxPoolSize" value="20" />
<property name="maxIdleTime" value="1800" />
<property name="acquireIncrement" value="2" />
<property name="maxStatements" value="0" />
<property name="initialPoolSize" value="2" />
<property name="idleConnectionTestPeriod" value="1800" />
<property name="acquireRetryAttempts" value="30" />
<property name="breakAfterAcquireFailure" value="true" />
<property name="testConnectionOnCheckout" value="false" />
</bean>

数据源demo

   

<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
destroy-method="close">
<!-- 必须的配置 -->
<property name="driverClassName" value="oracle.jdbc.OracleDriver" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="defaultAutoCommit" value="false" /> <!-- 可选配置 -->
<!-- 连接池启动时创建的初始化连接数量(默认值为0) -->
<property name="initialSize" value="${jdbc.initialSize}" /> <!-- 连接池中最大的空闲的连接数,超过的空闲连接将被释放,如果设置为负数表示不限制(默认为8个,maxIdle不能设置太小, 因为假如在高负载的情况下,连接的打开时间比关闭的时间快,会引起连接池中idle的个数
上升超过maxIdle,而造成频繁的连接销毁和创建,类似于jvm参数中的Xmx设置) -->
<property name="maxIdle" value="${jdbc.maxIdle}" /> <!-- 连接池中最小的空闲的连接数,低于这个数量会被创建新的连接(默认为0,调整为5,该参数越接近maxIdle,性能越好, 因为连接的创建和销毁,都是需要消耗资源的;
但是不能太大,因为在机器很空闲的时候,也会创建低于minidle个数的连接,类似于jvm参数中的Xmn设置) -->
<property name="minIdle" value="${jdbc.minIdle}" /> <!-- 并发连接的最大数。设置为0则无限制 -->
<property name="maxTotal" value="${jdbc.maxTotal}" /> <!-- 等待连接的最大连接的时间,以毫秒计 -->
<property name="maxWaitMillis" value="${jdbc.maxWaitMillis}" /> <!--SQL查询,用来验证从连接池取出的连接,在将连接返回给调用者之前. -->
<property name="validationQuery" value="select sysdate from dual" /> <!--指明是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个 -->
<property name="testOnBorrow" value="true" />
</bean> </beans>

数据源demo2

2>. 数据库配置SqlSessionFactoryBean(主要是:扫描mapper,定义事物管理)

    <!-- 数据库配置SqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml" />
</bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.howbuy.otc.dao.mapper" />
</bean> <bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>

数据库配置SqlSessionFactoryBean

    3>.service层配置

<!-- 导入文件 -->
<!--<import resource="classpath:spring-service.xml" />--> <!-- 如果存在一个事务,则支持当前事务。如果没有事务则开启 -->
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="transactionManager" />
</bean> <!-- 以AspectJ方式 定义 事务 -->
<aop:config proxy-target-class="true" >
<aop:advisor pointcut="execution(* com.howbuy.otc.invest.service..*.*(..))"
advice-ref="txAdvice" order="2"/>
</aop:config> <!-- 配置事务传播特性:如果存在一个事务,则支持当前事务。如果没有事务则开启:REQUIRED -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="update*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="save*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="exec*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="insert*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="merge*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="batch*" propagation="REQUIRED" rollback-for="Exception" /> <tx:method name="add*Atom" propagation="REQUIRES_NEW" rollback-for="Exception" />
<tx:method name="update*Atom" propagation="REQUIRES_NEW" rollback-for="Exception" />
<tx:method name="save*Atom" propagation="REQUIRES_NEW" rollback-for="Exception" />
<tx:method name="delete*Atom" propagation="REQUIRES_NEW" rollback-for="Exception" />
<tx:method name="*Atom" propagation="REQUIRES_NEW" rollback-for="Exception" /> <tx:method name="get*" propagation="SUPPORTS" rollback-for="Exception" />
<tx:method name="count*" propagation="SUPPORTS" rollback-for="Exception" />
<tx:method name="*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>

    <!-- 包扫描路径配置 -->
    <context:component-scan base-package="com.howbuy.otc" />

<!--实例定义AOP-->

      <aop:config>
        <aop:aspect id="handlerAspect" ref="handlerServiceAspect">
        <aop:around pointcut="execution(* com.howbuy.otc.invest.handler..*.*HandlerService.*(..))" method="doAround" />
        </aop:aspect>
      </aop:config>

 

至此spring的大致配置完成,至于定时任务,JMS等等,需要自己去配。

第三步:spring-mvc.xml  主要是(扫controller,视图解析器,上传文件配置,将reponsebody返回的java对象利用jasckson包转化为json字符串等等)

<?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"> <!-- 同时开启json格式的支持 -->
<mvc:annotation-driven /> <!-- spring mvc 扫描所有的controller 但是不扫描service -->
<context:component-scan base-package="com.controller">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Service" />
</context:component-scan> <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" /><!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑 -->
</bean> </beans>

spring-mvc.xml

第四步:因为我们没有用spring-boot,所用还需要配置,web-xml文件。

<!-- 加载(spring自身的,事务,mybatis)配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext*.xml</param-value>
</context-param> <!-- 防止spring内存溢出监听器 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener> <!--Spring的ApplicationContext 载入:Spring的监听器 -->
<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>
<load-on-startup>1</load-on-startup> </servlet> <servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping> <filter>
<description>字符集过滤器</description>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<description>字符集编码</description>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <!-- 配置session超时时间,单位分钟 -->
<session-config>
<session-timeout>60</session-timeout>
</session-config> <!-- DEFAULT PAGE -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

web.xml

至此--ssm简易demo达成完毕。

(最后补充一个完整的applicationContext.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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd"> <!-- 数据库连接池 :dataSource -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" ><!-- destroy-method="close" -->
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/play?useUnicode=true&amp;characterEncoding=UTF-8" />
<property name="user" value="root" />
<property name="password" value="root" />
<property name="minPoolSize" value="1" />
<property name="maxPoolSize" value="20" />
<property name="maxIdleTime" value="1800" />
<property name="acquireIncrement" value="2" />
<property name="maxStatements" value="0" />
<property name="initialPoolSize" value="2" />
<property name="idleConnectionTestPeriod" value="1800" />
<property name="acquireRetryAttempts" value="30" />
<property name="breakAfterAcquireFailure" value="true" />
<property name="testConnectionOnCheckout" value="false" />
</bean> <!-- 数据库配置SqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml" />
</bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.dao" />
</bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean> <!-- 如果存在一个事务,则支持当前事务。如果没有事务则开启 -->
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="transactionManager" />
</bean> <context:component-scan base-package="com.service.impl">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!-- 以AspectJ方式 定义 事务 -->
<aop:config proxy-target-class="true" >
<aop:advisor pointcut="execution(* com.service..*.*(..))" advice-ref="txAdvice" order="2"/>
</aop:config> <!-- 配置事务传播特性:如果存在一个事务,则支持当前事务。如果没有事务则开启:REQUIRED -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="update*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="save*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="exec*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="insert*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="merge*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="batch*" propagation="REQUIRED" rollback-for="Exception" /> <tx:method name="add*Atom" propagation="REQUIRES_NEW" rollback-for="Exception" />
<tx:method name="update*Atom" propagation="REQUIRES_NEW" rollback-for="Exception" />
<tx:method name="save*Atom" propagation="REQUIRES_NEW" rollback-for="Exception" />
<tx:method name="delete*Atom" propagation="REQUIRES_NEW" rollback-for="Exception" />
<tx:method name="*Atom" propagation="REQUIRES_NEW" rollback-for="Exception" /> <tx:method name="get*" propagation="SUPPORTS" rollback-for="Exception" />
<tx:method name="count*" propagation="SUPPORTS" rollback-for="Exception" />
<tx:method name="*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice> </beans>

applicationContext.xml

快速搭建一个SSM框架demo的更多相关文章

  1. springboot入门(一)--快速搭建一个springboot框架

    原文出处 前言在开始之前先简单介绍一下springboot,springboot作为一个微框架,它本身并不提供Spring框架的核心特性以及扩展功能,只是用于快速.敏捷地开发新一代基于Spring框架 ...

  2. 搭建一个ssm框架的maven项目需要配置的文件

    单独功能需要的配置文件: 1,mybatis配置文件      mybatis-config.xml2,spring配置文件        spring-context.xml  ......3,we ...

  3. 搭建一个SSM框架

    刚好学完springMVC,mybatis简单的组合,总结一下 1.创建一个maven项目,引入jar包 一共这么多,除了oracle jar包是我手动添加到lib里,其他都是通过pom.xml引入的 ...

  4. 快速搭建一个直播Demo

    缘由 最近帮朋友看一个直播网站的源码,发现这份直播源码借助 阿里云 .腾讯云这些大公司提供的SDK 可以非常方便的搭建一个直播网站.下面我们来给大家讲解下如何借助 腾讯云 我们搭建一个简易的 直播示例 ...

  5. jquery+flask+keras+nsfw快速搭建一个简易鉴黄工具

    1. demo 地址:http://www.huchengchun.com:8127/porn_classification 接口说明: 1. http://www.huchengchun.com:8 ...

  6. 快速搭建一个Quartz定时任务【转载,好文 ,值得收藏,亲身试用 效果不错】

    Quartz.NET 入门 概述 Quartz.NET是一个开源的作业调度框架,非常适合在平时的工作中,定时轮询数据库同步,定时邮件通知,定时处理数据等. Quartz.NET允许开发人员根据时间间隔 ...

  7. 快速搭建一个“微视”类短视频 App

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由腾讯云视频发表于云+社区专栏 关注公众号"腾讯云视频",一键获取 技术干货 | 优惠活动 | 视频方案 " ...

  8. 用Python手把手教你搭建一个web框架-flask微框架!

    在之前的文章当中,小编已经教过大家怎么搭建一个Django框架,今天我们来探索另外的一种框架的搭建,这个框架就是web框架-flask微框架啦!首先我们带着以下的几个问题来阅读本文: 1.flask是 ...

  9. 如何快速搭建一个 Node.JS 项目并进入开发?

    了解:如何快速搭建一个项目并进入开发? 在此不概述 Node.JS 的历史以及发展过程. 因为之前接触过通过 Java 开发语言,所以明确地知道一个服务器所需的文件,以及一个服务器所需要的操作. 那么 ...

随机推荐

  1. React.js 小书 Lesson26 - 实战分析:评论功能(五)

    作者:胡子大哈 原文链接:http://huziketang.com/books/react/lesson26 转载请注明出处,保留原文链接和作者信息. (本文未审核) 持久化评论 同样地,可以通过类 ...

  2. 45 Useful Oracle Queries--ref

    http://viralpatel.net/blogs/useful-oracle-queries/ Here’s a list of 40+ Useful Oracle queries that e ...

  3. bzoj 5372: [Pkusc2018]神仙的游戏

    Description 小D和小H是两位神仙.他们经常在一起玩神仙才会玩的一些游戏,比如"口算一个4位数是不是完全平方数". 今天他们发现了一种新的游戏:首先称s长度为len的前缀 ...

  4. 架构实战项目心得(十一):基于spring-security-oauth2的mysql数据表设计

    一.建立数据库及数据表结构 CREATE SCHEMA IF NOT EXISTS `oauth2` DEFAULT CHARACTER SET utf8 ; USE `oauth2` ; -- -- ...

  5. C# 多线程系列之Semaphore使用

    Semaphore,即信号量的意思.是操作系统原始提供的内核同步对象. Semaphore semaphoreAcceptedClients = , 3,"Semaphore1") ...

  6. mac os下载安装jmeter

    一.简介 jmeter是属于apache的一个开源产品,纯Java应用.最初用来进行功能测试,而后又扩展了更多的测试功能. 二.下载 进入apache的jmeter下载页:http://jmeter. ...

  7. SSM实现批量删除功能

    批量删除功能的实现 其实实现这个功能还是挺简单的 因为我这是直接拼接的,所以用了DOM方法来获取id话不多说直接上代码首先是复选框全选和反选这里的话 获取最上面一个复选框的状态同步到拼接的复选框  $ ...

  8. 搭建Jquery+SpringMVC+Spring+Hibernate+MySQL平台

    一. 开发环境 1. 点击此查看并下载需要的 Eclipse IDE for Java EE Developers 开发工具,依赖于java,推荐选用32位   2. 点击此查看并下载需要的 MySQ ...

  9. Excel删除空白行和列

    Sub DeleteEmptyRows() Dim LastRow As Long, r As Long LastRow = ActiveSheet.UsedRange.Rows.Count Last ...

  10. python中循环删除list和dict类型注意事项

    列表和字典在循环操作(增删)时,其长度会改变 # 删除 li = [11, 22, 33, 44, 'rock']中索引为单数的元素 # 方法一 del li[1::2] print(li) # [1 ...