spring 配置 druid

1. 创建工程,添加 jar 包依赖

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.     <groupId>com.test</groupId>  
  5.     <artifactId>spring.druid</artifactId>  
  6.     <packaging>war</packaging>  
  7.     <version>0.0.1-SNAPSHOT</version>  
  8.     <name>spring.druid Maven Webapp</name>  
  9.     <url>http://maven.apache.org</url>  
  10.   
  11.     <properties>  
  12.         <maven.compiler.source>1.8</maven.compiler.source>  
  13.         <maven.compiler.target>1.8</maven.compiler.target>  
  14.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  15.   
  16.         <!-- 第三方库 -->  
  17.         <junit.version>4.11</junit.version>  
  18.         <spring.version>4.1.6.RELEASE</spring.version>  
  19.         <mysql.connector.java.version>5.1.26</mysql.connector.java.version>  
  20.         <druid.version>1.0.7</druid.version>  
  21.         <javax.servlet.version>3.0.1</javax.servlet.version>  
  22.         <aspectjweaver.version>1.8.1</aspectjweaver.version>  
  23.         <org.slf4j.log4j12.version>1.7.5</org.slf4j.log4j12.version>  
  24.     </properties>  
  25.   
  26.     <repositories>  
  27.         <repository>  
  28.             <id>oschinaRepository</id>  
  29.             <name>local private nexus</name>  
  30.             <url>http://maven.oschina.net/content/groups/public/</url>  
  31.             <releases>  
  32.                 <enabled>true</enabled>  
  33.             </releases>  
  34.             <snapshots>  
  35.                 <enabled>true</enabled>  
  36.             </snapshots>  
  37.         </repository>  
  38.     </repositories>  
  39.   
  40.     <dependencies>  
  41.   
  42.         <dependency>  
  43.             <groupId>junit</groupId>  
  44.             <artifactId>junit</artifactId>  
  45.             <version>${junit.version}</version>  
  46.             <scope>test</scope>  
  47.         </dependency>  
  48.   
  49.         <!-- Log 框架 -->  
  50.         <dependency>  
  51.             <groupId>org.slf4j</groupId>  
  52.             <artifactId>slf4j-log4j12</artifactId>  
  53.             <version>${org.slf4j.log4j12.version}</version>  
  54.         </dependency>  
  55.   
  56.         <!-- servlet -->  
  57.         <dependency>  
  58.             <groupId>javax.servlet</groupId>  
  59.             <artifactId>javax.servlet-api</artifactId>  
  60.             <version>${javax.servlet.version}</version>  
  61.             <scope>provided</scope>  
  62.         </dependency>  
  63.   
  64.         <!-- Spring 框架 -->  
  65.         <!-- 加入spring mvc依赖包 -->  
  66.         <dependency>  
  67.             <groupId>org.springframework</groupId>  
  68.             <artifactId>spring-webmvc</artifactId>  
  69.             <version>${spring.version}</version>  
  70.         </dependency>  
  71.         <dependency>  
  72.             <groupId>org.springframework</groupId>  
  73.             <artifactId>spring-jdbc</artifactId>  
  74.             <version>${spring.version}</version>  
  75.         </dependency>  
  76.         <!-- 加入spring测试依赖包 -->  
  77.         <dependency>  
  78.             <groupId>org.springframework</groupId>  
  79.             <artifactId>spring-test</artifactId>  
  80.             <version>${spring.version}</version>  
  81.             <scope>test</scope>  
  82.         </dependency>  
  83.   
  84.         <!-- aspectj -->  
  85.         <dependency>  
  86.             <groupId>org.aspectj</groupId>  
  87.             <artifactId>aspectjweaver</artifactId>  
  88.             <version>${aspectjweaver.version}</version>  
  89.         </dependency>  
  90.   
  91.         <!-- 数据库驱动 -->  
  92.         <dependency>  
  93.             <groupId>mysql</groupId>  
  94.             <artifactId>mysql-connector-java</artifactId>  
  95.             <version>${mysql.connector.java.version}</version>  
  96.         </dependency>  
  97.   
  98.         <!-- 加入druid数据源依赖包 -->  
  99.         <dependency>  
  100.             <groupId>com.alibaba</groupId>  
  101.             <artifactId>druid</artifactId>  
  102.             <version>${druid.version}</version>  
  103.         </dependency>  
  104.   
  105.     </dependencies>  
  106.     <build>  
  107.         <finalName>spring.druid</finalName>  
  108.   
  109.         <plugins>  
  110.             <plugin>  
  111.                 <groupId>org.apache.maven.plugins</groupId>  
  112.                 <artifactId>maven-surefire-plugin</artifactId>  
  113.                 <version>2.19</version>  
  114.                 <configuration>  
  115.                     <skip>true</skip>  
  116.                     <testFailureIgnore>true</testFailureIgnore>  
  117.                     <includes>  
  118.                         <include>**/*Test.java</include>  
  119.                         <include>**/*TestCase.java</include>  
  120.                         <include>**/Test*.java</include>  
  121.                     </includes>  
  122.                     <excludes>  
  123.                         <exclude>**/Abstract*.java</exclude>  
  124.                     </excludes>  
  125.                 </configuration>  
  126.             </plugin>  
  127.   
  128.             <plugin>  
  129.                 <!-- 编译的时候使用JDK8和UTF8编码 -->  
  130.                 <groupId>org.apache.maven.plugins</groupId>  
  131.                 <artifactId>maven-compiler-plugin</artifactId>  
  132.                 <version>3.1</version>  
  133.                 <configuration>  
  134.                     <source>1.8</source>  
  135.                     <target>1.8</target>  
  136.                     <encoding>UTF-8</encoding>  
  137.                 </configuration>  
  138.             </plugin>  
  139.   
  140.             <!-- war打包插件 -->  
  141.             <plugin>  
  142.                 <groupId>org.apache.maven.plugins</groupId>  
  143.                 <artifactId>maven-war-plugin</artifactId>  
  144.                 <version>2.2</version>  
  145.                 <configuration>  
  146.                     <!-- http://maven.apache.org/plugins/maven-war-plugin/ -->  
  147.                     <packagingExcludes>WEB-INF/web.xml</packagingExcludes>  
  148.                 </configuration>  
  149.             </plugin>  
  150.         </plugins>  
  151.     </build>  
  152. </project>  

