当前位置:Gxlcms > 数据库问题 > druid数据库连接池

druid数据库连接池

时间:2021-07-01 10:21:17 帮助过:15人阅读

<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.lizj</groupId> 5 <artifactId>examination</artifactId> 6 <packaging>war</packaging> 7 <version>0.0.1-SNAPSHOT</version> 8 <name>examination Maven Webapp</name> 9 <url>http://maven.apache.org</url> 10 11 <properties> 12 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 13 <spring.version>3.2.13.RELEASE</spring.version> 14 <slf4j.version>1.7.7</slf4j.version> 15 <logback.version>1.1.2</logback.version> 16 </properties> 17 18 <dependencies> 19 <dependency> 20 <groupId>junit</groupId> 21 <artifactId>junit</artifactId> 22 <version>3.8.1</version> 23 <scope>test</scope> 24 </dependency> 25 <!-- log4j start --> 26 <dependency> 27 <groupId>log4j</groupId> 28 <artifactId>log4j</artifactId> 29 <version>1.2.17</version> 30 </dependency> 31 <!-- log4j end --> 32 33 <!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api --> 34 <dependency> 35 <groupId>javax.servlet</groupId> 36 <artifactId>servlet-api</artifactId> 37 <version>2.5</version> 38 </dependency> 39 40 <!--spring start --> 41 <!-- spring core/beans/aop/web/webmvc begin --> 42 <dependency> 43 <groupId>org.springframework</groupId> 44 <artifactId>spring-core</artifactId> 45 <version>${spring.version}</version> 46 </dependency> 47 <dependency> 48 <groupId>org.springframework</groupId> 49 <artifactId>spring-beans</artifactId> 50 <version>${spring.version}</version> 51 </dependency> 52 <dependency> 53 <groupId>org.springframework</groupId> 54 <artifactId>spring-web</artifactId> 55 <version>${spring.version}</version> 56 </dependency> 57 <dependency> 58 <groupId>org.springframework</groupId> 59 <artifactId>spring-webmvc</artifactId> 60 <version>${spring.version}</version> 61 </dependency> 62 <dependency> 63 <groupId>org.springframework</groupId> 64 <artifactId>spring-jdbc</artifactId> 65 <version>${spring.version}</version> 66 </dependency> 67 <dependency> 68 <groupId>org.springframework</groupId> 69 <artifactId>spring-context</artifactId> 70 <version>${spring.version}</version> 71 </dependency> 72 <dependency> 73 <groupId>org.springframework</groupId> 74 <artifactId>spring-aop</artifactId> 75 <version>${spring.version}</version> 76 </dependency> 77 <dependency> 78 <groupId>org.springframework</groupId> 79 <artifactId>spring-tx</artifactId> 80 <version>${spring.version}</version> 81 </dependency> 82 <dependency> 83 <groupId>org.springframework</groupId> 84 <artifactId>spring-orm</artifactId> 85 <version>${spring.version}</version> 86 </dependency> 87 88 <dependency> 89 <groupId>org.springframework</groupId> 90 <artifactId>spring-context-support</artifactId> 91 <version>${spring.version}</version> 92 </dependency> 93 94 <dependency> 95 <groupId>org.springframework</groupId> 96 <artifactId>spring-expression</artifactId> 97 <version>${spring.version}</version> 98 </dependency> 99 100 <dependency> 101 <groupId>org.springframework</groupId> 102 <artifactId>spring-aspects</artifactId> 103 <version>${spring.version}</version> 104 </dependency> 105 <dependency> 106 <groupId>org.springframework</groupId> 107 <artifactId>spring-test</artifactId> 108 <version>${spring.version}</version> 109 </dependency> 110 111 <dependency> 112 <groupId>org.aspectj</groupId> 113 <artifactId>aspectjrt</artifactId> 114 <version>1.8.6</version> 115 </dependency> 116 117 <dependency> 118 <groupId>org.aspectj</groupId> 119 <artifactId>aspectjweaver</artifactId> 120 <version>1.8.6</version> 121 </dependency> 122 123 <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging --> 124 <dependency> 125 <groupId>commons-logging</groupId> 126 <artifactId>commons-logging</artifactId> 127 <version>1.1.3</version> 128 </dependency> 129 <!--spring end --> 130 131 <!-- druid start --> 132 <!-- https://mvnrepository.com/artifact/com.alibaba/druid --> 133 <dependency> 134 <groupId>com.alibaba</groupId> 135 <artifactId>druid</artifactId> 136 <version>1.0.27</version> 137 </dependency> 138 <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 --> 139 <dependency> 140 <groupId>org.slf4j</groupId> 141 <artifactId>slf4j-log4j12</artifactId> 142 <version>1.7.9</version> 143 </dependency> 144 <!-- druid end --> 145 <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> 146 <dependency> 147 <groupId>mysql</groupId> 148 <artifactId>mysql-connector-java</artifactId> 149 <version>5.1.40</version> 150 </dependency> 151 <!-- spring start --> 152 153 <!-- 日志监控数据库连接池 --> 154 <dependency> 155 <groupId>org.slf4j</groupId> 156 <artifactId>slf4j-api</artifactId> 157 <version>${slf4j.version}</version> 158 </dependency> 159 <dependency> 160 <groupId>ch.qos.logback</groupId> 161 <artifactId>logback-access</artifactId> 162 <version>${logback.version}</version> 163 </dependency> 164 <dependency> 165 <groupId>ch.qos.logback</groupId> 166 <artifactId>logback-core</artifactId> 167 <version>${logback.version}</version> 168 </dependency> 169 <dependency> 170 <groupId>ch.qos.logback</groupId> 171 <artifactId>logback-classic</artifactId> 172 <version>${logback.version}</version> 173 </dependency> 174 <!-- 日志监控数据库连接池 --> 175 176 <!-- javaee web应用包 begin --> 177 <dependency> 178 <groupId>javax.servlet.jsp</groupId> 179 <artifactId>jsp-api</artifactId> 180 <version>2.1</version> 181 <scope>provided</scope> 182 </dependency> 183 <dependency> 184 <groupId>javax.servlet</groupId> 185 <artifactId>jstl</artifactId> 186 <version>1.2</version> 187 <scope>provided</scope> 188 </dependency> 189 <dependency> 190 <groupId>taglibs</groupId> 191 <artifactId>standard</artifactId> 192 <version>1.1.2</version> 193 <scope>provided</scope> 194 </dependency> 195 <!-- web应用包 end --> 196 197 </dependencies> 198 <build> 199 <finalName>examination</finalName> 200 </build> 201 </project> View Code

  3,配置:web.xml

技术分享
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <!-- 配置前端控制器 -->
  <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--加载前端控制器配置文件 上下文配置位置-->
        <init-param>
             <!-- contextConfigLocation:指定springmvc配置的加载位置,如果不指定则默认加载WEB-INF/[DispatcherServlet的Servlet名字]-servlet.xml(例如springmvc-servlet.xml)。 -->
             <param-name>contextConfigLocation</param-name>
             <param-value>classpath:applicationContext.xml</param-value>
        </init-param>
        <!-- 表示随WEB服务器启动 -->
        <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
       <servlet-name>springmvc</servlet-name>
        <!-- 备注:可以拦截三种请求
         第一种:拦截固定后缀的url,比如设置为 *.do、*.action,例如:/user/add.action 此方法最简单,不会导致静态资源(jpg,js,css)被拦截.
         第二种:拦截所有,设置为/,例如:/user/add /user/add.action此方法可以实现REST风格的url,
                  很多互联网类型的应用使用这种方式.但是此方法会导致静态文件(jpg,js,css)被拦截后不能正常显示.需要特殊处理.
         第三种:拦截所有,设置为/*,此设置方法错误,因为请求到Action,当action转到jsp时再次被拦截,提示不能根据jsp路径mapping成功. -->
       <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>
View Code

 4,applicationContext.xml

技术分享
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "/WEB-INF/dtd/spring-beans-2.0.dtd">
3 <beans>
4     <import resource="datasource-spring.xml" />
5     <!-- servicemvc -->    
6     <import resource="springmvc.xml" />
7     <!-- spring -->    
8     <import resource="spring.xml" />
9 </beans> 
View Code

 5,jdbc.properties

技术分享
1 jdbc.driverClassName=com.mysql.jdbc.Driver
2 jdbc.url=jdbc:mysql://127.0.0.1:3306/yun
3 jdbc.username=root
4 jdbc.password=root
View Code

 6,datasource-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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
  5     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
  6     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  7                         http://www.springframework.org/schema/beans/spring-beans.xsd  
  8                         http://www.springframework.org/schema/context  
  9                         http://www.springframework.org/schema/context/spring-context.xsd  
 10                         http://www.springframework.org/schema/tx
 11                         http://www.springframework.org/schema/tx/spring-tx.xsd
 12                         http://www.springframework.org/schema/aop
 13                           http://www.springframework.org/schema/aop/spring-aop.xsd
 14                         http://www.springframework.org/schema/mvc  
 15                         http://www.springframework.org/schema/mvc/spring-mvc.xsd">
 16 
 17     <aop:aspectj-autoproxy />
 18 
 19     <tx:annotation-driven />
 20 <!-- <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "/WEB-INF/dtd/spring-beans-2.0.dtd"> -->
 21 <!-- <beans> -->
 22     <bean id="propertyConfigure"
 23         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
 24         <property name="locations">
 25             <list>
 26                 <value>classpath*:jdbc.properties</value>
 27             </list>
 28         </property>
 29     </bean>
 30 
 31     <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
 32         init-method="init" destroy-method="close">
 33         <property name="driverClassName" value="${jdbc.driverClassName}" />
 34         <property name="url" value="${jdbc.url}" />
 35         <property name="username" value="${jdbc.username}" />
 36         <property name="password" value="${jdbc.password}" />
 37         <!-- 配置初始化大小、最小、最大 -->
 38         <property name="initialSize" value="1" />
 39         <property name="minIdle" value="1" />
 40         <property name="maxActive" value="10" />
 41 
 42         <!-- 配置获取连接等待超时的时间 10秒 -->
 43         <property name="maxWait" value="10000" />
 44 
 45         <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
 46         <property name="timeBetweenEvictionRunsMillis" value="60000" />
 47 
 48         <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
 49         <property name="minEvictableIdleTimeMillis" value="300000" />
 50 
 51         <property name="testWhileIdle" value="true" />
 52 
 53         <!-- 这里建议配置为TRUE,防止取到的连接不可用 -->
 54         <property name="testOnBorrow" value="true" />
 55         <property name="testOnReturn" value="false" />
 56 
 57         <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
 58         <property name="poolPreparedStatements" value="true" />
 59         <property name="maxPoolPreparedStatementPerConnectionSize"
 60             value="20" />
 61 
 62         <!-- 这里配置提交方式,默认就是TRUE,可以不用配置 -->
 63 
 64         <property name="defaultAutoCommit" value="true" />
 65 
 66         <!-- 验证连接有效与否的SQL,不同的数据配置不同 -->
 67         <property name="validationQuery" value="select 1 from dual " />
 68         <property name="filters" value="log4j" />
 69         <property name="proxyFilters">
 70             <list>
 71                 <!-- web 监控方式 -->
 72                 <!-- <ref bean="stat-filter" /> -->
 73                 <!-- 日志文件监控方式 -->
 74                 <ref bean="logFilter" />
 75             </list>
 76         </property>
 77     </bean>
 78 
 79     <bean id="stat-filter" class="com.alibaba.druid.filter.stat.StatFilter">
 80         <property name="mergeSql" value="true" />
 81         <property name="slowSqlMillis" value="10000" />
 82         <property name="logSlowSql" value="true" />
 83     </bean>
 84 
 85     <bean id="logFilter" class="com.alibaba.druid.filter.logging.Slf4jLogFilter">
 86         <property name="statementExecutableSqlLogEnable" value="false" />
 87     </bean>
 88     
 89     <!-- jdbc事务管理器 -->
 90     <bean id="transactionManager"
 91         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
 92         <property name="dataSource">
 93             <ref bean="dataSource" />
 94         </property>
 95     </bean>
 96 <!-- 配置事物传播特性,(REQUIRED)是否存在事务,不存在则创建一个事务 -->
 97  <tx:advice id="txAdvice" transaction-manager="transactionManager">
 98         <tx:attributes>
 99             <tx:method name="save*" propagation="REQUIRED" />
100             <tx:method name="add*" propagation="REQUIRED" />
101             <tx:method name="create*" propagation="REQUIRED" />
102             <tx:method name="insert*" propagation="REQUIRED" />
103             <tx:method name="update*" propagation="REQUIRED" />
104             <tx:method name="merge*" propagation="REQUIRED" />
105             <tx:method name="del*" propagation="REQUIRED" />
106             <tx:method name="remove*" propagation="REQUIRED" />
107             <tx:method name="query*" read-only="true"/>
108             <tx:method name="use*" read-only="true"/>
109             <tx:method name="get*" read-only="true" />
110             <tx:method name="count*" read-only="true" />
111             <tx:method name="find*" read-only="true" />
112             <tx:method name="list*" read-only="true" />
113             <tx:method name="*" propagation="REQUIRED"/>
114         </tx:attributes>
115     </tx:advice>
116     
117     <!-- 切面,配置参与事务的类 -->
118     <aop:config >
119         <aop:pointcut id="allMethod" expression="execution(* com.datasource.service.impl.*.*(..))" />
120 <!--         <aop:pointcut id="mqserviceMethod" expression="execution(* com.midea.plms.*.mqservice.*.*(..))" /> -->
121 <!--         <aop:pointcut id="hessianMethod" expression="execution(* com.midea.plms.*.hessain.impl.*.*(..))" /> -->
122         
123         <aop:advisor advice-ref="txAdvice" pointcut-ref="allMethod" />
124 <!--         <aop:advisor advice-ref="txAdvice" pointcut-ref="mqserviceMethod" /> -->
125 <!--         <aop:advisor advice-ref="txAdvice" pointcut-ref="hessianMethod" /> -->
126         
127         <!-- <aop:aspect order="-2147483648" ref="readWriteDataSourceTransactionProcessor">
128            <aop:around pointcut-ref="allMethod" method="determineReadOrWriteDB"/>
129         </aop:aspect>
130         <aop:aspect order="-2147483647" ref="readWriteDataSourceTransactionProcessor">
131            <aop:around pointcut-ref="mqserviceMethod" method="determineReadOrWriteDB"/>
132         </aop:aspect>
133          <aop:aspect order="-2147483646" ref="readWriteDataSourceTransactionProcessor">
134            <aop:around pointcut-ref="hessianMethod" method="determineReadOrWriteDB"/>
135         </aop:aspect> -->
136     </aop:config>
137     
138     <!-- spring jdbc -->
139     <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
140         <property name="dataSource">
141             <ref bean="dataSource" />
142         </property>
143     </bean> 
144 </beans> 
View Code

  7,spring.xml

技术分享
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "/WEB-INF/dtd/spring-beans-2.0.dtd">
 3 <beans>
 4     <bean id="addTestOperator" class="com.datasource.operator.AddTestOperator">
 5         <property name="testService">
 6             <ref bean="testService" />
 7         </property>
 8     </bean>
 9     
10     <bean id="testService" class="com.datasource.service.TestServiceImp">
11         <property name="testDao">
12             <ref bean="testDao" />
13         </property>
14     </bean>
15     
16     <bean id="testDao" class="com.datasource.dao.TestDao">
17         <property name="jdbcTemplate">
18             <ref bean="jdbcTemplate" />
19         </property>
20     </bean>
21 </beans> 
View Code

 8,springmvc.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:context="http://www.springframework.org/schema/context"
 4     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans
 6        http://www.springframework.org/schema/beans/spring-beans.xsd
 7        http://www.springframework.org/schema/context
 8        http://www.springframework.org/schema/context/spring-context.xsd
 9        http://www.springframework.org/schema/tx
10        http://www.springframework.org/schema/tx/spring-tx.xsd
11           http://www.springframework.org/schema/mvc
12        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
13     <!-- 配置扫描的包 -->
14     <context:component-scan base-package="com.lizj" />
15 
16     <!-- 注册HandlerMapper、HandlerAdapter两个映射类 无需配置,默认使用默认的处理    -->
17     <!-- 扩充了注解驱动,可以将请求参数绑定到控制器参数 -->
18     <mvc:annotation-driven />
19 
20     <!-- 访问静态资源 -->
21     <mvc:default-servlet-handler />
22     
23     <!-- 视图解析器   <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> -->
24     <bean
25         class="org.springframework.web.servlet.view.InternalResourceViewResolver">
26         <property name="prefix" value="/view/"></property>
27         <property name="suffix" value=".jsp"></property>
28     </bean> 
29 </beans>  
View Code

 9,后台测试:

技术分享
 1 private JdbcTemplate jdbcTemplate;
 2     
 3     
 4     public JdbcTemplate getJdbcTemplate() {
 5         return jdbcTemplate;
 6     }
 7 
 8     public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
 9         this.jdbcTemplate = jdbcTemplate;
10     }
11 
12     public int addStudent(){
13         String sql = "INSERT INTO STUDENT(id,name,age) VALUES(?,?,?)";
14         List<Object> list = new ArrayList<Object>();
15         list.add(99);
16         list.add("3737");
17         list.add("22111");
18         return jdbcTemplate.update(sql, list.toArray());
19     }
20 
21 TestService:
22 
23 void addTest();
24 
25 AddTestOperator:
26 
27     Logger log = Logger.getLogger(getClass());
28     
29     private TestService testService;
30     
31     public TestService getTestService() {
32         return testService;
33     }
34 
35     public void setTestService(TestService testService) {
36         this.testService = testService;
37     }
38 
39     public void addTest(){
40         log.info("AddTestOperator.addTest");
41         testService.addTest(); 
42     }
43 
44 TestServiceImp:
45 
46     Logger log = Logger.getLogger(getClass());
47     
48     private TestDao testDao;
49 
50     public TestDao getTestDao() {
51         return testDao;
52     }
53 
54     public void setTestDao(TestDao testDao) {
55         this.testDao = testDao;
56     }
57 
58     public void addTest() {
59         log.info(getClass()+".addTest");
60         int i = testDao.addStudent();
61         System.out.println(i);
62     }
View Code

测试:JunitTest

技术分享
 @Test
    public void addStudent(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        AddTestOperator AddTestOperator = (com.datasource.operator.AddTestOperator) context.getBean("addTestOperator");
        AddTestOperator.addTest();
    }
View Code

 

druid数据库连接池

标签:qos   sed   存在   template   mysq   between   port   express   ble   

人气教程排行