加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ssm搭建流程 超级详细

发布时间:2020-12-16 23:41:13 所属栏目:百科 来源:网络整理
导读:Ssm ? ? 新建 maven 项目,选择使用 maven- webapp 模板创建 ? ? ? 创建完成后,右键 =》 build path=》config build path=》 修改 jdk ? 修改 web.xml 的头,改成 3.0 的版本,默认 2.3 不自动支持 el 表达式: ?xml?version= "1.0" ?encoding= "UTF-8" ? w

Ssm?

?

新建maven项目,选择使用 maven-webapp模板创建

?

?

?

创建完成后,右键=》build path=》config build path=》修改jdk

?

修改web.xml的头,改成3.0的版本,默认2.3不自动支持el表达式:

<?xml?version="1.0"?encoding="UTF-8"?>

<web-app?xmlns="http://java.sun.com/xml/ns/javaee"?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

?????????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?引入依赖

???<!-- jstl的依赖包 -->

????<dependency>

????<groupId>javax.servlet</groupId>

????<artifactId>jstl</artifactId>

????<version>1.2</version>

</dependency>

????<!-- servlet项目底层api依赖,一般这些依赖web服务器都具有,但是eclipse编译环境没有,所以这里scope=provided -->

????<dependency>

????<groupId>javax.servlet</groupId>

????<artifactId>javax.servlet-api</artifactId>

????<version>4.0.1</version>

????<scope>provided</scope>

</dependency>

<dependency>

???????<groupId>javax.servlet.jsp</groupId>

???????<artifactId>jsp-api</artifactId>

???????<version>2.2</version>

???????<scope>provided</scope>

?????</dependency>

1.3?spring集成到web项目中来

//添加spring的依赖:

<dependency>

?????????<groupId>org.springframework</groupId>

?????????<artifactId>spring-context</artifactId>

?????????<version>5.0.9.RELEASE</version>

</dependency>

<dependency>

???? ?????????<groupId>org.springframework</groupId>

????????<artifactId>spring-aspects</artifactId>

????????<version>5.0.9.RELEASE</version>

</dependency>

spring引入到web项目中来,在web.xml中添加linstener

<!-- 用listener的方式引入spring容器 -->

??<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

??</listener>

??<!-- 配置spring的配置文件位置 -->

??<context-param>

?? <param-name>contextConfigLocation</param-name>

?? <param-value>classpath:spring.xml</param-value>

</context-param>

?

resources目录下创建spring配置文件:spring.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"

????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">

</beans>

?

spring.xml中配置包扫描设置

?

????<!--在beans里面?启动root WebApplicationContext的包扫描,dao层和service层的包应该再次被扫描 -->

????<context:component-scan?base-package="com.zhiyou.bd28"></context:component-scan>

1.4?引入log4j

???添加依赖

??? <!-- 引入log4j依赖 -->

????<dependency>

????<groupId>org.slf4j</groupId>

????<artifactId>slf4j-log4j12</artifactId>

????<version>1.7.25</version>

</dependency>

??给项目添加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依赖

<!-- 引入springmvc基础依赖 -->

<dependency>

????<groupId>org.springframework</groupId>

????<artifactId>spring-webmvc</artifactId>

????<version>5.0.9.RELEASE</version>

</dependency>

通过dispatchServletspringmvc引入到web项目中,在web.xml中添加

??<!-- 配置springmvc里的前端控制器DispatcherServlet -->

??<servlet>

?? <servlet-name>springmvc</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

?? <!-- 给DispatcherServlet设置初始化参数 -->

?? <init-param>

?? <param-name>contextConfigLocation</param-name>

?? <param-value>classpath:springmvc.xml</param-value>

?? </init-param>

?? <!-- springmvc的容器和servlet项目一起启动 -->

?? <load-on-startup>1</load-on-startup>

??</servlet>

??<servlet-mapping>

?? <servlet-name>springmvc</servlet-name>

?? <url-pattern>/</url-pattern>??<!-- 映射所有的用户请求,都交由DispatcherServlet处理 -->

??</servlet-mapping>

?

resources目录下创建springmvc的配置文件springmvc.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"

????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">

</beans>

?

在项目下面创建目录:

?

添加springmvc的扫描包,只扫描controller