2. 配置 spring 相关文件--> spring.xml

  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" xmlns:p="http://www.springframework.org/schema/p"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  6.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">  
  7.   
  8.     <!-- 引入项目配置文件 -->  
  9.     <!-- <context:property-placeholder location="classpath:config.properties"   
  10.         /> -->  
  11.     <bean  
  12.         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  13.         <property name="locations">  
  14.             <list>  
  15.                 <value>classpath:config.properties</value>  
  16.             </list>  
  17.         </property>  
  18.     </bean>  
  19.   
  20.     <!-- 默认的注解映射的支持 -->  
  21.     <context:annotation-config />  
  22.   
  23.     <import resource="spring-druid.xml" />  
  24.     <import resource="spring-jdbc.xml" />  
  25.       
  26. </beans>  

--> spring-druid.xml

  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" xmlns:aop="http://www.springframework.org/schema/aop"  
  4.     xmlns:c="http://www.springframework.org/schema/c" xmlns:cache="http://www.springframework.org/schema/cache"  
  5.     xmlns:context="http://www.springframework.org/schema/context"  
  6.     xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"  
  7.     xmlns:lang="http://www.springframework.org/schema/lang" xmlns:mvc="http://www.springframework.org/schema/mvc"  
  8.     xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task"  
  9.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"  
  10.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  11.         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd  
  12.         http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd  
  13.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd  
  14.         http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd  
  15.         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd  
  16.         http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd  
  17.         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd  
  18.         http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd  
  19.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd  
  20.         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">  
  21.   
  22.     <!-- 配置druid数据源 -->  
  23.     <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource"  
  24.         init-method="init" destroy-method="close">  
  25.         <!-- 数据库连接基础信息 -->  
  26.         <property name="url" value="${jdbc_url}" />  
  27.         <property name="username" value="${jdbc_username}" />  
  28.         <property name="password" value="${jdbc_password}" />  
  29.   
  30.         <!-- 初始化连接大小 -->  
  31.         <property name="initialSize" value="0" />  
  32.         <!-- 连接池最大使用连接数量 -->  
  33.         <property name="maxActive" value="1500" />  
  34.         <!-- 连接池最小空闲 -->  
  35.         <property name="minIdle" value="0" />  
  36.         <!-- 获取连接最大等待时间 -->  
  37.         <property name="maxWait" value="60000" />  
  38.   
  39.         <!-- 是否缓存preparedStatement,也就是PSCache。PSCache对支持游标的数据库性能提升巨大,比如说oracle。在mysql下建议关闭。 -->  
  40.         <!-- <property name="poolPreparedStatements" value="true" /> <property   
  41.             name="maxPoolPreparedStatementPerConnectionSize" value="33" /> -->  
  42.   
  43.         <!-- 验证数据库连接有效性,要求查询语句 -->  
  44.         <property name="validationQuery" value="${validationQuery}" />  
  45.         <!-- 建议配置为true,不影响性能,并且保证安全性。申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。 -->  
  46.         <property name="testWhileIdle" value="true" />  
  47.         <!-- 申请连接时执行validationQuery检测连接是否有效,配置true会降低性能。 -->  
  48.         <property name="testOnBorrow" value="false" />  
  49.         <!-- 归还连接时执行validationQuery检测连接是否有效,配置true会降低性能 -->  
  50.         <property name="testOnReturn" value="false" />  
  51.   
  52.         <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->  
  53.         <property name="timeBetweenEvictionRunsMillis" value="60000" />  
  54.         <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->  
  55.         <property name="minEvictableIdleTimeMillis" value="25200000" />  
  56.   
  57.         <!-- 对于长时间不使用的连接强制关闭 -->  
  58.         <property name="removeAbandoned" value="true" />  
  59.         <!-- 关闭超过30分钟的空闲连接,1800秒,也就是30分钟 -->  
  60.         <property name="removeAbandonedTimeout" value="1800" />  
  61.         <!-- 关闭abanded连接时输出错误日志 -->  
  62.         <property name="logAbandoned" value="true" />  
  63.   
  64.         <!-- 监控数据库 -->  
  65.         <!-- <property name="filters" value="mergeStat" /> -->  
  66.         <property name="filters" value="stat" />  
  67.     </bean>  
  68.   
  69.     <!-- 配置druid监控spring jdbc -->  
  70.     <bean id="druid-stat-interceptor"  
  71.         class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor" />  
  72.   
  73.     <bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut"  
  74.         scope="prototype">  
  75.         <property name="patterns">  
  76.             <list>  
  77.                 <value>spring.druid.service.*</value>  
  78.             </list>  
  79.         </property>  
  80.     </bean>  
  81.     <aop:config>  
  82.         <aop:advisor advice-ref="druid-stat-interceptor"  
  83.             pointcut-ref="druid-stat-pointcut" />  
  84.     </aop:config>  
  85.   
  86. </beans>  

