我之所以写一个快速搭建的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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>MyGroup</groupId> <artifactId>MyArtifact</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>MyArtifact Maven Webapp</name> <url>http://maven.apache.org</url> <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>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.0.1</version>
</dependency>
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.21</version> </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>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> <type>jar</type> </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> </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>
<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>
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>