Ssm
新建maven项目,选择使用 maven-webapp模板创建
创建完成后,右键=》build path=》config build path=》修改jdk
修改web.xml的头,改成3.0的版本,默认2.3不自动支持el表达式:
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
1.2 引入依赖
javax.servlet
jstl
1.2
javax.servlet
javax.servlet-api
4.0.1
provided
javax.servlet.jsp
jsp-api
2.2
provided
1.3 把spring集成到web项目中来
//添加spring的依赖:
org.springframework
spring-context
5.0.9.RELEASE
org.springframework
spring-aspects
5.0.9.RELEASE
把spring引入到web项目中来,在web.xml中添加linstener
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:spring.xml
在resources目录下创建spring配置文件:spring.xml,填写基本信息:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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">
在spring.xml中配置包扫描设置
1.4 引入log4j
添加依赖
org.slf4j
slf4j-log4j12
1.7.25
给项目添加log4j配置文件,在resources目录下添加log4j.properties
配置日志输出:
log4j.rootLogger=DEBUG, Console
#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
log4j.logger.org.apache.ibatis=INFO
2. 集成springmvc
2.1 添加springmvc依赖
org.springframework
spring-webmvc
5.0.9.RELEASE
通过dispatchServlet把springmvc引入到web项目中,在web.xml中添加:
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc.xml
1
springmvc
/
在resources目录下创建springmvc的配置文件springmvc.xml:
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"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
在项目下面创建目录:
添加springmvc的扫描包,只扫描controller
在springmvc.xml中配置视图解析器:
2.2 添加springmvc的json支持
添加依赖:
com.fasterxml.jackson.core
jackson-core
2.9.6
com.fasterxml.jackson.core
jackson-databind
2.9.6
com.fasterxml.jackson.core
jackson-annotations
2.9.6
在springmvc.xml中配置json解析器
text/html;charset=UTF-8
2.3 Springmvc文件上传支持
添加依赖
commons-fileupload
commons-fileupload
1.3.3
添加文件上传试图解析器:
2.4 让springmvc开发js/css/image等静态资源访问控制
在webapp下新建目录:
开放css image js目录静态资源访问的控制:
2.5 添加统一字符编码集设置
在web.xml中配置springmvc提供的filter设置,并配置参数UTF-8
org.springframework.web.filter.CharacterEncodingFilter
characterEncodingFilter
encoding
UTF-8
characterEncodingFilter
/*
3. 集成mybatis
3.1 引入mybatis和数据库驱动的依赖
mysql
mysql-connector-java
5.1.39
org.mybatis
mybatis
3.4.6
org.springframework
spring-tx
5.0.9.RELEASE
org.mybatis
mybatis-spring
1.3.2
在resources目录下新建mybatis的配置文件jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=sa
和mybatis.xml
configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
3.2 配置mybatis和spring集成的部分
3.2.1在spring中配置数据源
本例选用阿里的druid作为数据源
添加依赖:
com.alibaba
druid
1.1.11
org.springframework
spring-jdbc
5.0.9.RELEASE
通过把druid的类型交给spring容器管理从而把druid引入到项目中。在spring.xml中:
3.2.2 把Mybatis的SqlSessionFactory交给spring容器管理
删除mybatis.xml中关于数据源配置的context标签,和关于mapper文件的配置
添加mybatis dao的扫描:
3.2.3 使用spring管理mybatis的事务,把事务管理在service层进行拦截
在spring.xml中支持事务tx标签和aop标签,把xml的头改成:
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
然后添加配置:
3.3 启动druid监控
在web.xml中添加监控界面的访问servlet配置:
DruidStatView
com.alibaba.druid.support.http.StatViewServlet
profileEnable
true
resetEnable
false
loginUsername
druid
loginPassword
druid
DruidStatView
/druid/*
4. 集成mybatis-generator
org.mybatis.generator
mybatis-generator-maven-plugin
1.3.2
mysql
mysql-connector-java
5.1.39
${basedir}/src/main/resources/generatorConfig.xml
true
true
创建自动生成代码的配置文件:/src/main/resources/generatorConfig.xml
generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
connectionURL="${jdbc.url}"
userId="${jdbc.username}"
password="${jdbc.password}">
启动maven指令运行生成代码:
mybatis-generator:generate
5. 集成mybatis pagehelper
添加依赖:
com.github.pagehelper
pagehelper
5.1.6
配置插件到mybatis.xml中
6. 集成tk.mybatis
项目中引入依赖:
tk.mybatis
mapper
3.4.0
在spring.xml中引入
自动生成代码插件配置
org.mybatis.generator
mybatis-generator-maven-plugin
1.3.2
mysql
mysql-connector-java
5.1.39
tk.mybatis
mapper
3.4.0
${basedir}/src/main/resources/generatorConfig.xml
true
true
在自动生成代码配置文件generatorConfig.xml中配置:
首先在jdbc.properties中添加配置:
mapper.plugin=tk.mybatis.mapper.generator.MapperPlugin
mapper.Mapper=tk.mybatis.mapper.common.Mapper
然后再在generatorConfig.xml中配置:
connectionURL="${jdbc.url}"
userId="${jdbc.username}"
password="${jdbc.password}">
7. 集成EHCache
引入依赖
net.sf.ehcache
ehcache
2.10.5
org.springframework
spring-context-support
5.0.9.RELEASE
org.mybatis.caches
mybatis-ehcache
1.1.0
在resources目录下添加ehcache配置文件ehcache.xml
maxElementsInMemory="10000"
eternal="false"
overflowToDisk="true"
timeToIdleSeconds="10"
timeToLiveSeconds="20"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"/>
maxElementsInMemory="1000"
eternal="false"
overflowToDisk="true"
timeToIdleSeconds="10"
timeToLiveSeconds="20"/>
添加spring的cache支持文件:spring-ehcache.xml
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:cache="http://www.springframework.org/schema/cache"
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/cache
http://www.springframework.org/schema/cache/spring-cache.xsd">
把spring-ehcache.xml引入(import)到spring.xml中
散文欣赏
没有中意的散文附上一篇礼拜的原别离
远别离,古有皇英之二女,乃在洞庭之南,潇湘之浦。
海水直下万里深,谁人不言此离苦?
日惨惨兮云冥冥,猩猩啼烟兮鬼啸雨。
我纵言之将何补?
皇穹窃恐不照余之忠诚,雷凭凭兮欲吼怒。
尧舜当之亦禅禹。
君失臣兮龙为鱼,权归臣兮鼠变虎。
或云:尧幽囚,舜野死。
九疑联绵皆相似,重瞳孤坟竟何是?
帝子泣兮绿云间,随风波兮去无还。
恸哭兮远望,见苍梧之深山。
苍梧山崩湘水绝,竹上之泪乃可灭。