Mybaitis配置总结

  • 在mybatis-config.xml中配置分页插件,插件配置必须放在mapper前面
    <plugins>
        <plugin interceptor="com.rrtong.frame.dao.PaginationInterceptor">
            <property name="dialect" value="oracle"/>
        </plugin>
    </plugins>    
  • Mybatis分页拦截器获取sql
      /*以下得到的是要执行的sql语句,并不是配置的sql语句*/
      StatementHandler statementHandler = (StatementHandler)invocation.getTarget();
      BoundSql boundSql = statementHandler.getBoundSql();
      String originalSql = boundSql.getSql();    
  • 出现java.lang.AbstractMethodError: org.mybatis.spring.transaction.SpringManagedTransactionFactory.newTransaction(Ljava/sql/Connection;)Lorg/apache/ibatis/transaction/Transaction;

            版本不正确,需要使用更高版本的 mybatis-spring

           https://mybatis.googlecode.com/files/mybatis-spring-1.2.1.zip

  • Mybaitis参数严格区分大小写

             mybatis 配置文件中的参数绑定区分大小写,必须与传递的参数名保持一致

  • 出现数据类型111时,这是因为数据库字段数据类型与entity中的字段定义的数据类型不一致导致的,有以下两种方法解决

              1、设定entity的数据类型与数据库类型一致
              2、在sql中定义参数的数据类型,比如

        where userCode = #{userCode, jdbcType=VARCHAR}
  • 实现数据库字段与实体字段的关系对应
     <resultMap id="Archive" type="Archive">  
        <id     column="ARCHIVESKEY" property="archiveKey"     />  
        <result column="title"       property="archiveTitle"    />  
    </resultMap>
  • 定义oracle数据库的mybatis获取主键并返回的配置,mybatis before after设置
<p>       <selectKey  keyProperty="archiveKey" order="BEFORE" resultType="String">
           SELECT  SEQ_ARCHIVE.NEXTVAL
           FROM DUAL
       </selectKey></p>
  • 定义其他数据库的Mybatis获取主键并返回的配置
    <insert id="insertUser" useGeneratedKeys="true" keyProperty="userId" parameterType="com.mybatis.User">  
        insert into user(userName,password)  
        values(#{userName},#{password})  
    </insert>  
  • 定义mybatis查询结果注入到实体中
       <resultMap id="Archive" type="Archive">  
           <id     column="SPAARCHIVESKEY" property="archiveKey"     />  
           <result column="title"          property="archiveTitle"    />     
       </resultMap>



       <select id="getArchive"  parameterType="HashMap"  resultMap="Archive">
		select 
		    SPAARCHIVESKEY,
		    TITLE	    
		from Archives
		where USERCODE = #{userCode, jdbcType=VARCHAR}	
                And VALID= #{valid, jdbcType=VARCHAR}		
	</select>


  • 定义Mybaitis更新数据库表记录
        <update id="updateArchive" parameterType="Archive">
		update Tbl_ARCHIVES
		set
			title		= #{archiveTitle},
			content		= #{content},
			feeValue	= #{feeValue} 
			security 	= #{security},
			subjectType     = #{subjectType},
			gradeType 	= #{gradeType},
			gather 		= #{gather},
			archiveType = #{archiveType},
			keyWordName = #{keyWordName}
		where archivesKey = #{archiveKey}
	</update>
  • 定义mybatis删除数据表记录的方式
<delete id="deleteArchive" parameterType="String">
	delete from  title 
	where spaArchivesKey = #{archiveKey}
</delete>	
  • 定义公共的sql语句,允许复用
    <sql id="addrColumn">
         a.PUSH_ID,
         a.MEMBER_ID,
         a.CONTENT,
         a.PHOTO,
         a.PUSH_NAME,
         a.IS_AUTO,
         a.IS_FINE,
         a.IS_SOURCE,
    	 a.SOURCE_PUSH_ID, 
         DATE_FORMAT(a.CREATE_TIME,'%Y-%m-%d %H:%i:%s') CREATE_TIME, 
         DATE_FORMAT(a.DELETE_TIME,'%Y-%m-%d %H:%i:%s') DELETE_TIME 
     </sql>

       引用复用的sql语句

	<select id="getMyOrFineBlog" parameterType="MicroBlog" resultType="MicroBlog">
	    SELECT 
		<include refid="addrColumn"/> 
            FROM TBL_BLOG_PUSH a, TBL_USER_MEMBER b 
            WHERE a.MEMBER_ID = b.MEMBER_ID 
	    <if test="isFine == 1"> AND a.IS_FINE = #{isFine} </if>				
	</select>


原文地址:https://www.cnblogs.com/wala-wo/p/5119245.html