???<!-- 启动servlet?WebApplicationContext的包扫描,应该只有controller、viewresolver等注解 -->

????<context:component-scan?base-package="com.zhiyou.bd28.controller"></context:component-scan>

?

springmvc.xml中配置视图解析器:

????<!-- 视图模板的参数配置 -->

????<bean?class="org.springframework.web.servlet.view.InternalResourceViewResolver">

???? <property?name="viewClass"?value="org.springframework.web.servlet.view.JstlView"/>

???? <property?name="prefix"?value="/WEB-INF/jsp/"></property>

???? <property?name="suffix"?value=".jsp"></property>

</bean>

?

2.2?添加springmvcjson支持

添加依赖:

<!-- springmvc的转换支持json?-->

<dependency>

????<groupId>com.fasterxml.jackson.core</groupId>

????<artifactId>jackson-core</artifactId>

????<version>2.9.6</version>

</dependency>

<dependency>

????<groupId>com.fasterxml.jackson.core</groupId>

????<artifactId>jackson-databind</artifactId>

????<version>2.9.6</version>

</dependency>

<dependency>

????<groupId>com.fasterxml.jackson.core</groupId>

????<artifactId>jackson-annotations</artifactId>

????<version>2.9.6</version>

</dependency>

springmvc.xml中配置json解析器

<!-- 如果使用mvc标签需要改句配置,否则@Controller的类会加载失败 -->

????<mvc:annotation-driven>

????<mvc:message-converters>

????????<bean?class="org.springframework.http.converter.StringHttpMessageConverter"/>

????????<bean?class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">

???????? <property?name="supportedMediaTypes">

????????????<list>

????????????????<value>text/html;charset=UTF-8</value>

????????????</list>

????????</property>

????????</bean>

????</mvc:message-converters>

</mvc:annotation-driven>

2.3?Springmvc文件上传支持

添加依赖

<!-- springmvc支持文件上传的依赖 -->

<dependency>

????<groupId>commons-fileupload</groupId>

????<artifactId>commons-fileupload</artifactId>

????<version>1.3.3</version>

</dependency>

添加文件上传试图解析器:

????<!-- 支持文件上传视图解析器 -->

????<bean?id="multipartResolver"?class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

????????<!--允许上传的文件最大大小 ?单位是byte-->

????????<property?name="maxUploadSize"?value="60000000"/>

</bean>

2.4?springmvc开发js/css/image等静态资源访问控制

webapp下新建目录:

?

开放css image js目录静态资源访问的控制:

???<!-- 静态资源拦截配置 : -->

????<mvc:resources?mapping="/js/**"?location="/js/"?cache-period="31556926"/>

????<mvc:resources?mapping="/css/**"?location="/js/"?cache-period="31556926"/>

<mvc:resources?mapping="/image/**"?location="/js/"?cache-period="31556926"/>

2.5?添加统一字符编码集设置

web.xml中配置springmvc提供的filter设置,并配置参数UTF-8

??<!-- 配置spring web统一字符集编码设置 -->

??<filter>

<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

?? <filter-name>characterEncodingFilter</filter-name>

?? <init-param>

?? <param-name>encoding</param-name>

?? <param-value>UTF-8</param-value>

?? </init-param>

??</filter>

??<filter-mapping>

?? <filter-name>characterEncodingFilter</filter-name>