--> spring-jdbc.xml

  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" xmlns:aop="http://www.springframework.org/schema/aop"  
  4.     xmlns:c="http://www.springframework.org/schema/c" xmlns:cache="http://www.springframework.org/schema/cache"  
  5.     xmlns:context="http://www.springframework.org/schema/context"  
  6.     xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"  
  7.     xmlns:lang="http://www.springframework.org/schema/lang" xmlns:mvc="http://www.springframework.org/schema/mvc"  
  8.     xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task"  
  9.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"  
  10.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  11.         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd  
  12.         http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd  
  13.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd  
  14.         http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd  
  15.         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd  
  16.         http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd  
  17.         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd  
  18.         http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd  
  19.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd  
  20.         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">  
  21.   
  22.     <!-- 使用 spring jdbc 模板-->  
  23.     <bean id="jdbcTemplate " class="org.springframework.jdbc.core.JdbcTemplate">  
  24.         <constructor-arg ref="dataSource" />  
  25.     </bean>  
  26.     <!-- <bean id="simpleJdbcInsert" class="org.springframework.jdbc.core.simple.SimpleJdbcInsert">  
  27.         <constructor-arg ref="dataSource" />  
  28.     </bean>  
  29.     <bean id="simpleJdbcCall" class="org.springframework.jdbc.core.simple.SimpleJdbcCall">  
  30.         <constructor-arg ref="dataSource" />  
  31.     </bean> -->  
  32.   
  33. </beans>  

