zoukankan      html  css  js  c++  java
  • Spring MVC+Maven+Freemarker+Mybatis开发环境搭建

     
     

    利用STS(Spring Tool Suite)开发工具可以很方便的创建一个基于Maven的Spring MVC工程项目。然后再分别集成Freemarker以及Mybatis。 
    STS官网下载地址:点此下载Spring Tool Suite 运行STS后, 
    设置下工作空间的默认编码方式为UTF-8,Window->Preferences->General->Workspace。 
    配置下JRE,选择你自己安装的JDK(如1.8的版本),Window->Preferences->Java->Installed JREs 。 
    配置Maven本地仓库位置,默认是C盘路径,Window->Preferences->Maven->User Settings,可以自己安装个Maven,若嫌麻烦则默认用STS自带的即可不用配置。

    1.创建一个Spring MVC项目

    File->New->Spring Legacy Project,弹出向导后输入项目名称并选择Spring MVC Project,然后Next, 
    输入包名,Finish。第一次创建会提示下载模板文件,点下载即可,创建好的工程目录如下: 
    创建Spring MVC工程 
    运行项目Run as -> Run on Server ,可以用Tomcat 作为服务器,自己自行配置。如果项目有错误,请检查Build Path 以及pom.xml中的jar包是否在maven本地仓库中下载好了。 
    运行成功后,会打印出Hello world 以及服务器的时间,时间若是乱码,在jsp中 加上charset=utf-8即可。

    2.集成Freemarker

    表现层技术主要有三种:jsp、freemarker、velocity。 
    其实集成Freemarker是可选的,jsp就很好了。三者优缺点都有,看个人喜好吧。 
    1)在pom.xml中添加Freemarker 依赖:

            <!-- freemarker 依赖于 spring-context-support-->
            <dependency>
                <groupId>org.freemarker</groupId>
                <artifactId>freemarker</artifactId>
                <version>2.3.23</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context-support</artifactId>
            </dependency>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    2)找到servlet-context.xml,添加Freemarker的配置 
    为了看起来更简洁,beans和mvc的命名空间前缀调换了下,自己可以对照着更改。 
    文件在最后列出来了,在此不重复列出

    Freemarker配置中的视图后缀可以是任意的,默认的是.ftl . 
    如果要配置多个视图解析器,如同时配置jsp和Freemarker,那么Freemarker的优先级要设置的比jsp的高,通过order来设置。 
    很简单,就这两步,配置完后,运行成功则表示Freemarker配置成功。

    3.集成Mybatis

    实体映射框架可能会选择Hibernate,个人觉得Hibernate难学点,特别是那些对应关系。而Mybatis是半自动映射框架,灵活性高,学起来也简单。 
    1)依赖jar

     <!-- mybatis -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.2.6</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>1.2.2</version>
            </dependency>  
            <!-- microsoft jdbc -->
            <dependency>
                <groupId>net.sourceforge.jtds</groupId>
                <artifactId>jtds</artifactId>
                <version>1.3.1</version>
            </dependency>
            <!-- 事务 -->
            <dependency>  
                <groupId>org.springframework</groupId>  
                <artifactId>spring-jdbc</artifactId>  
            </dependency>  
            <dependency>  
                <groupId>org.springframework</groupId>  
                <artifactId>spring-tx</artifactId>  
            </dependency>  
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjweaver</artifactId>
                <version>${org.aspectj-version}</version>
            </dependency>
            <!-- 导入dbcp的jar包 -->  
            <dependency>  
                <groupId>commons-dbcp</groupId>  
                <artifactId>commons-dbcp</artifactId>  
                <version>1.4</version>  
            </dependency>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    驱动是Sqlserver的,自己可以替换成其他的 
    2)jdbc.properties

    hibernate.dialect=org.hibernate.dialect.SQLServerDialect
    driverClassName=net.sourceforge.jtds.jdbc.Driver
    validationQuery=SELECT 1
    jdbc_url=jdbc:jtds:sqlserver://127.0.0.1:1433/PTMPDB
    jdbc_password=12345
    jdbc_username=part_user
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    3)spring-mybatis.xml 
    文件在后面列举出来了,在此不重复列举

    4)最后别忘了将spring-mytabis.xml 让spring容器加载进去,在web.xml中加入:

    <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/root-context.xml,classpath:spring-mybatis.xml</param-value>
        </context-param>
    • 1
    • 2
    • 3
    • 4

    至此配置已经完成了,最后还得测试下是否集成成功。

    4.Mybatis自动生成工具

    可以根据数据库表自动生成dao、mapping、entity。Mybatis实体映射自动生成工具下载 
    运行项目,看是否能成功连接数据库并且能查出数据以验证是否配置成功。 
    目录结构图 
    1)pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.jykj</groupId>
        <artifactId>demo</artifactId>
        <name>Demo</name>
        <packaging>war</packaging>
        <version>1.0.0-BUILD-SNAPSHOT</version>
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <java-version>1.6</java-version>
            <org.springframework-version>4.1.2.RELEASE</org.springframework-version>
            <org.aspectj-version>1.6.10</org.aspectj-version>
            <org.slf4j-version>1.6.6</org.slf4j-version>
        </properties>
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-framework-bom</artifactId>
                    <version>${org.springframework-version}</version>
    
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
        <dependencies>
            <!-- Spring -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <exclusions>
                    <!-- Exclude Commons Logging in favor of SLF4j -->
                    <exclusion>
                        <groupId>commons-logging</groupId>
                        <artifactId>commons-logging</artifactId>
                     </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
            </dependency>
            <!-- AspectJ -->
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjrt</artifactId>
                <version>${org.aspectj-version}</version>
            </dependency>   
    
            <!-- Logging -->
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>${org.slf4j-version}</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>jcl-over-slf4j</artifactId>
                <version>${org.slf4j-version}</version>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
                <version>${org.slf4j-version}</version>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.15</version>
                <exclusions>
                    <exclusion>
                        <groupId>javax.mail</groupId>
                        <artifactId>mail</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>javax.jms</groupId>
                        <artifactId>jms</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>com.sun.jdmk</groupId>
                        <artifactId>jmxtools</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>com.sun.jmx</groupId>
                        <artifactId>jmxri</artifactId>
                    </exclusion>
                </exclusions>
                <scope>runtime</scope>
            </dependency>
    
            <!-- @Inject -->
            <dependency>
                <groupId>javax.inject</groupId>
                <artifactId>javax.inject</artifactId>
                <version>1</version>
            </dependency>
    
            <!-- Servlet -->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>servlet-api</artifactId>
                <version>2.5</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>javax.servlet.jsp</groupId>
                <artifactId>jsp-api</artifactId>
                <version>2.1</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
            </dependency>
    
            <!-- Test -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.7</version>
                <scope>test</scope>
            </dependency>  
            <!-- freemarker 依赖于 spring-context-support-->
            <dependency>
                <groupId>org.freemarker</groupId>
                <artifactId>freemarker</artifactId>
                <version>2.3.23</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context-support</artifactId>
            </dependency>
             <!-- mybatis -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.2.6</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>1.2.2</version>
            </dependency>  
            <!-- microsoft jdbc -->
            <dependency>
                <groupId>net.sourceforge.jtds</groupId>
                <artifactId>jtds</artifactId>
                <version>1.3.1</version>
            </dependency>
            <!-- 事务 -->
            <dependency>  
                <groupId>org.springframework</groupId>  
                <artifactId>spring-jdbc</artifactId>  
            </dependency>  
            <dependency>  
                <groupId>org.springframework</groupId>  
                <artifactId>spring-tx</artifactId>  
            </dependency>  
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjweaver</artifactId>
                <version>${org.aspectj-version}</version>
            </dependency>
            <!-- 导入dbcp的jar包 -->  
            <dependency>  
                <groupId>commons-dbcp</groupId>  
                <artifactId>commons-dbcp</artifactId>  
                <version>1.4</version>  
            </dependency>
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-eclipse-plugin</artifactId>
                    <version>2.9</version>
                    <configuration>
                        <additionalProjectnatures>
                            <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
                        </additionalProjectnatures>
                        <additionalBuildcommands>
                            <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
                        </additionalBuildcommands>
                        <downloadSources>true</downloadSources>
                        <downloadJavadocs>true</downloadJavadocs>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>2.5.1</version>
                    <configuration>
                        <source>1.6</source>
                        <target>1.6</target>
                        <compilerArgument>-Xlint:all</compilerArgument>
                        <showWarnings>true</showWarnings>
                        <showDeprecation>true</showDeprecation>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <version>1.2.1</version>
                    <configuration>
                        <mainClass>org.test.int1.Main</mainClass>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216

    2)web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" 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_2_5.xsd">
    
        <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/root-context.xml,classpath:spring-mybatis.xml</param-value>
        </context-param>
    
        <!-- Creates the Spring Container shared by all Servlets and Filters -->
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    
        <!-- Processes application requests -->
        <servlet>
            <servlet-name>appServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>appServlet</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
        <!-- 编码格式 -->
        <filter>
            <filter-name>encodingFilter</filter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
            <init-param>
                <param-name>encoding</param-name>
                <param-value>UTF-8</param-value>
            </init-param>
             <init-param>
                  <param-name>forceEncoding</param-name>
                  <param-value>true</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>encodingFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    
    
        <!-- 日志配置文件 -->
        <context-param>
          <param-name>log4jConfigLocation</param-name>
          <param-value>classpath:log4j.xml</param-value>
        </context-param>
        <listener>
          <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
        </listener>
    
    
    </web-app>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62

    3)servlet-context.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
        <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
    
        <!-- Enables the Spring MVC @Controller programming model -->
        <mvc:annotation-driven />
    
        <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
        <mvc:resources mapping="/resources/**" location="/resources/" />
    
        <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/views/" />
            <property name="suffix" value=".jsp" />
            <property name="order" value="2" />
        </bean>
        <!-- freemarker的配置 -->  
        <bean id="freemarkerConfigurer"  
            class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">  
            <property name="templateLoaderPath" value="/WEB-INF/views/" />  
            <property name="defaultEncoding" value="utf-8" />  
            <property name="freemarkerSettings">  
                <props>  
                    <prop key="template_update_delay">5</prop>
                    <prop key="default_encoding">UTF-8</prop>
                   <!--  <prop key="locale">UTF-8</prop> -->
                    <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
                    <prop key="time_format">HH:mm:ss</prop>
                    <prop key="number_format">0.####</prop>
                    <prop key="boolean_format">true,false</prop>
                    <prop key="whitespace_stripping">true</prop>
                    <prop key="tag_syntax">auto_detect</prop>
                    <prop key="url_escaping_charset">UTF-8</prop>
                </props>  
            </property>  
        </bean> 
        <!-- FreeMarker视图解析 ,优先级要高于默认的jsp-->  
        <bean id="viewResolver"
            class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
            <property name="order" value="1" />
            <property name="cache" value="true" />
            <property name="prefix" value="" />
            <property name="suffix" value=".ftl" />
            <property name="contentType" value="text/html;charset=UTF-8"></property>
            <property name="requestContextAttribute" value="request" />
            <property name="exposeSpringMacroHelpers" value="true" />
            <property name="exposeRequestAttributes" value="true" />
            <property name="exposeSessionAttributes" value="true" />
        </bean>
        <!-- 控制器 -->
        <context:component-scan base-package="com.jykj.demo.controller" />
        <!-- service -->
        <context:component-scan base-package="com.jykj.demo.service" />
    </beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62

    4)spring-mybatis.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:tx="http://www.springframework.org/schema/tx"
        xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        ">
    
        <!-- 
        <context:component-scan base-package="com.jykj.demo" />   -->
        <!-- 引入配置文件 -->  
        <bean id="propertyConfigurer"  
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
            <property name="location" value="classpath:jdbc.properties" />  
        </bean>  
        <!-- 配置数据源 -->
        <bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
             destroy-method="close">
            <property name="driverClassName" value="${driverClassName}"/>
            <property name="url" value="${jdbc_url}" />
            <property name="username" value="${jdbc_username}" />
            <property name="password" value="${jdbc_password}" />
    
            <!-- 初始化连接大小 -->
            <property name="initialSize" value="0" />
            <!-- 连接池最大使用连接数量 -->
            <property name="maxActive" value="20" />
            <!-- 连接池最小空闲 -->
            <property name="minIdle" value="0" />
            <!-- 获取连接最大等待时间 -->
            <property name="maxWait" value="60000" />
    
            <property name="poolPreparedStatements" value="true" /> 
            <!-- <property name="maxPoolPreparedStatementPerConnectionSize" value="33" /> -->
    
            <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
            <property name="timeBetweenEvictionRunsMillis" value="60000" />
            <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
            <property name="minEvictableIdleTimeMillis" value="25200000" />
    
            <!-- 打开removeAbandoned功能 -->
            <property name="removeAbandoned" value="true" />
            <!-- 1800秒,也就是30分钟 -->
            <property name="removeAbandonedTimeout" value="1800" />
            <!-- 关闭abanded连接时输出错误日志 -->
            <property name="logAbandoned" value="true" />
    
            <!-- 监控数据库 -->
            <!-- <property name="filters" value="mergeStat" /> -->
        </bean>
    
        <!-- myBatis文件 -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
            <property name="mapperLocations" value="classpath:com/jykj/demo/mapping/*.xml" />
            <property name="typeAliasesPackage" value="com.jykj.demo.entity" />
        </bean>
    
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.jykj.demo.dao" />
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
        </bean>
    
        <!-- 配置事务管理器 -->
        <bean id="transactionManager"
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource" />
        </bean>
    
        <!-- 注解方式配置事物 -->
        <!-- <tx:annotation-driven transaction-manager="transactionManager" /> -->
    
        <!-- 拦截器方式配置事物 -->
        <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <tx:method name="add*" propagation="REQUIRED" rollback-for="Exception" />
                <tx:method name="append*" propagation="REQUIRED"
                    rollback-for="Exception" />
                <tx:method name="insert*" propagation="REQUIRED"
                    rollback-for="Exception" />
                <tx:method name="save*" propagation="REQUIRED"
                    rollback-for="Exception" />
                <tx:method name="update*" propagation="REQUIRED"
                    rollback-for="Exception" />
                <tx:method name="modify*" propagation="REQUIRED"
                    rollback-for="Exception" />
                <tx:method name="edit*" propagation="REQUIRED"
                    rollback-for="Exception" />
                <tx:method name="delete*" propagation="REQUIRED"
                    rollback-for="Exception" />
                <tx:method name="remove*" propagation="REQUIRED"
                    rollback-for="Exception" />
                <tx:method name="repair" propagation="REQUIRED"
                    rollback-for="Exception" />
                <tx:method name="delAndRepair" propagation="REQUIRED"
                    rollback-for="Exception" />
    
                <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
                <tx:method name="select*" propagation="SUPPORTS" read-only="true" />
                <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
                <tx:method name="load*" propagation="SUPPORTS" read-only="true" />
                <tx:method name="search*" propagation="SUPPORTS" read-only="true" />
                <tx:method name="datagrid*" propagation="SUPPORTS"
                    read-only="true" />
    
                <tx:method name="*" propagation="SUPPORTS" rollback-for="Exception" />
            </tx:attributes>
        </tx:advice>
        <!-- 切面注入事务 -->
        <aop:config>
            <aop:pointcut id="transactionPointcut" expression="execution(* com.jykj.demo.service.*.*(..))" />
            <aop:advisor pointcut-ref="transactionPointcut"
                advice-ref="transactionAdvice" />
        </aop:config>
    </beans>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123

    总的来说,集成的话就是spring的配置。如果集成出现问题,请不要灰心,找出原因,多半是pom.xml中的jar包没有成功下载下来,这样的话需要手动下载后放到maven本地仓库中。 
    也可以参考其他人的博客: 
    参考链接一 
    参考链接二 
    参考链接三 
    Mybatis实体映射自动生成工具下载

  • 相关阅读:
    秀秀我的PENTAX的手动标头
    软件测试工具
    071226 冷
    071228晴转雨
    长假计划
    Merry Christmas
    信宿事件
    並べ替えーーバブルソート等
    收藏
    实践浪漫
  • 原文地址:https://www.cnblogs.com/yhtboke/p/5749105.html
Copyright © 2011-2022 走看看