Maven Web项目配置Mybatis

一、添加Mybatis和数据库相关的包

1 pom.xml中添加的包有mybatis,mybatis-spring,druid,MySQL-connector-Java,commons-io,reflectasm

完整的pom.xml内容为

 

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. <project xmlns="http://maven.apache.org/POM/4.0.0"   
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  4.   <modelVersion>4.0.0</modelVersion>  
  5.   <groupId>jade_admin</groupId>  
  6.   <artifactId>jade_admin</artifactId>  
  7.   <version>0.0.1-SNAPSHOT</version>  
  8.   <packaging>war</packaging>  
  9.   <name>jade_admin</name>  
  10.   <description/>  
  11.     
  12.   <properties>  
  13.     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  14.     <org.springframework.security.version>3.1.6.RELEASE</org.springframework.security.version>  
  15.     <org.springframework.version>3.2.9.RELEASE</org.springframework.version>  
  16.   </properties>  
  17.     
  18.   <dependencies>  
  19.     <dependency>  
  20.       <groupId>javax</groupId>  
  21.       <artifactId>javaee-api</artifactId>  
  22.       <version>7.0</version>  
  23.       <scope>provided</scope>  
  24.     </dependency>  
  25.       
  26.     <dependency>  
  27.         <groupId>org.springframework</groupId>  
  28.         <artifactId>spring-core</artifactId>  
  29.         <version>${org.springframework.version}</version>  
  30.     </dependency>  
  31.     <dependency>  
  32.         <groupId>org.springframework</groupId>  
  33.         <artifactId>spring-context</artifactId>  
  34.         <version>${org.springframework.version}</version>  
  35.     </dependency>  
  36.     <dependency>  
  37.         <groupId>org.springframework</groupId>  
  38.         <artifactId>spring-context-support</artifactId>  
  39.         <version>${org.springframework.version}</version>  
  40.     </dependency>  
  41.     <dependency>  
  42.         <groupId>org.springframework</groupId>  
  43.         <artifactId>spring-web</artifactId>  
  44.         <version>${org.springframework.version}</version>  
  45.     </dependency>  
  46.     <dependency>  
  47.         <groupId>org.springframework</groupId>  
  48.         <artifactId>spring-webmvc</artifactId>  
  49.         <version>${org.springframework.version}</version>  
  50.     </dependency>  
  51.       
  52.     <dependency>  
  53.         <groupId>org.springframework.security</groupId>  
  54.         <artifactId>spring-security-core</artifactId>  
  55.         <version>${org.springframework.security.version}</version>  
  56.     </dependency>  
  57.     <dependency>  
  58.         <groupId>org.springframework.security</groupId>  
  59.         <artifactId>spring-security-config</artifactId>  
  60.         <version>${org.springframework.security.version}</version>  
  61.     </dependency>  
  62.     <dependency>  
  63.         <groupId>org.springframework.security</groupId>  
  64.         <artifactId>spring-security-web</artifactId>  
  65.         <version>${org.springframework.security.version}</version>  
  66.     </dependency>  
  67.   
  68.     <dependency>  
  69.         <groupId>org.freemarker</groupId>  
  70.         <artifactId>freemarker</artifactId>  
  71.         <version>2.3.23</version>  
  72.     </dependency>   
  73.   
  74.     <dependency>  
  75.         <groupId>org.mybatis</groupId>  
  76.         <artifactId>mybatis</artifactId>  
  77.         <version>3.2.8</version>  
  78.     </dependency>  
  79.     <dependency>  
  80.         <groupId>org.mybatis</groupId>  
  81.         <artifactId>mybatis-spring</artifactId>  
  82.         <version>1.2.2</version>  
  83.     </dependency>  
  84.     <dependency>  
  85.         <groupId>com.alibaba</groupId>  
  86.         <artifactId>druid</artifactId>  
  87.         <version>1.0.14</version>  
  88.     </dependency>  
  89.     <dependency>  
  90.         <groupId>mysql</groupId>  
  91.         <artifactId>mysql-connector-java</artifactId>  
  92.         <version>5.1.34</version>  
  93.     </dependency>  
  94.       
  95.     <dependency>  
  96.         <groupId>commons-io</groupId>  
  97.         <artifactId>commons-io</artifactId>  
  98.         <version>2.4</version>  
  99.     </dependency>  
  100.     <dependency>  
  101.         <groupId>com.esotericsoftware.reflectasm</groupId>  
  102.         <artifactId>reflectasm</artifactId>  
  103.         <version>1.09</version>  
  104.     </dependency>  
  105.       
  106.     <dependency>  
  107.         <groupId>org.tuckey</groupId>  
  108.         <artifactId>urlrewritefilter</artifactId>  
  109.         <version>4.0.3</version>  
  110.     </dependency>  
  111.       
  112.   </dependencies>  
  113. </project>  

 

 