--> spring-mvc.xml

  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" xmlns:aop="http://www.springframework.org/schema/aop"  
  4.     xmlns:c="http://www.springframework.org/schema/c" xmlns:cache="http://www.springframework.org/schema/cache"  
  5.     xmlns:context="http://www.springframework.org/schema/context"  
  6.     xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"  
  7.     xmlns:lang="http://www.springframework.org/schema/lang" xmlns:mvc="http://www.springframework.org/schema/mvc"  
  8.     xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task"  
  9.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"  
  10.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  11.         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd  
  12.         http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd  
  13.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd  
  14.         http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd  
  15.         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd  
  16.         http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd  
  17.         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd  
  18.         http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd  
  19.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd  
  20.         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">  
  21.   
  22.     <!-- 启动支持 mvc 的注解,比如  @Controller, @RequestMapping 等-->  
  23.     <mvc:annotation-driven />  
  24.   
  25.     <!-- 自动扫描 controller 包下的所有类 -->  
  26.     <context:component-scan base-package="spring.druid.controller" />  
  27.   
  28.     <!-- 设置视图解析工具 -->  
  29.     <bean  
  30.         class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  31.         <!-- 设置文件前缀 -->  
  32.         <property name="prefix" value="/WEB-INF/jsp/" />  
  33.         <!-- 设置文件后缀 -->  
  34.         <property name="suffix" value=".jsp" />  
  35.     </bean>  
  36.   
  37. </beans>  

3. 其它相关配置文件

--> config.properties

  1. jdbc_url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull  
  2. jdbc_username=test  
  3. jdbc_password=test  
  4. driver_name=com.mysql.jdbc.Driver  
  5.   
  6. validationQuery=SELECT 1

--> log4j.properties

  1.  ### \u8BBE\u7F6E###  
  2. log4j.rootLogger = info,warning,stdout,logfile,E  
  3.   
  4. ### \u8F93\u51FA\u4FE1\u606F\u5230\u63A7\u5236\u62AC ###  
  5. log4j.appender.stdout = org.apache.log4j.ConsoleAppender  
  6. log4j.appender.stdout.Target = System.out  
  7. log4j.appender.stdout.layout = org.apache.log4j.PatternLayout  
  8. log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n  
  9.   
  10. ### \u8F93\u51FADEBUG \u7EA7\u522B\u4EE5\u4E0A\u7684\u65E5\u5FD7\u5230=E://logs/error.log ###  
  11. log4j.appender.logfile = org.apache.log4j.DailyRollingFileAppender  
  12. log4j.appender.logfile.File = D://logs/log.log  
  13. log4j.appender.logfile.Append = true  
  14. log4j.appender.logfile.Threshold = DEBUG   
  15. log4j.appender.logfile.layout = org.apache.log4j.PatternLayout  
  16. log4j.appender.logfile.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n  
  17.   
  18. ### \u8F93\u51FAERROR \u7EA7\u522B\u4EE5\u4E0A\u7684\u65E5\u5FD7\u5230=E://logs/error.log ###  
  19. log4j.appender.E = org.apache.log4j.DailyRollingFileAppender  
  20. log4j.appender.E.File =D://logs/error.log   
  21. log4j.appender.E.Append = true  
  22. log4j.appender.E.Threshold = ERROR   
  23. log4j.appender.E.layout = org.apache.log4j.PatternLayout  
  24. log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n   

