SSM Integrated Mybatis and Druid

A, Mybatis Profile

  MyBatis is an excellent persistence framework, the process of its operation jdbc database encapsulation, so that developers only need to focus on SQL itself, without the need to spend energy to processing such as registration drive, create a connection, create a statement, manually set the parameters, retrieval result set jdbc complicated procedure code.

  MyBatis let the program will focus on the sql, by mapping the way mybatis offer the freedom and flexibility to generate (semi-automated, most programmers need to write sql) to meet the needs sql statement, more than Hibernate, NHibernate and EF heavyweight ORM framework the generated SQL statements to be funny lot.

  MyBatis input parameters will be preparedStatement automatic mapping input, the query result set into a flexible mapping of java object.

  Various statement (statement, preparedStatemnt, CallableStatement) Mybatis xml or annotation by way arranged to be performed together, and the final map generation sql statement executed by a java object and the sql statement, sql and executed by the Last frame mybatis mapped into java objects and return.

Two, Druid Introduction

  Druid is a database connection pool to achieve Alibaba open-source platform, which combines the advantages of C3P0, DBCP, PROXOOL such as DB pool, while adding log monitoring, can be a good monitor implementation DB connection pool and SQL can be said for surveillance born DB connection pool, it said to be the best connection pool.

Third, the project configuration

1、db.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
jdbc.username=root
jdbc.password=yan748712

2、springMVC-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd ">

    <context:component-scan base-package="com.ework.upms.server" />

    <mvc:annotation-driven />

    <mvc:resources location="/static/" mapping="/static/**" />

    <!-- 模板解析器  -->
    <bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
        <property name="prefix" value="/WEB-INF/templates/" />
        <property name="suffix" value=".html" />
        <property name="templateMode" value="HTML5" />
        <property name="cacheable" value="false" />
        <property name="characterEncoding" value="UTF-8"/>
    </bean>

    <bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
        <property name="templateResolver" ref="templateResolver" />
    </bean>

    <bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
        <property name="templateEngine" ref="templateEngine" />
        <property name="characterEncoding"  value="UTF-8" />
    </bean>



    <bean id="propertyConfigure"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath*:db.properties</value>
            </list>
        </property>
    </bean>

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
          init-method="init" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <!-- 配置初始化大小、最小、最大 -->
        <property name="initialSize" value="1" />
        <property name="minIdle" value="1" />
        <property name="maxActive"configuration acquiring connection waiting timeout<-!/>= "10"value 

        
        = "maxWait"nameProperty<->value = "10000"  /> 

        <-! intervals the frequency of such detection, an idle connection is detected to be closed, in milliseconds -> 
        < Property name = "timeBetweenEvictionRunsMillis" value = "60000"  /> 

        <! - a connector disposed in the survival time of the minimum pool milliseconds -> 
        < Property name = "minEvictableIdleTimeMillis" value = "300000"  /> 

        < Property name = "testWhileIdle" value = "to true"  /> 

        <-! here recommended configuration is TRUE, taken to prevent the connection is not available -> 
        < Property name= "testOnBorrow" value = "to true"  /> 
        < Property name = "testOnReturn" value = "to false"  /> 

        <-! open PSCache, and specifies the size of each connection PSCache -> 
        < Property name = "poolPreparedStatements " value =" to true "  /> 
        < Property name =" maxPoolPreparedStatementPerConnectionSize " 
                  value =" 20 "  /> 

        <-! here presented in a way to configure the default is TRUE, you can not configure -> 
        < Property name =" defaultAutoCommit " value="true" />

        <!--Verify connection is valid or not SQL, different configuration different data -> 
        < Property name = "validationQuery" value = "SELECT. 1"  /> 
        < Property name = "Filters" value = "STAT"  /> 
    </ the bean > 
< / Beans >

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 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">
    <display-name>upms-server</display-name>

    <!-- springMVC的核心控制器 -->
    <servlet>
        <servlet-name>springMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:springMVC-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springMVC</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- Spring -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:springMVC-servlet.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>


    <!-- druid -->
    <servlet>
        <servlet-name>DruidStatView</servlet-name>
        <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>DruidStatView</servlet-name>
        <url-pattern>/druid/*</url-pattern>
    </servlet-mapping>
    <filter>
        <filter-name>druidWebStatFilter</filter-name>
        <filter-class>com.alibaba.druid.support.http.WebStatFilter</filter-class>
        <init-param>
            <param-name>exclusions</param-name>
            <param-value>/public/*,*.js,*.css,/druid*,*.jsp,*.swf </param-value>
        </init-param>
        <init-param>
            <param-name>principalSessionName</param-name>
            <param-value>sessionInfo</param-value>
        </init-param>
        <init-param>
            <param-name>profileEnable</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>druidWebStatFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


</web-app>

 

4, the parent pom.xml

<!--数据库相关-->
<mybatis.version>3.4.6</mybatis.version>
<mybatis-spring.version>1.3.1</mybatis-spring.version>
<mysql.version>5.1.40</mysql.version>
<druid.version>1.0.29</druid.version>
 <!-- Mybatis -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>${mybatis.version}</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql.version}</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>${druid.version}</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>${mybatis-spring.version}</version>
            </dependency>

 

5, pom.xml

    <!-- Mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
        </dependency>

Third, run renderings

 

 

 

 Done

 

Guess you like

Origin www.cnblogs.com/yansg/p/11323025.html