Mybatis框架——Mapper配置文件

Mybatis框架——Mapper配置文件

增删改查标签

  1. select
    id属性:当前名称空间下的statement的唯一标识,要求id必须和mapper接口中的方法的名字一致
    resultType:将结果集映射为Java的对象类型,也可以写resultMap(返回的一个集合,可以自定义的类型)
    parameterType:传入参数类型,当传一个值时可以省略,传对象时不要省略
  2. insert
    id:唯一标识,在同一个命名空间下保持唯一,使用动态代理之后要求和方法名保持一致
    parameterType:参数的类型,使用动态代理之后和方法的参数类型一致
    useGenerateKeys:开启主键回写
    keyColumn:指定数据库的主键
    keyProperty:主键对应的pojo属性名
    标签内部:具体的sql语句
  3. update
    id:当前名称空间下的statement的唯一标识符
    parameterType:传入的参数类型,可以省略
    标签内部:具体的sql语句
  4. delete
    id:当前名称空间下的statement的唯一标识符
    parameterType:传入的参数类型,可以省略
    标签内部:具体的sql语句

#和&的应用

  1. # { }特点
    在参数是一个非POJO类型时,该值可以是任何内容,只是负责占位;#{}是预编译处理,相当于PreparedStatement使用?占位符去替换参数,防止sql注入,
    书写方式:
select * from user where username like "%"#{username}"%";
insert into user (username,password,age,sex,hobby,remark) values (#{username},#{password},#{age},#{sex},#{hobby},#{remark});
  1. $ { }特点
    是字符串拼接,相当于sql语句中的Statement ,使用字符串去拼接sql,$可以是sql中任意部分传入到Statement 中,不能防止sql注入,大括号中必须写value(一个值时)

书写方式:

select * from user where username like '%${value}%' ;
insert into user (username,password,age,sex,hobby,remark) values ('${username}','${password}','${age}','${sex}','${hobby}','${remark}');

sql注入示例:select * from user where id = 1 or 1=1

ResultMap

ResultMap是Mybatis中最重要最强大的元素,使用ResultMap可以解决两大问题:

  1. POJO属性名和表结构字段名不一致的问题
  2. 完成高级查询,比如一对一、一对多、多对多
<resultMap type="org.mybatis.vo.UserVO" id="UserVO1ResultMap" autoMapping="true">
<select id="selectUserListByNameLike" resultMap="UserVO1ResultMap" parameterType="string">
   select * from user where username like "%"#{username}"%"
</select>

resultMap 会自动把查询的结果的列名与Java类型中的属性进行匹配,匹配成功了就把值封装到对象上,没有匹配成功的就是null或0。为了解决这个问题,会对result或者id进行一一对应,主键使用id标签,其他内容全都是result

<resultMap type="org.mybatis.vo.UserVO" id="UserVO1ResultMap" autoMapping="true">
  <!--主键对应-->
  <id column="userid" property="id"/>
  <!--非主键对应-->
  <result column="username" property="username"/>
  <result column="userpassword" property="password"/>
  <result column="userage" property="age"/>
  <result column="usersex" property="sex"/>
</resultMap>
<select id="selectUserListByNameLike" resultMap="UserVO1ResultMap" parameterType="string">
  select * from user where username like "%"#{username}"%"
</select>

Sql片段

经常使用的sql片段可以单独拿出来

<sql id=""></sql>
<include refId=""/>

例如

<sql id="userComm">
	id,username,password,age,sex
</sql>
<select id="selectUserListByNameLike" resultMap="UserVO1ResultMap" parameterType="string">
	select <include refId="userComm"/></include> from user where username like "%"#{username}"%"
</select>

建议sql片段单独放在一个commSql.xml文件里
需要在配置文件中引用通用文件

<mapper resource="org/mybatis/dao/sqlComm.xml"/>
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

九儿姑娘`

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值