myBatis——注解,#{}与${},resultMap的使用

本文介绍了myBatis的注解开发,强调了#{}与${}的区别,其中#{}对应PreparedStatement防止SQL注入,${}则无预编译存在安全隐患。重点解析了resultMap的使用,当数据库字段与类字段不匹配时,推荐使用resultMap解决。此外,详细讲解了myBatis中多对一关系的两种处理方式:按查询嵌套和按结果嵌套处理,提供了具体的配置和思路。

注解开发

注解开发不需要使用mapper.xml文件进行映射 直接绑定类

<mappers>
    <mapper class="com.luerdao.dao.UserMapper"></mapper>
</mappers>
@Select("select * from user")
List<User> getAllUser();
@Select("select * from user where id =#{id}")
User selectUserByID(int id);
@Insert("insert into user(id,name,pwd) values(#{id},#{name},#{pwd})")
int addUser(User user);
@Delete("delete from user where id = #{id}")
int deleteUser(@Param("id")int id);

@Param注解用于给方法参数起一个名字。以下是总结的使用原则:

  • 在方法只接受一个参数的情况下,可以不使用@Param。
  • 在方法接受多个参数的情况下,建议一定要使用@Param注解给参数命名。
  • 如果参数是 JavaBean , 则不能使用@Param。
  • 不使用@Param注解时,参数只能有一个,并且是Javabean。

关于#与$的区别

#{}相当于JDBC中的PrepareStatement

${} 则是没有进行预编译的 有注入问题

resultMap

在这里插入图片描述

数据库中的字段名

在这里插入图片描述

类中的字段名

在这里插入图片描述

查询出来的结果(pwd和类中字段不对应

解决方案

一:起别名
在这里插入图片描述

二:使用resulMap(推荐使用

将数据库中的column:pwd对应到类中的属性名property:password

在这里插入图片描述

多对一处理

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

多个学生关联到一个老师

这时直接查询的话 select *from student
F:\mdPicture\image-20201206233037784.png

查询不出来teacher 因为teacher在另一张表中

方式一:按查询嵌套处理

需求:获取所有学生及对应老师的信息
思路:

      1. 获取所有学生的信息
         根据获取的学生信息的老师ID->获取该老师的信息
                   3. 思考问题,这样学生的结果集中应该包含老师,该如何处理呢,数据库中我们一般使用关联查询?
                4. 做一个结果集映射:StudentTeacher
                5. StudentTeacher结果集的类型为 Student
                6. 学生中老师的属性为teacher,对应数据库中为tid。
                   多个 [1,...)学生关联一个老师=> 一对一,一对多
                7. 查看官网找到:**association – 一个复杂类型的关联;使用它来处理关联查询**
<select id="getStudent" resultMap="STMap">
    select *from student
</select>

<resultMap id="STMap" type="Student">
    <association property="teacher" column="tid" javaType="Teacher" select="selectT"></association>
</resultMap>

<select id="selectT" resultType="Teacher">
    select *from teacher where id =#{tid}
</select>

association property=“teacher” column="tid"中的tid传递给下面的select id=“selectT” 查询中,如果是只有一个值得话#{}名字可以随便填,建议与上面对应

F:\mdPicture\image-20201206234048164.png

总结:思路就是先把学生表查询出来,然后再通过查询出来的学生表中的tid去老师表中查询对应的老师,通过resultMap来实现

方式二:按结果嵌套处理

<select id="getStudent2" resultMap="STMap2">
     select s.id, s.name , t.name tname
        from student s,teacher t
        where s.tid = t.id
</select>

<resultMap id="STMap2" type="Student">
    <result property="id" column="id"/>
    <result property="name" column="name"/>
    <association property="teacher" javaType="Teacher">
        <result property="name" column="tname"></result>
    </association>
</resultMap>

在这里插入图片描述

总结:此方法很好理解,按照数据库的查询语法来写,查询出结果后我们通过结果集的映射来将Teacher表中的属性给映射到tname上

property填属性名,Type,javaType填类名,resultMap随便取名字,column数据库字段名

一对多

和多对一差不多 都是两种处理办法,按查询或者按结果处理,这里就不再赘述直接放测试用例
在这里插入图片描述

在这里插入图片描述

按结果处理

<select id="getTeacher" resultMap="TSMap">
    select t.id tid,t.name tname,s.id sid,s.name sname
    from teacher t ,student s
    where s.tid =t.id and t.id=#{id}
</select>
<resultMap id="TSMap" type="Teacher">
    <result property="id" column="tid"/>
    <result property="name" column="tname"/>
    <collection property="students" ofType="Student">
        <result property="name" column="sname"></result>
        <result property="name" column="sname" />
        <result property="tid" column="tid" />
    </collection>
</resultMap>

按查询处理

<select id="getTeacher2" resultMap="TSMap2">
    select *from teacher where id=#{id}
</select>
<resultMap id="TSMap2" type="Teacher">
    <result column="id" property="id"></result>
    <collection property="students" ofType="Students" column="id" select="selectStudentById"></collection>
</resultMap>

<select id="selectStudentById" resultType="Student">
    select * from student where tid=#{id}
</select>

欢迎访问我的博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值