4. 配置 web.xml 文件

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">  
  7.   
  8.     <display-name>spring.druid</display-name>  
  9.   
  10.     <!-- 加载配置文件 -->  
  11.     <context-param>  
  12.         <param-name>contextConfigLocation</param-name>  
  13.         <param-value>classpath*:spring.xml</param-value>  
  14.     </context-param>  
  15.   
  16.     <!-- spring -->  
  17.     <listener>  
  18.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  19.     </listener>  
  20.     <listener>  
  21.         <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>  
  22.     </listener>  
  23.   
  24.     <!-- spring mvc -->  
  25.     <servlet>  
  26.         <servlet-name>springMvc</servlet-name>  
  27.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  28.         <init-param>  
  29.             <param-name>contextConfigLocation</param-name>  
  30.             <param-value>classpath:spring-mvc.xml</param-value>  
  31.         </init-param>  
  32.         <load-on-startup>1</load-on-startup>  
  33.         <async-supported>true</async-supported>  
  34.     </servlet>  
  35.     <servlet-mapping>  
  36.         <servlet-name>springMvc</servlet-name>  
  37.         <url-pattern>/</url-pattern>  
  38.     </servlet-mapping>  
  39.   
  40.     <!-- druid 监控 -->  
  41.     <filter>  
  42.         <filter-name>druidWebStatFilter</filter-name>  
  43.         <filter-class>com.alibaba.druid.support.http.WebStatFilter</filter-class>  
  44.         <init-param>  
  45.             <param-name>exclusions</param-name>  
  46.             <param-value>/assets/*,*.css,*.js,*.gif,*.jpg,*.png,*.ico,*.eot,*.svg,*.ttf,*.woff,*.jsp,*.tpl,/druid/*</param-value>  
  47.         </init-param>  
  48.     </filter>  
  49.     <filter-mapping>  
  50.         <filter-name>druidWebStatFilter</filter-name>  
  51.         <url-pattern>/*</url-pattern>  
  52.     </filter-mapping>  
  53.   
  54.     <servlet>  
  55.         <servlet-name>druidStatView</servlet-name>  
  56.         <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>  
  57.     </servlet>  
  58.     <servlet-mapping>  
  59.         <servlet-name>druidStatView</servlet-name>  
  60.         <url-pattern>/druid/*</url-pattern>  
  61.     </servlet-mapping>  
  62.   
  63.     <!-- log4j -->  
  64.     <context-param>  
  65.         <param-name>log4jConfigLocation</param-name>  
  66.         <param-value>classpath:log4j.properties</param-value>  
  67.     </context-param>  
  68.     <context-param>  
  69.         <param-name>webAppRootKey</param-name>  
  70.         <param-value>spring-druid.root </param-value>  
  71.     </context-param>  
  72.     <listener>  
  73.         <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>  
  74.     </listener>  
  75.   
  76.     <!-- 设置请求编码格式 -->  
  77.     <filter>  
  78.         <filter-name>encodingFilter</filter-name>  
  79.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  80.         <init-param>  
  81.             <param-name>encoding</param-name>  
  82.             <param-value>UTF-8</param-value>  
  83.         </init-param>  
  84.     </filter>  
  85.     <filter-mapping>  
  86.         <filter-name>encodingFilter</filter-name>  
  87.         <url-pattern>/*</url-pattern>  
  88.     </filter-mapping>  
  89. </web-app>  

5. 创建表

  1. CREATE TABLE `spring_druid` (  
  2.   `id` int(10) unsigned NOT NULL AUTO_INCREMENT,  
  3.   `test_name` varchar(255) DEFAULT NULL,  
  4.   PRIMARY KEY (`id`)  
  5. ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;  

6. 创建单元测试类

  1. package spring.druid.junitTest;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.PreparedStatement;  
  5. import java.sql.ResultSet;  
  6. import java.sql.SQLException;  
  7. import java.sql.Statement;  
  8. import java.util.Map;  
  9.   
  10. import org.junit.Test;  
  11. import org.junit.runner.RunWith;  
  12. import org.springframework.beans.factory.annotation.Autowired;  
  13. import org.springframework.jdbc.core.JdbcTemplate;  
  14. import org.springframework.test.context.ContextConfiguration;  
  15. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;  
  16.   
  17. import com.alibaba.druid.pool.DruidDataSource;  
  18.   
  19. @RunWith(SpringJUnit4ClassRunner.class)  
  20. @ContextConfiguration(value = { "classpath:spring.xml" })  
  21. public class SpringDruidJunitTest {  
  22.     @Autowired  
  23.     private JdbcTemplate jdbcTemplate;  
  24.   
  25.     @Autowired  
  26.     private DruidDataSource dataSource;  
  27.   
  28.     @Test  
  29.     public void testInsert() {  
  30.         jdbcTemplate.execute("insert into spring_druid (test_name) values ('spring')");  
  31.     }  
  32.   
  33.     @Test  
  34.     public void testQuery() {  
  35.         Map<String, Object> map = jdbcTemplate.queryForMap("select * from spring_druid limit 1");  
  36.         System.out.println(map);  
  37.     }  
  38.   
  39.     @Test  
  40.     public void testDruidDataSource() throws SQLException {  
  41.         Connection conn = dataSource.getConnection();  
  42.         conn.setAutoCommit(false);  
  43.         PreparedStatement pstat = conn.prepareStatement("insert into spring_druid(test_name) values (?)",  
  44.                 Statement.RETURN_GENERATED_KEYS);  
  45.         pstat.setString(1"druid");  
  46.         pstat.executeUpdate();  
  47.         Statement stat = conn.createStatement();  
  48.         ResultSet rs = stat.executeQuery("select * from spring_druid");  
  49.         conn.commit();  
  50.         while (rs.next()) {  
  51.             System.out.println("id=" + rs.getInt("id") + ", test_name=" + rs.getString("test_name"));  
  52.         }  
  53.     }  
  54. }  

7. 创建 controller 类

  1. package spring.druid.controller;  
  2.   
  3. import java.util.Map;  
  4.   
  5. import javax.servlet.http.HttpServletRequest;  
  6.   
  7. import org.springframework.beans.factory.annotation.Autowired;  
  8. import org.springframework.jdbc.core.JdbcTemplate;  
  9. import org.springframework.stereotype.Controller;  
  10. import org.springframework.web.bind.annotation.RequestMapping;  
  11.   
  12. @Controller  
  13. public class SpringDruidController {  
  14.   
  15.     @Autowired  
  16.     private JdbcTemplate jdbcTemplate;  
  17.   
  18.     @RequestMapping(value = "/test")  
  19.     public String m(final HttpServletRequest request) {  
  20.         Map<String, Object> map = jdbcTemplate.queryForMap("select * from spring_druid limit 1");  
  21.         request.setAttribute("test", map);  
  22.         return "test";  
  23.     }  
  24. }  

8. 创建 test.jsp 文件

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <!DOCTYPE html>  
  4. <html>  
  5. <head>  
  6. <meta charset="UTF-8">  
  7. <title>Insert title here</title>  
  8. </head>  
  9. <body>  
  10. <h2>${test}</h2>  
  11. </body>  
  12. </html>  



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值