Mybatis的mapper配置文件(一)

<?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.geo.dao.UserMapper">
    <!--resultMap将数据库中的列(字段名)和entity中属性的名字建立映射关联,id 这个resultMap的唯一标识,type表示要映射的实体-->
    <resultMap id="userMap" type="com.geo.entity.SysUser">
        <!--//主键可以这样使用,id 为了调高性能-->
        <id property="id" column="id"/>
        <!--其他的字段,property代表entity属性,column 代表数据库字段-->
        <result property="userName" column="user_name"/>
        <result property="userPassword" column="user_password"/>
        <result property="userEmail" column="user_email"/>
        <result property="userInfo" column="user_info"/>
    </resultMap>
    <!-- 返回类型使用resultMap,就是前面定义的resultMap-->
    <select id="selectById" resultMap="userMap">
        select * from sys_user where id =#{id}
    </select>
    <!-- 返回类型使用resulType,需要SQL中为所有列名和属性名不一致的列设置别名,但是驼峰式命名的属性可以和数据库里的下划线自动转换,设置mybatis的setting mapUnderscoreToCamelCase 的值为true 所以这里使用 select * -->
    <select id="selectAll" resultType="com.geo.entity.SysUser">
        select * from sys_user
    </select>
    <insert id="insert">
        insert into sys_user(id,user_name,user_password,user_email,user_info)
        values(#{id},#{userName},#{userPassword},#{userEmail},#{userInfo})
    </insert>
</mapper>

namespace 必须配置成接口的全限定名。Mybatis内部就是通过这讲接口和mapepr.xml关联起来的。接口中方法和mapper.xml 中的id对应。否则会报错

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值