2 /src/main/webapp/WEB-INF/lib/中添加cglib-3.1.jar

二、配置web.xml

 

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">  
  3.   <display-name>jade_admin</display-name>  
  4.     <welcome-file-list>  
  5.         <welcome-file>index.jsp</welcome-file>  
  6.     </welcome-file-list>  
  7.   
  8.     <context-param>  
  9.         <param-name>contextConfigLocation</param-name>  
  10.         <param-value>/WEB-INF/classes/config/spring/applicationContext-config.xml</param-value>  
  11.     </context-param>  
  12.   
  13.     <listener>  
  14.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  15.     </listener>  
  16.     
  17.     <filter>  
  18.         <filter-name>UrlRewriteFilter</filter-name>  
  19.         <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>  
  20.     </filter>  
  21.     <filter-mapping>  
  22.         <filter-name>UrlRewriteFilter</filter-name>  
  23.         <url-pattern>/*</url-pattern>  
  24.         <dispatcher>REQUEST</dispatcher>  
  25.         <dispatcher>FORWARD</dispatcher>  
  26.     </filter-mapping>  
  27.   
  28.     <!-- 添加Spring-Security过滤器 -->  
  29.     <filter>  
  30.         <filter-name>springSecurityFilterChain</filter-name>  
  31.         <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>  
  32.     </filter>  
  33.     <filter-mapping>  
  34.         <filter-name>springSecurityFilterChain</filter-name>  
  35.         <url-pattern>/service/*</url-pattern>  
  36.     </filter-mapping>  
  37.           
  38.     <servlet>    
  39.         <servlet-name>dispatcher</servlet-name>    
  40.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    
  41.         <init-param>    
  42.             <param-name>contextConfigLocation</param-name>    
  43.             <param-value>/WEB-INF/classes/config/spring/applicationContext-mvc.xml</param-value>    
  44.         </init-param>    
  45.         <load-on-startup>1</load-on-startup>    
  46.     </servlet>    
  47.     <servlet-mapping>    
  48.         <servlet-name>dispatcher</servlet-name>    
  49.         <url-pattern>/service/*</url-pattern>    
  50.     </servlet-mapping>  
  51.   
  52. </web-app>  

 

 

三、配置applicationContext-config.xml

 

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  6.     xsi:schemaLocation="  
  7.     http://www.springframework.org/schema/beans  
  8.     http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
  9.     http://www.springframework.org/schema/context  
  10.     http://www.springframework.org/schema/context/spring-context-3.2.xsd  
  11.     http://www.springframework.org/schema/mvc  
  12.     http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">  
  13.       
  14.     <import resource="classpath*:config/spring/applicationContext-security.xml" />  
  15.     <import resource="classpath*:config/spring/applicationContext-resources.xml" />  
  16.   
  17. </beans>  

 

 

四、配置applicationContext-resource.xml

 

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  6.     xmlns:tx="http://www.springframework.org/schema/tx"  
  7.     xmlns:aop="http://www.springframework.org/schema/aop"  
  8.     xsi:schemaLocation="  
  9.     http://www.springframework.org/schema/beans  
  10.     http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
  11.     http://www.springframework.org/schema/context  
  12.     http://www.springframework.org/schema/context/spring-context-3.2.xsd  
  13.     http://www.springframework.org/schema/mvc  
  14.     http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd  
  15.     http://www.springframework.org/schema/aop  
  16.     http://www.springframework.org/schema/aop/spring-aop-3.2.xsd    
  17.     http://www.springframework.org/schema/tx    
  18.     http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">  
  19.   
  20.       
  21.     <!-- 阿里 druid数据库连接池 -->  
  22.     <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">    
  23.          <!-- 数据库基本信息配置 -->  
  24.          <property name="url" value="jdbc:mysql://127.0.0.1:3306/jade?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull" />    
  25.          <property name="username" value="root" />    
  26.          <property name="password" value="123456" />    
  27.          <property name="driverClassName" value="com.mysql.jdbc.Driver" />    
  28.          <property name="filters" value="stat" />    
  29.          <!-- 最大并发连接数 -->  
  30.          <property name="maxActive" value="20" />  
  31.          <!-- 初始化连接数量 -->  
  32.          <property name="initialSize" value="1" />  
  33.          <!-- 配置获取连接等待超时的时间 -->  
  34.          <property name="maxWait" value="60000" />  
  35.          <!-- 最小空闲连接数 -->  
  36.          <property name="minIdle" value="10" />    
  37.          <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->  
  38.          <property name="timeBetweenEvictionRunsMillis" value="60000" />  
  39.          <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->  
  40.          <property name="minEvictableIdleTimeMillis" value="300000" />    
  41.          <property name="validationQuery" value="SELECT 'x'" />    
  42.          <property name="testWhileIdle" value="true" />    
  43.          <property name="testOnBorrow" value="false" />    
  44.          <property name="testOnReturn" value="false" />    
  45.          <property name="maxOpenPreparedStatements" value="20" />  
  46.          <!-- 打开removeAbandoned功能 -->  
  47.          <property name="removeAbandoned" value="true" />  
  48.          <!-- 1800秒,也就是30分钟 -->  
  49.          <property name="removeAbandonedTimeout" value="1800" />  
  50.          <!-- 关闭abanded连接时输出错误日志 -->     
  51.          <property name="logAbandoned" value="true" />  
  52.     </bean>   
  53.       
  54.     <bean id="txManager"  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  55.         <property name="dataSource" ref="dataSource" />  
  56.     </bean>  
  57.     <bean id="txTemplate" class="org.springframework.transaction.support.TransactionTemplate">  
  58.         <property name="transactionManager" ref="txManager" />  
  59.     </bean>  
  60.     <bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  61.         <property name="dataSource" ref="dataSource"/>  
  62.         <property name="configLocation" value="classpath:config/mybatis/configuration.xml"/>  
  63.         <property name="mapperLocations" value="classpath*:config/mybatis/mapper/*Mapper.xml" />  
  64.         <property name="typeAliasesPackage" value="com.zheng.bean" />    
  65.     </bean>  
  66.     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
  67.         <property name="basePackage" value="com.zheng.dao"/>  
  68.         <property name="sqlSessionFactory" ref="SqlSessionFactory" />  
  69.     </bean>  
  70.   
  71.     <tx:annotation-driven transaction-manager="txManager" />  
  72.       
  73. </beans>  

 

 

五、配置configuration.xml

 

[html] view plain copy
 
 在CODE上查看代码片派生到我的代码片
    1. <?xml version="1.0" encoding="UTF-8" ?>   
    2. <!DOCTYPE configuration   PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN"   "http://ibatis.apache.org/dtd/ibatis-3-config.dtd">  
    3.   
    4.     <!-- "http://ibatis.apache.org/dtd/ibatis-3-config.dtd" -->  
    5.     <!--   
    6.         plugins在配置文件中的位置必须符合要求,否则会报错,顺序如下:  
    7.         properties?, settings?,   
    8.         typeAliases?, typeHandlers?,   
    9.         objectFactory?,objectWrapperFactory?,   
    10.         plugins?,   
    11.         environments?, databaseIdProvider?, mappers?  
    12.     -->  
    13. <configuration>  
    14.     <properties>  
    15.     </properties>  
    16.     <!-- mybatis的内部配置 -->  
    17.     <settings>  
    18.         <!--使全局的映射器启用缓存  -->  
    19.         <setting name="cacheEnabled" value="true" />  
    20.         <!--启用延迟加载 ,当禁用时,所有关联对象都会即时加载 -->  
    21.         <setting name="lazyLoadingEnabled" value="false" />  
    22.         <!-- 当启用时,有延迟加载属性的对象在被调用时将会完全加载任意属性。否则,每种属性将会按需要加载  -->  
    23.         <setting name="aggressiveLazyLoading" value="true" />  
    24.         <!--允许多结果集返回, 允许或不允许多种结果集从一个单独的语句中返回(需要适合的驱动)-->  
    25.         <setting name="multipleResultSetsEnabled" value="true" />  
    26.         <!--使用列标签代替列名,不同的驱动在这方便表现不同。参考驱动文档或充分测试两种方法来决定所使用的驱动  -->  
    27.         <setting name="useColumnLabel" value="true" />  
    28.         <!--不能用生成键  -->  
    29.         <setting name="useGeneratedKeys" value="false" />  
    30.         <!-- 指定MyBatis如何自动映射列到字段/属性。PARTIAL只会自动映射简单,没有嵌套的结果。FULL会自动映射任意复杂的结果(嵌套的或其他情况)  -->  
    31.         <setting name="autoMappingBehavior" value="PARTIAL" />  
    32.         <!--默认执行器为SIMPLE,SIMPLE执行器没有什么特别之处。REUSE执行器重用预处理语句。BATCH执行器重用语句和批量更新   -->  
    33.         <setting name="defaultExecutorType" value="SIMPLE" />  
    34.         <!--超时时间2500毫妙,它决定驱动等待一个数据库响应的时间  -->  
    35.         <setting name="defaultStatementTimeout" value="25000" />  
    36.           
    37.         <setting name="proxyFactory" value="CGLIB"/>  
    38.     </settings>  
    39.       
    40. </configuration>  
posted @ 2017-04-19 12:39  爱你爱自己  阅读(232)  评论(0编辑  收藏  举报