Spring + SpringMVC + Mybatis

maven dependency”

<?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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.zhanglu</groupId>
  <artifactId>honor</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>honor Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <!--spring -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>4.3.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>4.3.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>4.3.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>4.3.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>4.3.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>4.3.7.RELEASE</version>
      <scope>test</scope>
    </dependency>


    <!-- mybatis-->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.4.5</version>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.3.1</version>
    </dependency>
    <dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper</artifactId>
      <version>5.1.2</version>
    </dependency>


    <!--drud -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.0.18</version>
    </dependency>

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.30</version>
    </dependency>

    <!--Log -->< dependency>< groupId> org.slf4j< /groupId>< artifactId≫ slf4j-api< /artifactId>< version> 1.7.25< /version>< /dependency>< dependency>< groupId> log4j< /groupId>< artifactId> log4j< /ArtifactId>< version> 1.2.17< /version>< /dependency>< dependencY>< groupId> org.slf4j< /groupId>< artifactId> slf4j-log4j12< /artifActId>< version> 1.7.21< /version>< /dependency><! --CGLIG --&gT;< dependency>< groupId> cglib< /groupId>< artifactId> cglib&lT; /artifactId>< version> 3.2.5< /version>< /dependency>< /dePendencies>< build>< finalName> honor< /finalName>< pluginManagemenT> <! - lock down plugins versions to avoid using Maven defaults (Maven *->< plugins>< plugin>< artifactId> maven-clean-plugin< /artifactId>< version> 3.0.0< /version>< /plugin><! - see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->< plugin>< artifactId> maven-resources-plugin< /artiFactId>< version> 3.0.2< /version>< /plugin>< plugin>< artifactId> maven-compiler-plugin< /artifactId>< version> 3.7.0< /version>< /plugin>< plugin>< artifactId> maveN-surefire-plugin< /artifactId>< version> 2.20.1< /version>< /plUgin>< plugin>< artifactId> maven-war-plugin< /artifactId>< version> 3.2.0< /version>< /plugin>< plugin>< artifactId> maven-install-plugin< /artifactId>< version> 2.5.2< /version>< /plugin>< plugin>< artifactId> maven-deploy-plugin≪ /artifactId>< version> 2.8.2< /version>< /plugin></plugins>< /pluginManagement>< /build>< /project>

spring-context.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:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xsi:schemaLocation="
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd
    http://www.springframework.org/schema/jdbc
    http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
    http://www.springframework.org/schema/cache
    http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util.xsd">


    <context:component-scan base-package="com.zhanglu">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <!--The jdbc.properties configuration file --> is introduced.< bean id= "propertyConfigurer" class= "org.springframework.beans.f"Actory.config.PropertyPlaceholderConfigurer ">< property name= "location" value= "jdbc.pr"Operties "/>< /bean><! --dataSource -->< bean id= "dataSource" class= "Com.alibaba.druid.pool.DruidDataSource ">< property name= "driverClassName" value= "${jdb"C.driverClassName} "/>< property name= "URL" value= "${jdbc.url}" />< properTy name= "username" value= "${jdbc.username}" />< property name= "password" value= "${jdbc."Password} "/>< property name= "validationQuery" value= "${jdbc.validationQuery}" />< /bean><! - mybatis-->< bean id= "sqlSessionFactory" class= "org.mybatis.spri"Ng.SqlSessionFactoryBean "P:dataSource-ref= "dataSource"P:configLocation= "classpAth:mybatis-config.xml "P:mapperLocations= "classpath:mapper/*.xml" /><!! - scanning basAll interfaces with @MyBatisDao annotation under ePackage are -->< bean id= "mapperScannerConfigurer" class= "org.mybatis.spr"Ing.mapper.MapperScannerConfigurer ">< property name= "sqlSessionFactoryBeanName" value="SqlSessionFactory" />< property name= "basePackage" value= "com.zhanglu" />&lT; property name= "annotationClass" value= "com.zhanglu.annotation.MyBatisDao" />< /bean><! - --> in dataSource data source transaction management.< bean id= "transactionManager" class= "org.springframewo"Rk.jdbc.datasource.DataSourceTransactionManager "P:dataSource-ref= "dataSource" />&lT;! - enable --> support for transaction annotations.< tx:annotation-driven transaction-manager= "transactionManager" /><! - transaction behavior is configured -->< tx:advice id= "txAdvice" transaction-manager= "transactionManager" >< tx:attributes>< tx:method name= "save*" propagation= "REQUIRED" />< tx:method name= "delete*" propagation= "REQUIRED" />< tx:method name= "insErt* "propagation=" REQUIRED "/>";< tx:method name= "update*" propagation= "REQUIRED" /&gT;< tx:method name= "find*" propagation= "SUPPORTS" read-only= "true" />≪ tx:method name= "get*" propagation= "SUPPORTS" read-only= "true" />< tx:method name= "select*" propagation= "SUPPORTS" read-only= "true" />< /tx:attributes>< /tx:aDvice>< /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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="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-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <mvc:annotation-driven/>
    <context:component-scan base-package="com.zhanglu.controller"/>

    <!--Configure view parser -->< bean class= "org.springframework.web.servlet.view.InternalResourceViewResolver">< property name= "prefix" value= "/WEB-INF/views/" />< property name= "sufFix "value=".Jsp "/>";< /bean><!! -- configuring view parser: parsing view --> with the name of the view;< bean class= "Org.springframework.web.servlet.view.BeanNameViewResolver ">< property name= "order" valUe= "100" />< /bean>< /beans>

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>

        <!--Logging implementation, built-in SLF4J, COMMONS_LOGGING, LOG4J, LOG4J2, JDK_LOGGING, STDOUT_LOGGING, NO_LOGGING,Press without specifyingTry it in a certain order until you get it, so you can refer to LogFactory -->.< setting name= "logImpl" value= "LOG4J" />< setting name= "cacheEnabled" value= "true" />< setting name= "proxyFactory" valUe= "CGLIB" />< setting name= "lazyLoadingEnabled" value= "true" /><!!Whether the proxy object method loads all the attributes of lazy loaded objects at the same time, the default is true -->< setting name= "aggressiveLazyLoading" value= "FasL"E "/>The default method that triggers the loading of all attributes of an object for lazy loading is the following four methods - - gt;< setting name= "lazyLoadTrigGerMethods "value=" equals, clone, hashCode, toString "/>";<!! -- automatic mapping type with optional values of NONE, PARTIAL and FUL.L, refer to AutoMappingBehavior enumeration -->< setting name= "autoMappingBehavior" value= "PARTIAL" /><!! - the default Executor type, value reference ExecutorType enumeration -->< setting name= "defaultExecutorType "value=" SIMPLE "/>";<!! - the default timeout time is in seconds. The default is null, not timeout -->< setting name= "defaultStatementTimeout" value= "10" />< setting name= "localCacheScope" value= "SESSIO"N "/>< /settings><!! - automatically scan the type under com.how2java.pojo, making it in subsequent configuration file Category.xml.Using reWhen sultType is used, you can use Category directly instead of writing the whole com.how2java.pojo.Category-->.< typeAliases>< package name= "com.zhanglu.entity" />< /typeAliases>< plugins>&lT;! - com.github.pagehelper is the --> of the PageHelper class.< plugin interceptor= "com.github.pageheLper.PageInterceptor "><, --4.0 automatically recognize the database --><! --< property NamE= "dialect" value= "MySQL" /> --><! - this parameter is false --> by default.<!! - set to tRue will use RowBounds's first parameter offset as pageNum page number to use --><!! - the same as the pageNum effect in startPage; --><! --< property name= "offsetAsPageNum" value= "true" /> --><!! - this parameter.The default is false --><!! - when set to true, RowBounds paging will be used for count query --><! --< PRoperty name= "rowBoundsWithCount" value= "true" /> --><!! - when set to true, if pageSize=0Or RowBounds.limit = 0 will query all the results --><!! - (equivalent to not executing paging queries, but returning results are still Page types).< property name= "pageSizeZero" value= "true" /> --><!! - 3.3.0 version available - paging parameter rationalization, silentRecognize false to disable -->When rationalization is enabled, if pageNum & lt; 1 queries the first page, if pageNum & gt; pages queries the last page - & gt;<! - if pageNum< is disabled when rationalization is done, 1 or pageNum> pages will return empty data --><! --< propertyName= "reasonable" value= "true" /> --><!!! - 3.5.0 version available - to support startPage (Object para)MS) method --><! -- Added a `params'parameter to configure the parameter mapping for taking values from Map or ServletRequest - & gt;PageNum, pageSize, count, pageSizeZero, reasonable, and default values for mappings are not configured - & gt;<!! - do not understandUnder the premise of this meaning, do not copy the configuration arbitrarily.< property name= "params" value= "pageNum=start; pageSize=limit;" />-->< /plugin>< /plugins><!! -- multiple data sources can be configured. This function takes the place of --> in the spring container.<! --< environments default= "development" >< environment id= "development" >< transactionManager type= "JDBC" />< dataSource type= "POOLED" >< property name= "driver" value= "com.mysql.jdbc.Driver" />< PropertY name= "URL" value= "jdbc:mysql://localhost:3306/how2java? CharacterEncoding=UTF-8" />< property name= "username" value= "root" />< property name= "password" value= "root" />< /dataSource>< /environment>< /environments> --><!! -- this function replaces --> in the spring configuration.<! --< mappers>< mapper resource= "com/how2java/pojo/Category.xml "/>< mapper class= "com.how2java.mapper.CategoryMapper" />< /mappers> -->< /configuration>

jdbc.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/honor
jdbc.username=root
jdbc.password=abc123
jdbc.validationQuery=select 'x'

Leave a Reply

Your email address will not be published. Required fields are marked *