我之所以写一个快速搭建的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', '11', '11', '无效', '10'); INSERT INTO `sys_com_code` VALUES ('status', '10', '10', '有效', '10'); -- ---------------------------- -- 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', '-1', '系统设置', 'system', 'system', '0', '1'); INSERT INTO `sys_resource` VALUES ('2', '-1', '管理首页', 'home', 'home', '0', '2'); INSERT INTO `sys_resource` VALUES ('3', '1', '用户授权', 'user', 'user', '1', '11'); INSERT INTO `sys_resource` VALUES ('4', '3', '角色管理', '/role/list.do', 'role:list.do', '1', '111'); INSERT INTO `sys_resource` VALUES ('5', '3', '用户管理', '/user/list.do', 'user:list.do', '0', '112'); INSERT INTO `sys_resource` VALUES ('6', '3', '资源管理', '/resource/list.do', 'resource:list.do', '0', '113'); INSERT INTO `sys_resource` VALUES ('7', '1', '知识案例', 'demo', 'demo', '1', '112'); INSERT INTO `sys_resource` VALUES ('8', '7', 'freemarker案例', '/freemarker/list.do', 'freemarker:list.do', '1', '1123'); -- ---------------------------- -- 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 ('1', '超级管理员', '1'); INSERT INTO `sys_role` VALUES ('2', '普通管理员', '1'); INSERT INTO `sys_role` VALUES ('3', '普通用户', '1'); -- ---------------------------- -- 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 ('8', '2', '-1'); INSERT INTO `sys_role_resource` VALUES ('9', '2', '1'); INSERT INTO `sys_role_resource` VALUES ('10', '2', '3'); INSERT INTO `sys_role_resource` VALUES ('11', '2', '4'); INSERT INTO `sys_role_resource` VALUES ('12', '2', '5'); INSERT INTO `sys_role_resource` VALUES ('13', '2', '2'); INSERT INTO `sys_role_resource` VALUES ('14', '3', '-1'); INSERT INTO `sys_role_resource` VALUES ('15', '3', '1'); INSERT INTO `sys_role_resource` VALUES ('16', '3', '3'); INSERT INTO `sys_role_resource` VALUES ('17', '3', '4'); INSERT INTO `sys_role_resource` VALUES ('18', '3', '5'); INSERT INTO `sys_role_resource` VALUES ('19', '4', '-1'); INSERT INTO `sys_role_resource` VALUES ('20', '4', '1'); INSERT INTO `sys_role_resource` VALUES ('21', '4', '3'); INSERT INTO `sys_role_resource` VALUES ('22', '4', '4'); INSERT INTO `sys_role_resource` VALUES ('23', '4', '2'); INSERT INTO `sys_role_resource` VALUES ('47', '1', '-1'); INSERT INTO `sys_role_resource` VALUES ('48', '1', '1'); INSERT INTO `sys_role_resource` VALUES ('49', '1', '3'); INSERT INTO `sys_role_resource` VALUES ('50', '1', '4'); INSERT INTO `sys_role_resource` VALUES ('51', '1', '5'); INSERT INTO `sys_role_resource` VALUES ('52', '1', '6'); INSERT INTO `sys_role_resource` VALUES ('53', '1', '7'); INSERT INTO `sys_role_resource` VALUES ('54', '1', '8'); INSERT INTO `sys_role_resource` VALUES ('55', '1', '2'); -- ---------------------------- -- 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 ('8', 'admin', '53d8c7cee066fec7840bcbbfb1733192', '12'); INSERT INTO `sys_user` VALUES ('9', 'superadmin', '53d8c7cee066fec7840bcbbfb1733192', '12'); INSERT INTO `sys_user` VALUES ('10', 'loafer', '53d8c7cee066fec7840bcbbfb1733192', '12'); -- ---------------------------- -- 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 ('2', '2', '8'); INSERT INTO `sys_user_role` VALUES ('3', '1', '9'); INSERT INTO `sys_user_role` VALUES ('4', '3', '10');
预先准备:用插件自动生成自动生成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>
第二步:
创建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>
第二个配置文件 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&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>
<?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>
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>
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-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>
至此--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&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>