mybatis系列2

来源:互联网 发布:ubuntu 声音图标 灰色 编辑:程序博客网 时间:2024/05/20 08:02

generatorConfig.xml  配置   Oracle 数据库而言

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
<generatorConfiguration>
 <classPathEntry location="C:\Program Files\Java\ojdbc14-10.2.0.1.0.jar" />
    <context id="context1" defaultModelType="flat" targetRuntime="MyBatis3">
   
        <plugin type="org.mybatis.generator.plugins.MapperConfigPlugin">
            <property name="targetPackage" value=""/>
            <property name="targetProject" value="PluginManagerMgr/src/main/java"/>
        </plugin>
        <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@144.131.254.240:1521:nbcsora" userId="papp"
            password="papp" />
        <javaModelGenerator targetPackage="com.buybal.ums.mgr.model" targetProject="PluginManagerMgr/src/main/java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <sqlMapGenerator targetPackage="com.buybal.ums.mgr.dao" targetProject="PluginManagerMgr/src/main/resources">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
        <javaClientGenerator targetPackage="com.buybal.ums.mgr.dao" targetProject="PluginManagerMgr/src/main/java" type="XMLMAPPER">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>
       
        <table  tableName="TLOGUSER" domainObjectName="Tloguser">
            <property name="useActualColumnNames" value="fasle" />
        </table>
    </context>
</generatorConfiguration>


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>
 <plugins>
  <plugin interceptor="com.chinaums.qmfcs.dataaccess.db.interceptor.DialectStatementHandlerInterceptor">
   <property name="debug" value="true"/>
  </plugin>
 </plugins>
</configuration>

applicationContext-datasource.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:jee="http://www.springframework.org/schema/jee"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd">
 <description>数据源配置</description>
 <beans profile="local">
  <bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource">
   <property name="driver" value="${jdbc.driverClass}" />
   <property name="driverUrl" value="${jdbc.url}" />
   <property name="user" value="${jdbc.username}" />
   <property name="password" value="${jdbc.password}" />
   <property name="alias" value="Pool_dbname" />
   <property name="maximumActiveTime" value="300000" />
   <property name="prototypeCount" value="0" />
   <property name="maximumConnectionCount" value="10" />
   <property name="minimumConnectionCount" value="2" />
   <property name="simultaneousBuildThrottle" value="10" />
   <property name="houseKeepingTestSql" value="select 1 from dual" />
  </bean>
 </beans>
 <beans profile="!local">
  <jee:jndi-lookup id="dataSource" jndi-name="${jdbc.jndi}" />
 </beans>
</beans>

