Home  >  Article  >  Database  >  Mybaitis配置过程注意点_MySQL

Mybaitis配置过程注意点_MySQL

WBOY
WBOYOriginal
2016-06-01 13:08:29843browse
  • 在mybatis-config.xml中配置分页插件,插件配置必须放在mapper前面
    <plugins>        <plugin interceptor="com.rrtong.frame.dao.PaginationInterceptor">            <property name="dialect" value="oracle"></property>        </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"></id>          <result column="title" property="archiveTitle"></result>      </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 property="archiveKey"></id>             <result column="title" property="archiveTitle"></result>            </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>	


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn