SpringMVC+JDBC+Druid

  • Druid GitHub:https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98
  • 工具:idea  

     步骤:

SpringMVC+JDBC+Druid_第1张图片

 

SpringMVC+JDBC+Druid_第2张图片

这里也可以选择自己安装的maven

SpringMVC+JDBC+Druid_第3张图片

 next然后填写项目名然后 finish

 项目构建中...SpringMVC+JDBC+Druid_第4张图片

ok,项目构建完成,如下图所示。左边是项目构建完成的目录结构 ,右边是maven配置文件,只有一个junit。

SpringMVC+JDBC+Druid_第5张图片

下面开始编辑配置文件 引入我们需要的库

1  
2     <properties>
3         <junit.version>3.8.1junit.version>
4         <spring.version>4.1.6.RELEASEspring.version>
5         <mysql.connector.java.version>5.1.26mysql.connector.java.version>
6         <druid.version>1.0.7druid.version>
7         <org.slf4j.log4j12.version>1.7.5org.slf4j.log4j12.version>
8     properties>

 

 标签上面插入上面的代码,这是库的版本,当然也可以省略这一步骤,下面的库中直接填写版本。

 1  
 2         <dependency>
 3             <groupId>org.slf4jgroupId>
 4             <artifactId>slf4j-log4j12artifactId>
 5             <version>${org.slf4j.log4j12.version}version>
 6         dependency>
 7         
 8         <dependency>
 9             <groupId>org.springframeworkgroupId>
10             <artifactId>spring-webmvcartifactId>
11             <version>${spring.version}version>
12         dependency>
13         
14         <dependency>
15             <groupId>org.springframeworkgroupId>
16             <artifactId>spring-jdbcartifactId>
17             <version>${spring.version}version>
18         dependency>
19 
20         
21         <dependency>
22             <groupId>mysqlgroupId>
23             <artifactId>mysql-connector-javaartifactId>
24             <version>${mysql.connector.java.version}version>
25         dependency>
26 
27         
28         <dependency>
29             <groupId>com.alibabagroupId>
30             <artifactId>druidartifactId>
31             <version>${druid.version}version>
32         dependency>

 

在  中插入上面的代码 引入这几个库功能不多 暂时引用这几个就可以了。

打开maven视图,然后刷新,这个过程可能会很慢,等待库下载完毕。如果idea没有视图在左下角找一下

SpringMVC+JDBC+Druid_第6张图片

下载完毕后,可以看到多了很多包

SpringMVC+JDBC+Druid_第7张图片

在resource文件夹下新建三个xml文件 分别是 spring-druid.xml   spring-jdbc.xml  spring-mvc.xml 和两个 properties文件 分别是config.properties和 log4g.properties

SpringMVC+JDBC+Druid_第8张图片

 

 config.properties

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

 

 log4j.properties

 

 1  ### \u8BBE\u7F6E###
 2 log4j.rootLogger = info,stdout,logfile
 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

 

 spring-druid.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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    
    <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
          init-method="init" destroy-method="close">
        
        <property name="url" value="${jdbc_url}" />
        <property name="username" value="${jdbc_username}" />
        <property name="password" value="${jdbc_password}" />

        
        <property name="initialSize" value="2" />
        
        <property name="maxActive" value="20" />
        
        <property name="minIdle" value="0" />
        
        <property name="maxWait" value="60000" />

        
        

        
        <property name="validationQuery" value="${validationQuery}" />
        
        <property name="testWhileIdle" value="true" />
        
        <property name="testOnBorrow" value="false" />
        
        <property name="testOnReturn" value="false" />

        
        <property name="timeBetweenEvictionRunsMillis" value="60000" />
        
        <property name="minEvictableIdleTimeMillis" value="25200000" />

        
        <property name="removeAbandoned" value="true" />
        
        <property name="removeAbandonedTimeout" value="1800" />
        
        <property name="logAbandoned" value="true" />

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

    
    <bean id="druid-stat-interceptor"
          class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor" />

    <bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut"
          scope="prototype">
        <property name="patterns">
            <list>
                <value>spring.druid.service.*value>
            list>
        property>
    bean>
    <aop:config>
        <aop:advisor advice-ref="druid-stat-interceptor"
                     pointcut-ref="druid-stat-pointcut" />
    aop:config>

beans>

 

 spring-jdbc.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="jdbcTemplate " class="org.springframework.jdbc.core.JdbcTemplate">
        <constructor-arg ref="dataSource"/>
    bean>

beans>

 

spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"
       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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


    
    <bean
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:config.propertiesvalue>
            list>
        property>
    bean>

    <import resource="spring-druid.xml" />
    <import resource="spring-jdbc.xml" />

    
    <mvc:annotation-driven />

    
    <context:component-scan base-package="com.kangyaba">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    context:component-scan>
    
    <context:annotation-config/>


    
    <bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        
        <property name="prefix" value="/WEB-INF/pages/" />
        
        <property name="suffix" value=".jsp" />
    bean>


beans>

 

 

 

 新建如下图所示文件

SpringMVC+JDBC+Druid_第9张图片

 

IUserService:

 1 package com.kangyaba.user;
 2 
 3 import org.springframework.stereotype.Service;
 4 
 5 import java.util.Map;
 6 
 7 /**
 8  * Created by Z on 2016/12/22.
 9  */
10 @Service
11 public interface IUserService {
12     Map findUser();
13 }

 

 UserCtrl:

 1 package com.kangyaba.user;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.stereotype.Controller;
 5 import org.springframework.ui.Model;
 6 import org.springframework.web.bind.annotation.RequestMapping;
 7 import org.springframework.web.bind.annotation.RequestMethod;
 8 
 9 /**
10  * Created by Z on 2016/12/22.
11  */
12 @Controller
13 public class UserCtrl {
14     @Autowired
15     IUserService iUserService;
16 
17     @RequestMapping(value = "/finduser", method = RequestMethod.GET)
18     public String findUser(Model model) {
19         model.addAttribute("user_nickname", iUserService.findUser().get("nickname"));
20         return "user";
21     }
22 }

 

 

 

UserServiceImpl: 

 1 package com.kangyaba.user;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.jdbc.core.JdbcTemplate;
 5 import org.springframework.stereotype.Component;
 6 
 7 import java.util.Map;
 8 
 9 /**
10  * Created by Z on 2016/12/22.
11  */
12 @Component
13 public class UserServiceImpl implements IUserService {
14     @Autowired
15     JdbcTemplate jdbcTemplate;
16 
17     public Map findUser() {
18         Map map = jdbcTemplate.queryForMap("select * from USER where id='1'");
19         return map;
20     }
21 }

 

 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 version="3.0" 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">
  <display-name>Archetype Created Web Applicationdisplay-name>

  
  <context-param>
    <param-name>contextConfigLocationparam-name>
    <param-value>classpath*:spring-mvc.xmlparam-value>
  context-param>

  
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
  listener>
  <listener>
    <listener-class>org.springframework.web.util.IntrospectorCleanupListenerlistener-class>
  listener>

  
  <servlet>
    <servlet-name>springMvcservlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    <init-param>
      <param-name>contextConfigLocationparam-name>
      <param-value>classpath:spring-mvc.xmlparam-value>
    init-param>
    <load-on-startup>1load-on-startup>
    <async-supported>trueasync-supported>
  servlet>
  <servlet-mapping>
    <servlet-name>springMvcservlet-name>
    <url-pattern>/url-pattern>
  servlet-mapping>

  
  <filter>
    <filter-name>druidWebStatFilterfilter-name>
    <filter-class>com.alibaba.druid.support.http.WebStatFilterfilter-class>
    <init-param>
      <param-name>exclusionsparam-name>
      <param-value>/assets/*,*.css,*.js,*.gif,*.jpg,*.png,*.ico,*.eot,*.svg,*.ttf,*.woff,*.jsp,*.tpl,/druid/*param-value>
    init-param>
  filter>
  <filter-mapping>
    <filter-name>druidWebStatFilterfilter-name>
    <url-pattern>/*url-pattern>
  filter-mapping>

  <servlet>
    <servlet-name>druidStatViewservlet-name>
    <servlet-class>com.alibaba.druid.support.http.StatViewServletservlet-class>
  servlet>
  <servlet-mapping>
    <servlet-name>druidStatViewservlet-name>
    <url-pattern>/druid/*url-pattern>
  servlet-mapping>


  
  <context-param>
    <param-name>log4jConfigLocationparam-name>
    <param-value>classpath:log4j.propertiesparam-value>
  context-param>
  <context-param>
    <param-name>webAppRootKeyparam-name>
    <param-value>spring-druid.root param-value>
  context-param>
  <listener>
    <listener-class>org.springframework.web.util.Log4jConfigListenerlistener-class>
  listener>

  
  <filter>
    <filter-name>encodingFilterfilter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
    <init-param>
      <param-name>encodingparam-name>
      <param-value>UTF-8param-value>
    init-param>
  filter>
  <filter-mapping>
    <filter-name>encodingFilterfilter-name>
    <url-pattern>/*url-pattern>
  filter-mapping>
web-app>

 

 user.jsp

<%--
  Created by IntelliJ IDEA.
  User: Z
  Date: 2016/12/22
  Time: 15:22
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Titletitle>
head>
<body>
<h2>
用户昵称:${user_nickname}
h2>
body>
html>

 

 至此,已经完成了一个springmvc demo,tomcat配置省略,下面看一下运行情况:

druid:

 SpringMVC+JDBC+Druid_第10张图片

 

转载于:https://www.cnblogs.com/6324/p/6211686.html

你可能感兴趣的:(SpringMVC+JDBC+Druid)