zoukankan      html  css  js  c++  java
  • 使用MyBatis 框架犯的错误

    最近做项目,数据层使用的是MyBatis框架,在使用过程中,犯了一些错误:

    resultMap和resultType书写错误导致问题

    resultMap和resultType二者用法不一样:

    resultType:

    <select id="count" parameterType="AreaDto"
     resultType="java.lang.Integer">  
            SELECT count(*) FROM USER  
    </select>  
    

    resultMap:

    <resultMap type="com.liulanghan.Blog" id="BlogResult">    
        <id column="id" property="id"/>    
        <result column="title" property="title"/>    
        <result column="content" property="content"/>    
        <result column="owner" property="owner"/>    
    </resultMap>   
    
    <select id="selectBlog" parameterType="int" resultMap="BlogResult">    
          select * from t_blog where id = #{id}    
    </select>  

    resultType值可以指定很多类型,包括一个类。

    jdbcType中的int类型为INTEGER

    Mybatis中javaType和jdbcType对应关系:

    jdbcTypejavaType
    CHAR String
    VARCHAR String
    BOOLEAN boolean
    INTEGER int
    FLOAT double
    DOUBLE double



    在insert语句中,values后忘了加#{ },直接属性上去了。

    <insert id="add" parameterType="EStudent">
      insert into TStudent(name, age) values(#{name}, #{age})
    </insert>

    values后面的值是相应类对应的属性值,还可以对这些属性指定jdbcType :

    <insert id="insert"  parameterType="com.examples.Role">
            insert into role ( id,name,type) values 
            (
             #{id,jdbcType=CHAR},
             #{name,jdbcType=VARCHAR},
             #{type,jdbcType=CHAR}
            )
        </insert>
  • 相关阅读:
    关于Oracle
    form表单中包含特殊字符,需要转义。
    mysql5.7解压版安装步骤
    mysql报1055错误
    配置maven私有仓库
    全选,反选
    前后端数据交互(json)
    正则表达式匹配html标签里的中文
    excel创建行、插入行、设置样式
    Python 中文字符的输出
  • 原文地址:https://www.cnblogs.com/bigdata1024/p/8387427.html
Copyright © 2011-2022 走看看