使用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>
posted @ 2016-12-07 22:29  chaplinthink  阅读(219)  评论(0编辑  收藏  举报