applicationContext-dataaccess.xml  里面的配置 (被web.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"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:jpa="http://www.springframework.org/schema/data/jpa"
 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/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
 <context:component-scan base-package="com.chinaums.qmfcs.dataaccess.service" />
 <bean id="springContextUtil" class="com.chinaums.qmfcs.dataaccess.util.SpringContextUtil" />
 <!-- 数据源配置 -->
 <import resource="applicationContext-datasource.xml" />
 <import resource="applicationContext-cache.xml" />
 <!-- Jpa Entity Manager 配置 -->
 <bean id="entityManagerFactory"
  class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" depends-on="springContextUtil">
  <property name="dataSource" ref="dataSource" />
  <property name="packagesToScan" value="com.chinaums.qmfcs.dataaccess.model" />
  <property name="persistenceProviderClass" value="org.hibernate.ejb.HibernatePersistence" />
  <property name="jpaProperties">
   <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<!--     <prop key="hibernate.format_sql">true</prop> -->
    <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
    <prop key="hibernate.show_sql">true</prop>
<!--     <prop key="hibernate.cache.region.factory_class">com.chinaums.qmfcs.dataaccess.cache.HibernateHazelcastCacheRegionFactory</prop>
    <prop key="hibernate.generate_statistics">false</prop> -->
                <prop key="hibernate.hbm2ddl.auto">none</prop>
                <prop key="hibernate.enable_lazy_load_no_trans">true</prop>
            </props>
  </property>
 </bean>
 <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
  <property name="entityManagerFactory" ref="entityManagerFactory" />
 </bean>
 <!-- 解决实体的延时加载问题 start -->
 <bean name="openEntityManagerInViewInterceptor"
        class="org.springframework.orm.jpa.support.OpenEntityManagerInViewInterceptor">
        <property name="entityManagerFactory">
        <ref bean="entityManagerFactory" />
        </property>
    </bean>
    <!-- end -->
 <bean id="transactionManagerForMybatis" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
     <property name="dataSource" ref="dataSource" />
 </bean>
 <tx:annotation-driven transaction-manager="transactionManager"/>
 <tx:annotation-driven transaction-manager="transactionManagerForMybatis"/>
 <jpa:repositories base-package="com.chinaums.qmfcs.dataaccess.repository"  transaction-manager-ref="transactionManager"
  entity-manager-factory-ref="entityManagerFactory"/>
 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <property name="configLocation" value="classpath:mybatis-config.xml" />
  <property name="mapperLocations" value="classpath:mybatis/*Mapper.xml" />     
  <property name="typeAliasesPackage" value="com.chinaums.qmfcs.dataaccess.model" />
 </bean>
 <bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
  <property name="basePackage" value="com.chinaums.qmfcs.dataaccess.dao" />
  <!-- 这里不要定义sqlSessionFactory, 定义了会导致properties文件无法加载 -->
 </bean>
 <bean id="pageImplConverter" class="com.chinaums.qmfcs.dataaccess.util.PageImplConverter" />
 <aop:config>
  <aop:aspect ref="pageImplConverter">
   <aop:around method="convert" pointcut="execution(org.springframework.data.domain.Page com.chinaums.qmfcs.dataaccess.repository..*(..))"/>
  </aop:aspect>
 </aop:config>
</beans>

其中 mybatis  文件夹下的  Mapper.xml  是对象操作   


QmcsFpUserMapper.xml 如:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.chinaums.qmfcs.dataaccess.dao.QmcsFpUserMapper">
  <resultMap id="FpUserResultMap" type="com.chinaums.qmfcs.dataaccess.model.QmcsFpUser">
    <id column="ID" jdbcType="VARCHAR" property="id" />
    <result column="MOBILE_NO" jdbcType="VARCHAR" property="mobileNo" />
    <result column="USER_CODE" jdbcType="VARCHAR" property="userCode" />
    <result column="USER_NAME" jdbcType="VARCHAR" property="userName" />
    <result column="IS_DELETED" jdbcType="VARCHAR" property="isDeleted" />
    <result column="CREATE_TIME" jdbcType="TIMESTAMP" property="createTime" />
    <result column="UPDATE_TIME" jdbcType="TIMESTAMP" property="updateTime" />
  </resultMap>
  <sql id="FPUSER_Column_List">
        ID, MOBILE_NO, USER_CODE, USER_NAME, IS_DELETED, CREATE_TIME, UPDATE_TIME
  </sql>
 
  <select id="selectFpUsers" resultMap="FpUserResultMap">
         select <include refid="FPUSER_Column_List" /> from TP_QMCS_FP_USER where IS_DELETED='0'
  </select>  
 
  <select id="getPageFpUser"  resultMap="FpUserResultMap">
      SELECT * FROM(SELECT ROWNUM AS no, U.*
      FROM TP_QMCS_FP_USER U
      WHERE ROWNUM &lt; #{end,jdbcType=DECIMAL}
        AND U.IS_DELETED ='0'
        <if test="mobileNo!=null and mobileNo != '' ">
          and U.MOBILE_NO like concat(concat('%',#{mobileNo,jdbcType=VARCHAR}),'%')
        </if>
        <if test="userName!=null and userName != '' ">
          and U.USER_NAME like concat(concat('%',#{userName,jdbcType=VARCHAR}),'%')
        </if>
        ORDER BY  U.UPDATE_TIME DESC
      ) where no &gt; #{start,jdbcType=DECIMAL}
    </select>
   
     <!-- 根据条件查询用户条数 -->
    <select id="selectCountFpUser" resultType="java.lang.Integer">
      SELECT COUNT (1)
      FROM TP_QMCS_FP_USER U
      where U.IS_DELETED = '0'
      <if test="mobileNo != null and mobileNo != ''  ">
        and U.MOBILE_NO like concat(concat('%',#{mobileNo,jdbcType=VARCHAR}),'%')
      </if>
      <if test="userName != null and userName != ''  ">
        and U.USER_NAME like concat(concat('%',#{userName,jdbcType=VARCHAR}),'%')
      </if>
    </select> 
   
    <select id="getFpUserByUserCodeIsNull" resultMap="FpUserResultMap">
      SELECT  <include refid="FPUSER_Column_List" />
      FROM TP_QMCS_FP_USER
      where IS_DELETED = '0'
      AND USER_CODE IS null
    </select>
   
  <update id="updateFpUserByMobileNo" parameterType="com.chinaums.qmfcs.dataaccess.model.QmcsFpUser" >
    update TP_QMCS_FP_USER
    set USER_CODE = #{userCode,jdbcType=VARCHAR}
    where MOBILE_NO = #{mobileNo,jdbcType=VARCHAR}
  </update>
  ****注意批量update******* 和Mysql有区别
  <update id="updateBatchFpUser" parameterType="java.util.List">
    <foreach collection="list" item="item" index="index" open="begin" close=";end;" separator=";">
       update TP_QMCS_FP_USER 
       set USER_CODE = #{item.userCode,jdbcType=VARCHAR}
       where MOBILE_NO = #{item.mobileNo,jdbcType=VARCHAR}
   </foreach>
  </update>
</mapper>