zoukankan      html  css  js  c++  java
  • mybatis整合spring

    <dependencies>
      
            <dependency>
              <groupId>junit</groupId>
              <artifactId>junit</artifactId>
              <version>3.8.1</version>
              <scope>test</scope>
            </dependency>
        
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>4.2.9.RELEASE</version>
                <exclusions>
                    <exclusion>
                        <groupId>commons-logging</groupId>
                        <artifactId>commons-logging</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
                <version>4.2.9.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aop</artifactId>
                <version>4.2.9.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aspects</artifactId>
                <version>4.2.9.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-tx</artifactId>
                <version>4.2.9.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>4.2.9.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context-support</artifactId>
                <version>4.2.9.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>4.2.9.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>4.2.9.RELEASE</version>
            </dependency>
            
            
            <!-- mybatis依赖 -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.1.1</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>1.1.1</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-ehcache</artifactId>
                <version>1.0.0</version>
            </dependency>
            <!-- mysql驱动包依赖 -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.21</version>
            </dependency>
            <!-- alibaba数据源依赖 -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.0.11</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.1.26</version>
            </dependency>
            
            
            <!-- 日志 -->
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>jcl-over-slf4j</artifactId>
                <version>1.5.8</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>1.5.8</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
                <version>1.5.8</version>
            </dependency>
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.14</version>
            </dependency>
            
            <!-- servlet-->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>3.1.0</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>javax.servlet.jsp</groupId>
                <artifactId>javax.servlet.jsp-api</artifactId>
                <version>2.3.1</version>
                <scope>provided</scope>
            </dependency>
            
            <!-- springmvc返回json相关-->
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-core</artifactId>
                <version>2.8.8</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.8.8</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-annotations</artifactId>
                <version>2.8.8</version>
            </dependency>
            
             <!-- springmvc文件上传依赖 -->
            <dependency>
                <groupId>commons-fileupload</groupId>
                <artifactId>commons-fileupload</artifactId>
                <version>1.3.1</version>
            </dependency>
            
            <dependency>
                <groupId>commons-io</groupId>
                <artifactId>commons-io</artifactId>
                <version>2.4</version>
            </dependency>
            
            
            
            <!-- <dependency>
                <groupId>org.wltea</groupId>
                <artifactId>analyzer</artifactId>
                <version>2012_u6</version>
                <scope>system</scope>
                <systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/analyzer-2012_u6.jar</systemPath>
            </dependency> -->
            
            
            <dependency>
                <groupId>com.google.guava</groupId>
                <artifactId>guava</artifactId>
                <version>19.0</version>
            </dependency>
            
            
            
        
      </dependencies>
      
      <build>
         <plugins>
            <plugin> 
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source> 
                    <target>1.7</target> 
                    <encoding>UTF8</encoding> 
                </configuration>
            </plugin>
            
            
            <plugin>  
                <groupId>org.mortbay.jetty</groupId>  
                <artifactId>jetty-maven-plugin</artifactId>  
                <version>8.1.8.v20121106</version>  
                <configuration>  
                    <reload>manual</reload>  
                    <webAppConfig>  
                        <contextPath>/${project.build.finalName}</contextPath>  
                    </webAppConfig>  
                    <connectors>  
                        <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">  
                            <port>9080</port>  
                            <!--<maxIdleTime>60000</maxIdleTime>-->  
                        </connector>  
                    </connectors>  
                </configuration>  
            </plugin>  
            
            
            <!-- <plugin>
              <groupId>org.eclipse.jetty</groupId>
              <artifactId>jetty-maven-plugin</artifactId>
              <version>8.1.16.v20140903</version>
              <configuration>
                     <webDefaultXml>src/main/resources/jetty/webdefault.xml</webDefaultXml> 
                     <scanIntervalSeconds>5</scanIntervalSeconds>
                     <webApp>
                         <contextPath>/my</contextPath>
                     </webApp>
                     <connectors>
                         <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
                             <port>9000</port>
                             <maxIdleTime>60000</maxIdleTime>
                         </connector>
                     </connectors>
                 </configuration>
            </plugin>
             -->
            
        </plugins>
      </build>
      
    </project>
    <?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"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:p="http://www.springframework.org/schema/p"
        xmlns:tx="http://www.springframework.org/schema/tx"
        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
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
            
            <context:property-placeholder location="classpath:config.properties"/>
            
            <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="0" />
            <!-- 连接池最大使用连接数量 -->
            <property name="maxActive" value="20" />
            <!-- 连接池最大空闲 -->
            <!-- <property name="maxIdle" value="20" /> -->
            <!-- 连接池最小空闲 -->
            <property name="minIdle" value="0" />
            <!-- 获取连接最大等待时间 -->
            <property name="maxWait" value="60000" />
    
            <!-- <property name="poolPreparedStatements" value="true" /> <property 
                name="maxPoolPreparedStatementPerConnectionSize" value="33" /> -->
    
            <property name="validationQuery" value="${validationQuery}" />
            <property name="testOnBorrow" value="false" />
            <property name="testOnReturn" value="false" />
            <property name="testWhileIdle" value="true" />
    
            <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
            <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="stat" /> -->
            <property name="filters" value="mergeStat" />
        </bean>
        <!-- myBatis文件 -->
        
        
        
        <!-- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> -->
        
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
            <property name="mapperLocations" value="classpath:com/dogoframework/mapping/*.xml" />
        </bean>
    
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.dogoframework.dao" />
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
        </bean>
    
        <!-- 配置事务管理器 -->
        <!-- <bean id="transactionManager"
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource" />
        </bean> -->
    
        <!--  PROPAGATION_REQUIRED-支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。
        PROPAGATION_SUPPORTS-支持当前事务,如果当前没有事务,就以非事务方式执行。
        PROPAGATION_MANDATORY-支持当前事务,如果当前没有事务,就抛出异常。
        PROPAGATION_REQUIRES_NEW-新建事务,如果当前存在事务,把当前事务挂起。
        PROPAGATION_NOT_SUPPORTED-以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
        PROPAGATION_NEVER-以非事务方式执行,如果当前存在事务,则抛出异常。
        PROPAGATION_NESTED-如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则进行与PROPAGATION_REQUIRED类似的操作。
         -->
        <!-- 注解方式配置事物 -->
        <!-- <tx:annotation-driven transaction-manager="transactionManager" /> -->
      
        <!-- 拦截器方式配置事物 -->
        <!-- <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <tx:method name="add*" propagation="REQUIRED" />
                <tx:method name="append*" propagation="REQUIRED" />
                <tx:method name="insert*" propagation="REQUIRED" />
                <tx:method name="save*" propagation="REQUIRED" />
                <tx:method name="update*" propagation="REQUIRED" />
                <tx:method name="modify*" propagation="REQUIRED" />
                <tx:method name="edit*" propagation="REQUIRED" />
                <tx:method name="delete*" propagation="REQUIRED" />
                <tx:method name="remove*" propagation="REQUIRED" />
                <tx:method name="repair" propagation="REQUIRED" />
                <tx:method name="delAndRepair" propagation="REQUIRED" />
                <tx:method name="get*" propagation="SUPPORTS" />
                <tx:method name="find*" propagation="SUPPORTS" />
                <tx:method name="load*" propagation="SUPPORTS" />
                <tx:method name="search*" propagation="SUPPORTS" />
                <tx:method name="datagrid*" propagation="SUPPORTS" />
                <tx:method name="select*" propagation="SUPPORTS" />
                <tx:method name="*" rollback-for="Exception"  />
            </tx:attributes>
        </tx:advice>
        <aop:config>
        
            <aop:pointcut id="transactionPointcut" expression="execution(* com.dogoframework.service.*.*(..))" />
            <aop:advisor pointcut-ref="transactionPointcut"
                advice-ref="transactionAdvice" />
        </aop:config> -->
    
    
    </beans>

    springboot

    //@MapperScan("com.demo.*.mapper"):一个*代表任意字符串,但只代表一级包,比如可以扫到com.demo.aaa.mapper,不能扫到com.demo.aaa.bbb.mapper
    //@MapperScan("com.demo.**.mapper"):两个*代表任意个包,比如可以扫到com.demo.aaa.mapper,也可以扫到

    一对多

    <resultMap type="dogo.model.TerminalUser" id="routeCartResultMap" extends="BaseResultMap">
            <!-- 区域定格一对一关联  -->
            <association property="area" javaType="dogo.model.Area">
                <id property="id" column="area_id"/>
                <result property="areaname" column="area_areaname"/>
            </association>
            <!-- 路线一对一关联  -->
            <association property="line" javaType="dogo.model.Line">
                <id property="id" column="line_id"/>
                <result property="routename" column="line_routename"/>
            </association>
            <!-- 等级一对一关联  -->
            <association property="level" javaType="dogo.model.Level">
                <id property="id" column="level_id"/>
                <result property="levelname" column="level_levelname"/>
            </association>
            <!-- 销售渠道一对一关联  -->
            <association property="saleChannel" javaType="dogo.model.SaleChannel">
                <id property="id" column="salechannel_id"/>
                <result property="channelcode" column="salechannel_channelcode"/>
            </association>
            <!-- 销售主渠道一对一关联  -->
            <association property="primaryChannel" javaType="dogo.model.PrimaryChannel">
                <id property="id" column="primarychannel_id"/>
                <result property="channelcode" column="primarychannel_channelcode"/>
            </association>
            <!-- 销售次渠道一对一关联  -->
            <association property="secondChannel" javaType="dogo.model.SecondChannel">
                <id property="id" column="secondchannel_id"/>
                <result property="channelcode" column="secondchannel_channelcode"/>
            </association>
            <!-- 终端部门一对一关联  -->
            <association property="terminalDepartment" javaType="dogo.model.TerminalDepartment">
                <id property="id" column="terminaldepartment_id"/>
                <result property="name" column="terminaldepartment_name"/>
            </association>
            
            <!-- 终端定格一对一关联  -->
            <association property="terminalGroup" javaType="dogo.model.TerminalGroup">
                <id property="id" column="terminalgroup_id"/>
                <result property="groupname" column="terminalgroup_groupname"/>
            </association>
            <!-- 终端商品库存一对多关联 -->
            <collection property="terminalNowStockList" javaType="list" ofType="dogo.model.TerminalNowStock">
                <id property="id" column="terminalnowstock_id" />
                <result property="productid" column="terminalnowstock_productid"/>
                <result property="sumbox" column="terminalnowstock_sumbox" />
                <!-- 终端商品库存和商品表一对一关联  -->
                <!-- <association property="storeSroduct" javaType="dogo.model.StoreProduct">
                    <id property="id" column="storesroduct_id"/>
                    <result property="productname" column="storesroduct_productname"/>
                </association> -->
            </collection> 
            
            
            <!-- 终端送货标准商品库存一对多关联 -->
            <!-- <collection property="puhuobiaozhunTerminalNowStockList" javaType="list" ofType="dogo.model.TerminalNowStock">
                <id property="id" column="puhuobiaozhunterminalnowstock_id" />
                <result property="productid" column="puhuobiaozhunterminalnowstock_productid"/>
                <result property="sumbox" column="puhuobiaozhunterminalnowstock_sumbox" />
                终端送货标准商品库存和商品表一对一关联 
                <association property="puhuobiaozhunstoreSroduct" javaType="dogo.model.StoreProduct">
                    <id property="id" column="puhuobiaozhunstoresroduct_id"/>
                    <result property="productname" column="puhuobiaozhunstoresroduct_productname"/>
                    送货标准和商品表一对一关联 
                    <association property="distributionStandard" javaType="dogo.model.DistributionStandard">
                        <id property="id" column="distributionstandard_id"/>
                        <result property="productid" column="distributionstandard_productid"/>
                        <result property="levelid" column="distributionstandard_levelid"/>
                        <result property="youhuo" column="distributionstandard_youhuo"/>
                        <result property="puhuo" column="distributionstandard_puhuo"/>
                        <result property="youxiaopuhuo" column="distributionstandard_youxiaopuhuo"/>
                    </association>
                </association>
            </collection>  -->
            
        </resultMap>
    <resultMap type="com.yonyou.iuap.demo.uiresources.po.UiresourcesTree" id="treeResultMap" extends="BaseResultMap">
              <collection property="childs" javaType="list" ofType="com.yonyou.iuap.demo.verify.po.VerifyPO">
                <id property="id" column="vid" />
                <result property="fileid" column="fileid"/>
                <result property="filename" column="filename" />
              </collection>
        </resultMap>
        
        <!-- 查询资源树 -->
        <select id="tree" resultMap="treeResultMap">
             select a.*,b.id vid, b.fileid,b.filename from sit_uiresources a 
             left join sit_verify b
             on a.id = b.source_id
             where 
             FIND_IN_SET(#{sourceId},b.all_source_id) 
             and b.status = #{status}
        </select>
  • 相关阅读:
    React.js自学第一天
    优化的34条定律
    JS 打字机效果
    css3 翻书效果
    对象操作
    表单提交验证
    封装cookie组件
    iOS中为网站添加图标到主屏幕
    教你从Win8换回Win7
    关于VB中Print函数在数组中换行的理解
  • 原文地址:https://www.cnblogs.com/jentary/p/7109706.html
Copyright © 2011-2022 走看看