第一篇:基础
1、涉及到的链接
- maven中央仓库:http://mvnrepository.com/
- Druid源码:https://github.com/alibaba/druid
- bootstrap3:http://www.bootcss.com/
2、环境
3、开始搭建
3.1、新建maven项目
1.png2.png
3.png
4.png
5.png
6.png ctrl+shift+alt+s打开设置 7.png 8.png 9.png 10.png
<properties>
<!-- spring版本号 -->
<spring.version>4.3.7.RELEASE</spring.version>
<!-- mybatis版本号 -->
<mybatis.version>3.4.2</mybatis.version>
<!-- log4j日志文件管理包版本 -->
<slf4j.version>1.7.25</slf4j.version>
<aspectjweaver.version>1.8.10</aspectjweaver.version>
<fileupload.version>1.3.2</fileupload.version>
<!--数据库-->
<mysql.version>6.0.6</mysql.version>
<!--阿里连接池-->
<druid.version>1.1.5</druid.version>
<!-- gson -->
<gson.version>2.8.0</gson.version>
<!--fastjson-->
<fastjson.version>1.2.32</fastjson.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<!-- 导入java ee jar 包 -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<!-- JSTL标签类 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- mybatis/spring包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!-- mybatis核心包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!-- aspectjweaver 依赖 begin -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectjweaver.version}</version>
</dependency>
<!-- aspectjweaver 依赖 end -->
<!-- spring依赖 begin-->
<!-- spring核心依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring ioc依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring aop依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring 扩展依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!--spring dao层依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring web相关依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring test相关依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<!-- spring依赖 end-->
<!-- log日志依赖 begin-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!--log日志依赖 end-->
<!-- fileupload 文件上传 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>${fileupload.version}</version>
</dependency>
<!--数据库依赖-->
<!-- 导入Mysql数据库链接jar包 start-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!-- 导入Mysql数据库链接jar包 end-->
<!-- gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20160810</version>
</dependency>
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<!-- JSTL标签类 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- POI 相关依赖 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.16</version>
</dependency>
<!--二维码-->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.0</version>
</dependency>
<!-- 其他依赖 begin -->
<!--dom4j-->
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.8.RELEASE</version>
</dependency>
<!--shiro安全框架-->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-web</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.4.0</version>
</dependency>
<!--mybatis自动生成-->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator</artifactId>
<version>1.3.5</version>
<type>pom</type>
</dependency>
<!--mybatis分页-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
<!--logback-->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.logback-extensions</groupId>
<artifactId>logback-ext-spring</artifactId>
<version>0.1.4</version>
</dependency>
<!--定时-->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.java-websocket</groupId>
<artifactId>Java-WebSocket</artifactId>
<version>1.3.6</version>
</dependency>
<!--验证码-->
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>
</dependencies>
<build>
<finalName>oldmountain</finalName>
<plugins>
<!--指定jdk版本-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!--mybatis反向生成Mapper.xml文件,dao,bean,这个以后会另开教程-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
</dependencies>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
</build>
3.2、配置文件
- 1、阿里连接池配置 jdbc.properties
druid.driverClassName=com.mysql.jdbc.Driver
druid.url=jdbc:mysql://***.***.***.***/***?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
druid.username=***
druid.password=***
#初始化时建立物理连接的个数。初始化发生在显示调用init方法,或者第一次getConnection时
druid.initialSize=0
#最大连接池数量
druid.maxActive=20
#已经不再使用,配置了也没效果
druid.maxIdle=20
#定义最小空闲,最小连接池数量
druid.minIdle=1
#定义最长等待时间,获取连接时最大等待时间,单位毫秒。配置了maxWait之后,缺省启用公平锁,并发效率会有所下降,如果需要可以通过配置useUnfairLock属性为true使用非公平锁
druid.maxWait=60000
#属性类型是字符串,通过别名的方式配置扩展插件,常用的插件有:
#监控统计用的filter:stat日志用的filter:log4j防御sql注入的filter:wall
druid.filters=stat
#有两个含义:
#1) Destroy线程会检测连接的间隔时间2) testWhileIdle的判断依据,详细看testWhileIdle属性的说明
druid.timeBetweenEvictionRunsMillis=60000
druid.minEvictableIdleTimeMillis=300000
#用来检测连接是否有效的sql,要求是一个查询语句。
#如果validationQuery为null,testOnBorrow、testOnReturn、testWhileIdle都不会其作用
druid.validationQuery=SELECT 'x'
#建议配置为true,不影响性能,并且保证安全性。申请连接的时候检测,
#如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。
druid.testWhileIdle=true
#申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能
druid.testOnBorrow=false
#归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能
druid.testOnReturn=false
#要启用PSCache,必须配置大于0,当大于0时,poolPreparedStatements自动触发修改为true。
#在Druid中,不会存在Oracle下PSCache占用内存过多的问题,可以把这个数值配置大一些,比如说100
druid.maxOpenPreparedStatements=100
druid.removeAbandoned=true
druid.removeAbandonedTimeout=1800
druid.logAbandoned=true
- 2、配置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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">
<!-- 启用注解 -->
<context:annotation-config/>
<!-- 自动扫描 -->
<!-- 启动组件扫描,排除@Controller组件,该组件由SpringMVC配置文件扫描 -->
<!--@Controller组件 在spring-mvc中配置扫描-->
<context:component-scan base-package="com.demo.oldmountain">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 事务相关控制 -->
<bean id="transactionManager" name="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 引入配置文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"/>
</bean>
<!-- 配置DataSource数据源 -->
<!-- 阿里 druid 数据库连接池 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<!-- 数据库基本信息配置 -->
<property name="url" value="${druid.url}"/>
<property name="username" value="${druid.username}"/>
<property name="password" value="${druid.password}"/>
<property name="driverClassName" value="${druid.driverClassName}"/>
<property name="filters" value="${druid.filters}"/>
<!-- 最大并发连接数 -->
<property name="maxActive" value="${druid.maxActive}"/>
<!-- 初始化连接数量 -->
<property name="initialSize" value="${druid.initialSize}"/>
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="${druid.maxWait}"/>
<!-- 最小空闲连接数 -->
<property name="minIdle" value="${druid.minIdle}"/>
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="${druid.timeBetweenEvictionRunsMillis}"/>
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="${druid.minEvictableIdleTimeMillis}"/>
<property name="validationQuery" value="${druid.validationQuery}"/>
<property name="testWhileIdle" value="${druid.testWhileIdle}"/>
<property name="testOnBorrow" value="${druid.testOnBorrow}"/>
<property name="testOnReturn" value="${druid.testOnReturn}"/>
<property name="maxOpenPreparedStatements" value="${druid.maxOpenPreparedStatements}"/>
<!-- 打开 removeAbandoned 功能 -->
<property name="removeAbandoned" value="${druid.removeAbandoned}"/>
<!-- 1800 秒,也就是 30 分钟 -->
<property name="removeAbandonedTimeout" value="${druid.removeAbandonedTimeout}"/>
<!-- 关闭 abanded 连接时输出错误日志 -->
<property name="logAbandoned" value="${druid.logAbandoned}"/>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="delete*" propagation="REQUIRED" read-only="false"
rollback-for="java.lang.Exception"/>
<tx:method name="insert*" propagation="REQUIRED" read-only="false"
rollback-for="java.lang.Exception"/>
<tx:method name="update*" propagation="REQUIRED" read-only="false"
rollback-for="java.lang.Exception"/>
<tx:method name="save*" propagation="REQUIRED" read-only="false"
rollback-for="java.lang.Exception"/>
</tx:attributes>
</tx:advice>
<aop:aspectj-autoproxy proxy-target-class="true"/>
<!-- 事物处理 -->
<aop:config>
<aop:pointcut id="pc" expression="execution(* com.demo.oldmountain.service..*(..))"/>
<aop:advisor pointcut-ref="pc" advice-ref="txAdvice"/>
</aop:config>
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>-->
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
<property name="typeAliasesPackage" value="com.demo.oldmountain.entity"/>
<!--分页pagehelper-->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<!-- 这里的几个配置主要演示如何使用,如果不理解,一定要去掉下面的配置 -->
<property name="properties">
<value>
helperDialect=mysql
<!--当reasonable设置为 true 时,pageNum<=0 时会查询第一页, pageNum>pages(超过总数时),会查询最后一页-->
reasonable=true
<!--支持通过 Mapper 接口参数来传递分页参数-->
supportMethodsArguments=true
<!--params=count=countSql-->
<!--允许在运行时根据多数据源自动识别对应方言的分页-->
autoRuntimeDialect=true
</value>
</property>
</bean>
</array>
</property>
</bean>
<!-- 通过扫描的模式,扫描在com.demo.oldmountain.dao.mapper目录下的所有mapper -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.demo.oldmountain.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<!-- 创建一个sqlSession实例,线程安全的,可以在所有DAO实例共享,原理是将sqlSession,事务与当前线程挂钩 -->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
<!-- ================ Shiro start 本教程不涉及,略 ================ -->
<!--<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">-->
<!--<property name="realm" ref="ShiroRealm"/>-->
<!--</bean>-->
<!-- 項目自定义的Realm -->
<!--<bean id="ShiroRealm" class="com.demo.oldmountain.interceptor.shiro.ShiroRealm"></bean>-->
<!-- Shiro Filter -->
<!--<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">-->
<!--<property name="securityManager" ref="securityManager"/>-->
<!--<property name="loginUrl" value="/"/>-->
<!--<property name="successUrl" value="/toIndex"/>-->
<!--<property name="unauthorizedUrl" value="/login"/>-->
<!--<property name="filterChainDefinitions">-->
<!--<value>-->
<!--/system/** = anon-->
<!--/static/** = anon-->
<!--/picture/* = anon-->
<!--/uploadFiles/* = anon-->
<!--</value>-->
<!--</property>-->
<!--</bean>-->
<!-- ================ Shiro end ================ -->
<!--验证码配置,比如登录时会要求输入4位随机验证码,下面配置是生成验证码图片,本教程不涉及,可不配置-->
<bean id="captchaProducer" class="com.google.code.kaptcha.impl.DefaultKaptcha">
<property name="config">
<bean class="com.google.code.kaptcha.util.Config">
<constructor-arg>
<props>
<!--Border around kaptcha. Legal values are yes or no. -->
<prop key="kaptcha.border">no</prop>
<!--Color of the border. Legal values are r,g,b (and optional alpha) or white,black,blue. -->
<prop key="kaptcha.border.color">green</prop>
<!--Thickness of the border around kaptcha. Legal values are > 0. -->
<prop key="kaptcha.border.thickness">1</prop>
<!--Width in pixels of the kaptcha image. -->
<prop key="kaptcha.image.width">100</prop>
<!--Height in pixels of the kaptcha image. -->
<prop key="kaptcha.image.height">45</prop>
<!--The image producer. -->
<prop key="kaptcha.producer.impl">com.google.code.kaptcha.impl.DefaultKaptcha</prop>
<!--The text producer. -->
<prop key="kaptcha.textproducer.impl">com.google.code.kaptcha.text.impl.DefaultTextCreator
</prop>
<prop key="kaptcha.obscurificator.impl">com.google.code.kaptcha.impl.ShadowGimpy</prop>
<!--The background implementation. -->
<prop key="kaptcha.background.impl">com.google.code.kaptcha.impl.DefaultBackground</prop>
<!--The word renderer implementation. -->
<prop key="kaptcha.word.impl">com.google.code.kaptcha.text.impl.DefaultWordRenderer</prop>
<!--The value for the kaptcha is generated and is put into the HttpSession. This is the key value for that item in the session. -->
<prop key="kaptcha.session.key">KAPTCHA_SESSION_KEY</prop>
<!--The date the kaptcha is generated is put into the HttpSession. This is the key value for that item in the session. -->
<prop key="kaptcha.session.date">KAPTCHA_SESSION_DATE</prop>
<!-- 验证码背景颜色渐变,开始颜色 -->
<prop key="kaptcha.background.clear.from">248,248,248</prop>
<!-- 验证码背景颜色渐变,结束颜色 -->
<prop key="kaptcha.background.clear.to">248,248,248</prop>
<!-- 验证码的字符 -->
<prop key="kaptcha.textproducer.char.string">0123456789abcdefghijklmnopqrstuvwxyz</prop>
<!-- 验证码字体颜色 -->
<prop key="kaptcha.textproducer.font.color">0,0,255</prop>
<!-- 验证码的效果,水纹 -->
<prop key="kaptcha.obscurificator.impl">com.google.code.kaptcha.impl.WaterRipple</prop>
<!-- 验证码字体大小 -->
<prop key="kaptcha.textproducer.font.size">35</prop>
<!-- 验证码字数 -->
<prop key="kaptcha.textproducer.char.length">4</prop>
<!-- 验证码文字间距 -->
<prop key="kaptcha.textproducer.char.space">2</prop>
<!-- 验证码字体 -->
<prop key="kaptcha.textproducer.font.names">new Font("Arial", 1, fontSize), new Font("Courier",
1, fontSize)
</prop>
<!-- 不加噪声 -->
<prop key="kaptcha.noise.impl">com.google.code.kaptcha.impl.DefaultNoise</prop>
<!--The noise color. Legal values are r,g,b. -->
<prop key="kaptcha.noise.color">black</prop>
</props>
</constructor-arg>
</bean>
</property>
</bean>
</beans>
- 3、配置spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
<context:component-scan base-package="com.demo.oldmountain.controller"/>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!-- 对静态资源的访问 -->
<mvc:resources mapping="/static/**" location="/static/"/>
<mvc:resources mapping="/plugins/**" location="/plugins/"/>
<mvc:resources mapping="/pages/**" location="/pages/"/>
<!-- 访问拦截 -->
<!--登录拦截,验证用户是否登录,本教程不涉及,略-->
<!--<mvc:interceptors>-->
<!--<mvc:interceptor>-->
<!--<mvc:mapping path="/**/**"/>-->
<!--<bean class="com.demo.oldmountain.interceptor.LoginHandlerInterceptor"/>-->
<!--</mvc:interceptor>-->
<!--</mvc:interceptors>-->
<!-- pjax视图拦截 ,本教程不涉及,略 -->
<!--<mvc:interceptors>-->
<!--<mvc:interceptor>-->
<!--<mvc:mapping path="/**/**.view"/>-->
<!--<mvc:mapping path="/**/**.modal"/>-->
<!--<bean class="com.demo.oldmountain.interceptor.PjaxHandlerInterceptor"/>-->
<!--</mvc:interceptor>-->
<!--</mvc:interceptors>-->
<!-- 国际化资源配置,本教程不涉及,略 -->
<!--<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">-->
<!--<!– 国际化信息所在的文件名 –>-->
<!--<property name="basename" value="messages/message"/>-->
<!--<!– 如果在国际化资源文件中找不到对应代码的信息,就用这个代码作为名称 –>-->
<!--<property name="useCodeAsDefaultMessage" value="true" />-->
<!--</bean>-->
<!-- 定义跳转的文件的前后缀 ,视图模式配置 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--配置SessionLocaleResolver-->
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"></bean>
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>
<!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->
<!--本教程不涉及,略-->
<!--如果自定义监听上传,请修改class中CommonsMultipartResolver为继承的那个类-->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 默认编码 -->
<property name="defaultEncoding" value="UTF-8"/>
<!-- 文件大小最大值 -->
<property name="maxUploadSize" value="10485760000"/>
<!-- 内存中的最大值 -->
<property name="maxInMemorySize" value="40960"/>
</bean>
</beans>
- 3、配置logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- For assistance related to logback-translator or configuration -->
<!-- files in general, please contact the logback user mailing list -->
<!-- at http://www.qos.ch/mailman/listinfo/logback-user -->
<!-- -->
<!-- For professional support please see -->
<!-- http://www.qos.ch/shop/products/professionalSupport -->
<!-- -->
<configuration debug="true" scan="true" scanPeriod="60 seconds" packagingData="true">
<!--控制台输出 -->
<!--级别:TRACE < DEBUG < INFO < WARN < ERROR-->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<!--<Encoding>UTF-8</Encoding>-->
<!--<Target>System.out</Target>-->
<encoding>UFT-8</encoding>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
<!--<filter class="ch.qos.logback.classic.filter.ThresholdFilter">-->
<!--<level>error</level>-->
<!--</filter>-->
</appender>
<appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!--See http://logback.qos.ch/manual/appenders.html#RollingFileAppender-->
<!--and http://logback.qos.ch/manual/appenders.html#TimeBasedRollingPolicy-->
<!--for further documentation-->
<file>${catalina.home}/logs/oldmountain_log/PurePro_</file>
<encoder>
<pattern>[oldmountain_sys] %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${catalina.home}/logs/oldmountain_log/PurePro_.%d{yyyy-MM-dd'.log'}</fileNamePattern>
</rollingPolicy>
</appender>
<logger name="com.demo.oldmountain.dao" level="DEBUG" />
<logger name="org.mybatis">
<level value="TRACE"/>
</logger>
<logger name="logback"/>
<!--打印SQL语句-->
<logger name="jdbc.sqltiming" level="TRACE"/>
<logger name="com.ibatis" level="TRACE"/>
<logger name="com.ibatis.common.jdbc.SimpleDataSource" level="TRACE"/>
<logger name="com.ibatis.common.jdbc.ScriptRunner" level="TRACE"/>
<logger name="com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate"
level="TRACE"/>
<logger name="java.sql.Connection" level="TRACE"/>
<logger name="java.sql.Statement" level="TRACE"/>
<logger name="java.sql.PreparedStatement" level="TRACE"/>
<logger name="java.sql.ResultSet" level="TRACE"/>
<root level="info">
<level value="info"/>
<appender-ref ref="file"/>
<appender-ref ref="console"/>
</root>
</configuration>
- 3、配置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">
<display-name>Archetype Created Web Application</display-name>
<!-- Spring和mybatis的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-mybatis.xml</param-value>
</context-param>
<!--logback-->
<context-param>
<param-name>logbackConfigLocation</param-name>
<param-value>classpath:logback.xml</param-value>
</context-param>
<!-- 编码过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<async-supported>true</async-supported>
<init-param>
<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>
<!--shiro安全框架,本教程不涉及,略-->
<!--<filter>-->
<!--<filter-name>shiroFilter</filter-name>-->
<!--<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>-->
<!--<async-supported>true</async-supported>-->
<!--<init-param>-->
<!--<param-name>targetFilterLifecycle</param-name>-->
<!--<param-value>true</param-value>-->
<!--</init-param>-->
<!--</filter>-->
<!--<filter-mapping>-->
<!--<filter-name>shiroFilter</filter-name>-->
<!--<url-pattern>/*</url-pattern>-->
<!--</filter-mapping>-->
<!-- Spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 防止Spring内存溢出监听器 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<!--logback监听-->
<listener>
<listener-class>ch.qos.logback.ext.spring.web.LogbackConfigListener</listener-class>
</listener>
<!-- Spring MVC servlet -->
<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/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<!-- 此处可以可以配置成*.do,对应struts的后缀习惯 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<error-page>
<error-code>404</error-code>
<location>/pages/common/404.html</location>
</error-page>
<!--<error-page>-->
<!--<error-code>500</error-code>-->
<!--<location>/page/error404</location>-->
<!--</error-page>-->
<!--此处可以不配置,如果一个tomcat发布多个项目,可能需要配置这个-->
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>mos_ms.root</param-value>
</context-param>
</web-app>
4、配置tomcat发布项目
16.png 18.png 17.png 18.png19.png 20.png 21.png 22.png
4、开始搭建、配置tomcat发布项目
发布成功
23.png 24.png注意!!!在没有写mapper的情况下,请注释掉spring-mybatis.xml
中对mapper的配置
未完待续,持续更新
网友评论