?? <url-pattern>/*</url-pattern>

??</filter-mapping>

3.?集成mybatis

3.1?引入mybatis和数据库驱动的依赖

<!-- 引入数据库驱动依赖 -->

<dependency>

????<groupId>mysql</groupId>

????<artifactId>mysql-connector-java</artifactId>

????<version>5.1.39</version>

</dependency>

<!-- 引入mybatis依赖 -->

????<dependency>

????<groupId>org.mybatis</groupId>

????<artifactId>mybatis</artifactId>

????<version>3.4.6</version>

</dependency>

<!-- spring统一事务管理依赖 -->

<dependency>

????<groupId>org.springframework</groupId>

????<artifactId>spring-tx</artifactId>

????<version>5.0.9.RELEASE</version>

</dependency>

<!-- spring和mybatis的集成依赖包 -->

<dependency>

????<groupId>org.mybatis</groupId>

????<artifactId>mybatis-spring</artifactId>

????<version>1.3.2</version>

</dependency>

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

?

<?xml?version="1.0"?encoding="UTF-8"??>

<!DOCTYPE?configuration

??PUBLIC?"-//mybatis.org//DTD Config 3.0//EN"

??"http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>

??<!-- 使用java的properties文件来定义属性的话需resource指定文件的路径,相对classpath路径 -->

??<properties?resource="jdbc.properties"></properties>

??

??<!-- settings里面是一些mybatis的底层配置参数 -->

??<settings>

?? <setting?name="cacheEnabled"?value="true"/>?<!-- 关闭mybatis的二级缓存 -->

?? <setting?name="logImpl"?value="SLF4J"/>

?? <setting?name="mapUnderscoreToCamelCase"?value="true"/>

?? <setting?name="lazyLoadingEnabled"?value="true"/>

?? <setting?name="aggressiveLazyLoading"?value="false"/>

??</settings>

??<!-- typeAliases用来配置mapper中引用的类型名称的简称(别名) -->

??<typeAliases>

?? <!-- 通过包名称配置别名,该方法配置的别名就直接等于包下的类名(去掉包名称的部分)

?? ??????????或者类名称首字母小写-->

?? <package?name="com.zhiyou.bd28.pojo"/>

??</typeAliases>

??<environments?default="development">

????<environment?id="development">

??????<transactionManager?type="JDBC"/>

??????<dataSource?type="POOLED">

????????<property?name="driver"?value="${jdbc.driver}"/>

????????<property?name="url"?value="${jdbc.url}"/>

????????<property?name="username"?value="${jdbc.username}"/>

????????<property?name="password"?value="${jdbc.password}"/>

??????</dataSource>

????</environment>

??</environments>

??<mappers>

????<package?name="com.zhiyou.bd28.mapper"/>

??</mappers>

</configuration>

?

3.2?配置mybatisspring集成的部分

3.2.1spring中配置数据源

?????本例选用阿里的druid作为数据源

???添加依赖:

<!-- 引入druid数据连接池框架 -->

<dependency>

????<groupId>com.alibaba</groupId>

????<artifactId>druid</artifactId>

????<version>1.1.11</version>

</dependency>

<!-- 引入spring jdbc依赖 -->

<dependency>

????<groupId>org.springframework</groupId>

????<artifactId>spring-jdbc</artifactId>

????<version>5.0.9.RELEASE</version>

</dependency>

通过把druid的类型交给spring容器管理从而把druid引入到项目中。在spring.xml中:

????<!-- 配置druid数据源 -->

????<context:property-placeholder?location="classpath:jdbc.properties"/>

????<bean?id="dataSource"?class="com.alibaba.druid.pool.DruidDataSource"?init-method="init"?destroy-method="close">

???? <property?name="driverClassName"?value="${jdbc.driver}"/>

????<property?name="url"?value="${jdbc.url}"/>

????<property?name="username"?value="${jdbc.username}"/>

????<property?name="password"?value="${jdbc.password}"/>

????<property?name="filters"?value="stat"/>

????<property?name="maxActive"?value="20"/>

????<property?name="initialSize"?value="1"/>

????<property?name="maxWait"?value="60000"/>

????<property?name="minIdle"?value="1"/>

????<property?name="timeBetweenEvictionRunsMillis"?value="60000"/>

????<property?name="minEvictableIdleTimeMillis"?value="300000"/>

????<property?name="validationQuery"?value="SELECT ‘x‘"/>?<!-- 检查数据连接成功的语句 -->

????<property?name="testWhileIdle"?value="true"/>

????<property?name="testOnBorrow"?value="false"/>

????<property?name="testOnReturn"?value="false"/>

????</bean>

3.2.2 MybatisSqlSessionFactory交给spring容器管理

????<!-- 引入mybatisSqlSessionFactory -->

????<bean?id="sqlSessionFactory"?class="org.mybatis.spring.SqlSessionFactoryBean">

????????<!-- 引入druid连接池数据源 -->

????????<property?name="dataSource"?ref="dataSource"></property>

????????<property?name="mapperLocations"?value="classpath:com/zhiyou/bd28/dao/*Mapper.xml"/>

????????<!-- 添加mybatis.xml配置文件的引入 -->

???? <property?name="configLocation"?value="classpath:mybatis.xml"></property>

????</bean>

????<!-- 配置sqlSessionTemplate的sqlSessionFactory注入 -->

????<bean?id="sqlSession"?class="org.mybatis.spring.SqlSessionTemplate"?scope="prototype">

????<constructor-arg?name="sqlSessionFactory"?ref="sqlSessionFactory"/>

</bean>

删除mybatis.xml中关于数据源配置的context标签,和关于mapper文件的配置

<mappers>

????<package?name="com.zhiyou.bd28.dao"/>

??</mappers>

添加mybatis dao的扫描:

<!-- 添加mapper?Dao的扫描 -->

<bean?class="org.mybatis.spring.mapper.MapperScannerConfigurer">

????????<property?name="basePackage"?value="com.zhiyou.bd28.dao"/>

</bean>

3.2.3 使用spring管理mybatis的事务,把事务管理在service层进行拦截

spring.xml中支持事务tx标签和aop标签,把xml的头改成:

<?xml?version="1.0"?encoding="UTF-8"?>

<beans?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

???????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">

然后添加配置:

<!-- 事务管理器 -->

????<bean?id="transactionManager"?class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

????????<property?name="dataSource"?ref="dataSource"/>

????</bean>

????<!--事务详情(事务通知/增强), 在aop筛选基础上,确定使用什么样的事务。例如:读写、只读等

????????<tx:attributes>?用于配置事务详情(属性属性)

????????????<tx:method name=""/> 详情具体配置

????????????????propagation 传播行为 , REQUIRED:必须;REQUIRES_NEW:必须是新的 read-only 是否是只读事物

????????????????isolation 隔离级别 read-only 只读事物

????-->

????<tx:advice?id="txAdvice"?transaction-manager="transactionManager">

????????<tx:attributes>

????????????<tx:method?name="select*"?read-only="true"/>

????????????<tx:method?name="find*"?read-only="true"/>

????????????<tx:method?name="get*"?read-only="true"/>

????????????<tx:method?name="*"/>

????????</tx:attributes>

????</tx:advice>

????<!--支持基于注解的aspectj-->

????<aop:aspectj-autoproxy/>

????<!--AOP编程,切入点表达式 确定增强的连接器,从而获得切入点-->

????<aop:config>

????????<aop:advisor?advice-ref="txAdvice"?pointcut="execution(* com.zhiyou.bd28.service..*.*(..))"/>

</aop:config>

3.3?启动druid监控

web.xml中添加监控界面的访问servlet配置:

??<!-- druid监控页面设置 -->

??<servlet>??

<servlet-name>DruidStatView</servlet-name>??

<servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>

??<init-param>

<param-name>profileEnable</param-name>

<param-value>true</param-value>

??</init-param>

??<init-param>

????<param-name>resetEnable</param-name>

????<param-value>false</param-value>

??</init-param>

??<!-- 可选项 登陆的用户名和密码 -->

??<init-param>

?? <param-name>loginUsername</param-name>

?? <param-value>?druid</param-value>

??</init-param>

??<init-param>

?? <param-name>loginPassword</param-name>

?? <param-value>druid</param-value>

??</init-param>

??</servlet>??

??<servlet-mapping>??

<servlet-name>DruidStatView</servlet-name>??

????<url-pattern>/druid/*</url-pattern>??

??</servlet-mapping>

?

4.?集成mybatis-generator

<!-- 添加依赖:

Pom.xml中引入generator的插件依赖:-->

??<build>

?? <plugins>

?? <plugin>

?? ??<!-- 添加插件 -->

??????????<groupId>org.mybatis.generator</groupId>

??????????<artifactId>mybatis-generator-maven-plugin</artifactId>

??????????<version>1.3.2</version>

??????????<!-- executions的作用是使得该项目具有:mvn?mybatis-generator:generate启动命令 -->

??????????<!-- 给插件添加依赖 -->

??????????<dependencies>

?????????? <dependency>

????<groupId>mysql</groupId>

????<artifactId>mysql-connector-java</artifactId>

????<version>5.1.39</version>

</dependency>

??????????</dependencies>

??????????<!-- 给插件配置参数 -->

??????????<configuration>

?????????? <!-- 生成代码配置文件的路径 -->

?????????? <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>

?????????? <overwrite>true</overwrite>

?????????? <verbose>true</verbose>

??????????</configuration>

????????</plugin>

?? </plugins>

??</build>

?

创建自动生成代码的配置文件:/src/main/resources/generatorConfig.xml

<?xml?version="1.0"?encoding="UTF-8"?>

<!DOCTYPE?generatorConfiguration

??PUBLIC?"-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"

??"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

?

<generatorConfiguration>

??<properties?resource="jdbc.properties"/>

??<context?id="mysqltables"?targetRuntime="MyBatis3"?defaultModelType="flat">

????<jdbcConnection?driverClass="${jdbc.driver}"

????????connectionURL="${jdbc.url}"

????????userId="${jdbc.username}"

????????password="${jdbc.password}">

????</jdbcConnection>

????<javaTypeResolver?>

??????<property?name="forceBigDecimals"?value="false"?/>

????</javaTypeResolver>

?

<!-- 实体类的生成基本配置 -->

????<javaModelGenerator?targetPackage="com.zhiyou.bd28.model"?targetProject="src/main/java">

??????<property?name="enableSubPackages"?value="true"?/>

??????<property?name="trimStrings"?value="true"?/>

????</javaModelGenerator>

<!-- mapper.xml配置文件的路径等参数配置 -->

????<sqlMapGenerator?targetPackage="com.zhiyou.bd28.dao"??targetProject="src/main/resources">

??????<property?name="enableSubPackages"?value="true"?/>

????</sqlMapGenerator>

<!-- 配置mapper接口的代码生成路径 -->

????<javaClientGenerator?type="XMLMAPPER"?targetPackage="com.zhiyou.bd28.dao"??targetProject="src/main/java">

??????<property?name="enableSubPackages"?value="true"?/>

????</javaClientGenerator>

?

<!-- 每一个table标签代表着一个表的代码自动生成设置 -->

????<table?tableName="otb_user"?domainObjectName="OtbUser"?>

??????<property?name="useActualColumnNames"?value="false"/>

????</table>

??</context>

</generatorConfiguration>

?

启动maven指令运行生成代码:

mybatis-generator:generate

5.?集成mybatis pagehelper

添加依赖:

<!-- mybatis自动分页插件依赖 -->

<dependency>

????<groupId>com.github.pagehelper</groupId>

????<artifactId>pagehelper</artifactId>

????<version>5.1.6</version>

</dependency>

?

配置插件到mybatis.xml

??<plugins>

?? <!-- 添加pagehelper插件配置 -->

?? <plugin?interceptor="com.github.pagehelper.PageInterceptor">

?? <property?name="helperDialect"?value="mysql"/>

?? </plugin>

??</plugins>

6.?集成tk.mybatis

  1. 项目中引入依赖:

?????<!-- 集成tk.mybatis -->

?????<dependency>

????<groupId>tk.mybatis</groupId>

????<artifactId>mapper</artifactId>

????<version>3.4.0</version>?

?</dependency>

  1. spring.xml中引入

????<!-- 引入tk.mybatis的通用map -->

????<bean?class="tk.mybatis.spring.mapper.MapperScannerConfigurer">

????????<property?name="basePackage"?value="com.zhiyou.bd28.dao"/>

</bean>

  1. 自动生成代码插件配置

??<build>

?? <plugins>

?? <plugin>

?? ??<!-- 添加插件 -->

??????????<groupId>org.mybatis.generator</groupId>

??????????<artifactId>mybatis-generator-maven-plugin</artifactId>

??????????<version>1.3.2</version>

??????????<!-- executions的作用是使得该项目具有:mvn?mybatis-generator:generate启动命令 -->

??????????<!-- 给插件添加依赖 -->

??????????<dependencies>

?????????? <dependency>

????<groupId>mysql</groupId>

????<artifactId>mysql-connector-java</artifactId>

????<version>5.1.39</version>

</dependency>

<dependency>

????<groupId>tk.mybatis</groupId>

????<artifactId>mapper</artifactId>

????<version>3.4.0</version>

?</dependency>

??????????</dependencies>

??????????<!-- 给插件配置参数 -->

??????????<configuration>

?????????? <!-- 生成代码配置文件的路径 -->

?????????? <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>

?????????? <overwrite>true</overwrite>

?????????? <verbose>true</verbose>

??????????</configuration>

????????</plugin>

?? </plugins>

??</build>

  1. 在自动生成代码配置文件generatorConfig.xml中配置:

首先在jdbc.properties中添加配置:

???mapper.plugin=tk.mybatis.mapper.generator.MapperPlugin

mapper.Mapper=tk.mybatis.mapper.common.Mapper

然后再在generatorConfig.xml中配置:

??<context?id="mysqltables"?targetRuntime="MyBatis3"?defaultModelType="flat">

????<property name="javaFileEncoding"?value="UTF-8"/>

????<property name="beginningDelimiter"?value="`"/>

????<property name="endingDelimiter"?value="`"/>

?? <plugin type="${mapper.plugin}">

????????<property name="mappers"?value="${mapper.Mapper}"/>

????</plugin>

????<jdbcConnection?driverClass="${jdbc.driver}"

????????connectionURL="${jdbc.url}"

????????userId="${jdbc.username}"

????????password="${jdbc.password}">

????</jdbcConnection>

????<javaTypeResolver?>

??????<property?name="forceBigDecimals"?value="false"?/>

????</javaTypeResolver>

?

7.?集成EHCache

引入依赖

?<!-- ehcache支持 -->

?<dependency>

????<groupId>net.sf.ehcache</groupId>

????<artifactId>ehcache</artifactId>

????<version>2.10.5</version>

</dependency>

<dependency>

????<groupId>org.springframework</groupId>

????<artifactId>spring-context-support</artifactId>

????<version>5.0.9.RELEASE</version>

</dependency>

<dependency>

????<groupId>org.mybatis.caches</groupId>

????<artifactId>mybatis-ehcache</artifactId>

????<version>1.1.0</version>

</dependency>

resources目录下添加ehcache配置文件ehcache.xml

?

<?xml?version="1.0"?encoding="UTF-8"?>

<ehcache>

????<!-- java.io.tmpdir:Java临时目录。指定一个文件目录,当EhCache把数据写到硬盘上或者系统jvm内存时,将把数据写到这个文件目录下 -->

????<diskStore?path="java.io.tmpdir"/>?

????<!-- 设定缓存的默认数据过期策略 -->

????<defaultCache

????????????maxElementsInMemory="10000"

????????????eternal="false"

????????????overflowToDisk="true"

????????????timeToIdleSeconds="10"

????????????timeToLiveSeconds="20"

????????????diskPersistent="false"

????????????diskExpiryThreadIntervalSeconds="120"/>

?

????<!-- ?自定义缓存策略 -->

????<cache?name="myCache"

???????????maxElementsInMemory="1000"

???????????eternal="false"

???????????overflowToDisk="true"

???????????timeToIdleSeconds="10"

???????????timeToLiveSeconds="20"/>

</ehcache>

?

添加spring的cache支持文件:spring-ehcache.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: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">

?

????<!--配置cache-->

????<cache:annotation-driven?cache-manager="cacheManager"/>

?

????<!--需要引入ehcache.jar 和 spring-context-support.jar-->

????<bean?id="cacheManager"?class="org.springframework.cache.ehcache.EhCacheCacheManager">

????????<property?name="cacheManager"?ref="ehcache"/>

????</bean>

????<!-- 引入刚才的配置文件,一定要看好路径-->

????<bean?id="ehcache"?class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">

????????<property?name="configLocation"?value="classpath:ehcache.xml"></property>

????</bean>

</beans>

spring-ehcache.xml引入(import)到spring.xml中

????<!-- 引入spring-ehcache.xml -->

????<import?resource="spring-ehcache.xml"/>

????

?散文欣赏

没有中意的散文附上一篇礼拜的原别离

远别离,古有皇英之二女,乃在洞庭之南,潇湘之浦。海水直下万里深,谁人不言此离苦?日惨惨兮云冥冥,猩猩啼烟兮鬼啸雨。我纵言之将何补?皇穹窃恐不照余之忠诚,雷凭凭兮欲吼怒。尧舜当之亦禅禹。君失臣兮龙为鱼,权归臣兮鼠变虎。或云:尧幽囚,舜野死。九疑联绵皆相似,重瞳孤坟竟何是?帝子泣兮绿云间,随风波兮去无还。恸哭兮远望,见苍梧之深山。苍梧山崩湘水绝,竹上之泪乃可灭。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读