Mybatis查询

7 篇文章 0 订阅

1.定义需要的映射

1.1首先再UserMapper接口中定义查询接口,

在这里插入图片描述

接口的方法映射到对应的xml文件中的语句,xml文件中id对应方法名;

1.2mapper.xml中的注意事项:

构建返回的结果集合:
在这里插入图片描述
在这里插入图片描述

1.3 注意

1.3.1 只传递一个参数

我们发现当查询只传递一个参数时,无论接口中的名称和xml中的名称是否对应,只要类型对应都能够按照传递的参数进行查询
在这里插入图片描述
在xml文件中:
在这里插入图片描述
在测试中:
在这里插入图片描述
即使名称不对应依旧能够返回结果:
在这里插入图片描述

1.3.2 传递多个参数

在这里插入图片描述
在这里插入图片描述

此时名称不对应,就会报错,因为找不到相应的参数;

Parameter 'uid' not found.Available parameters are [password, param1, username, param2]

当前端和后端的属性名称不能够对应时;我们可以使用@Param注解,使用如下:

public User getUserByNameAndPassword(@Param("name") String username, String password);

这样我们可以在查询时使用name来找到username参数;
将xml中的查询参数改为name
在这里插入图片描述
此时进行测试:
在这里插入图片描述
这表示name可以指代username参数进行查询;

2.动态传输的问题

动态传输的标识:
->#{}表示从接口声明的方法参数中获取参数,并设置到当前sql语句当中;
-> KaTeX parse error: Expected 'EOF', got '#' at position 4: {}与#̲的不同指出在于#{}可以将其中…时不会加上引号;
因此当替换sql的信息为非系统关键字的使用一定要使用#{},可以防止sql注入;
当替换的sql为系统的mysql的关键字一定要使用${};如果使用#{}就会给替换的值加上单引号,导致查询的sql错误;

2.1 例子;

当我们使用模糊查询时;在mapper.xml文件中:

<select id="getListByName" resultType="com.example.demo.model.User">
    select * from userinfo where username like '%#{username}%'
</select>

此时就会报错
在这里插入图片描述

因为我们此时定义的sql语句是错的
上述sql语句相当于:

select * from userinfo where username like '%'username'%'

出现了两次单引号;
改成$就可以避免这个问题;

<select id="getListByName" resultType="com.example.demo.model.User">
    select * from userinfo where username like '%${username}%'
</select>

在这里插入图片描述

2.2 关于sql注入

定义:使用一个特殊的sql语句查询了一个不应该查询到的内容

2.2.1 解决sql注入问题:

1.Mapper类-只有开发人员自己能够调用;
手动排查sql注入的关键字–‘or/and’;
2.针对like查询–使用系统提共的参数进行参数拼接;
使用concat关键字进行拼接;
示例:

select * from userinfo where username like concat('%',#{username},'%');

在这里插入图片描述

3. 联表查询

3.1 一对一:

文章表和用户表;
当使用联表查询的时候,如果表中数据是1对1的关系,我们需要改动一下查询的返回集合构成,:
如下:在这里以一个文章表和用户表相互对应为例:

   <resultMap id="BaseResultMap" type="com.example.demo.model.ArticleInfo">
        <id property="id" column="id"></id>
        <result property="title" column="title"></result>
        <result property="content" column="content"></result>
        <result property="createtime" column="createtime"></result>
        <result property="updatetime" column="updatetime"></result>
        <result property="uid" column="uid"></result>
        <result property="rcount" column="rcount"></result>
        <result property="state" column="state"></result>
        <association property="user" columnPrefix="u_"
                     resultMap="com.example.demo.mapper.UserMapper.BaseResultMap">

        </association>
    </resultMap>

在这里插入图片描述
在这里插入图片描述

注意

这里需要注意:查询的前缀一定不能省略,如果两张表中有相同的字段名,就会出现数据覆盖的问题,

3.1.2 实现过程

1.创建实体对象: articleinfo->包含了属性user;
2.在mapper接口中定义查询方法
3.mapper.xml具体实现; 在resultmap中定义一对一的关系;–> sql语句

<mapper namespace="com.example.demo.mapper.ArticleInfoMapper">
    <resultMap id="BaseResultMap" type="com.example.demo.model.ArticleInfo">
        <id property="id" column="id"></id>
        <result property="title" column="title"></result>
        <result property="content" column="content"></result>
        <result property="createtime" column="createtime"></result>
        <result property="updatetime" column="updatetime"></result>
        <result property="uid" column="uid"></result>
        <result property="rcount" column="rcount"></result>
        <result property="state" column="state"></result>
        <association property="user" columnPrefix="u_"
                     resultMap="com.example.demo.mapper.UserMapper.BaseResultMap">

#用于联表查询,定义查询到的user属性映射关系;
        </association>
    </resultMap>
    <select id="getAll" resultMap="BaseResultMap">
        select a.*,u.username u_username,u.id u_id,u.password u_password from articleinfo
        a left join userinfo u on a.uid=u.id
    </select>
</mapper>

在联表查询时,两个标的字段都需要与我们设置的对象属性一对一的对应起来

3.2 一对多查询

一对多指的是,当前查询到的数据还能够通过某个相关联的数据查询到另一张表的多个数据,因此,我们查询的返回值会出现一个对象还附带着多个其他对象的情况,这种情况下我们需要定义一个collection来保存这些其他对象:
示例:每个用户拥有多篇文章

<resultMap id="BaseResultMap" type="com.example.demo.model.User">
    <id column="id" property="id"></id>
    <result column="username" property="username" />
    <result column="password" property="password" />
    <result column="photo" property="photo" />
    <collection property="alist" columnPrefix="a_"
                resultMap="com.example.demo.mapper.ArticleInfoMapper.BaseResultMap">
    </collection>
</resultMap>

可以发现一对一的时候,使用的association除了关键字不同其他的都相同,collection代表的是集合,就是说查询到当前所有符合条件的数据都显示
在这里插入图片描述

4. 设置动态sql语句

4.1 if的使用,

如果存在就拼接到sql语句中,如果不存在就不拼接:

首先定义接口

在这里插入图片描述

再定义sql.xml方法:

<select id="getArticleInfo" resultMap="BaseResultMap">
    select * from articleinfo where title = #{title}
    <if test="content!=null">
        and content = #{content}
    </if>
    <if test="state!=0">
        and state=#{state}
    </if>
</select>

测试

在这里插入图片描述
在这里插入图片描述
总结:使用if可以进行选择性的参数拼接;
当三个参数都不是必要传递的时候,我们需要写三个if判断,这样太麻烦了
接下来使用trim关键字实现多个参数的判断拼接:

4.2 trim的使用

定义接口

在这里插入图片描述

定义xml

<insert id="addArticle">
    insert into articleinfo(
        title,content,uid
    <trim prefix="," suffixOverrides=",">
        <if test="rcount !=null">
            rcount,
        </if>
        <if test="state!=null">
            state,
        </if>
    </trim>
    )values(#{title},#{content},#{uid} <trim prefix="," suffixOverrides=",">
    <if test="rcount !=null">
        #{rcount},
    </if>
    <if test="state!=null">
        #{state},
    </if>
</trim>)

trim中的几个参数,prefix添加前缀,suffix添加后缀,加上override表示重写;

测试

在这里插入图片描述
在这里插入图片描述

4.3 where的使用

当关键字不为null时,都为查询条件:

定义接口:

public List<ArticleInfo> getArticleInfo3(String title, String content,int state);

定义xml

<select id="getArticleInfo3" resultType="com.example.demo.model.ArticleInfo">
    select * from articleinfo
    <where>
        <if test="title!=null">
            title=#{title}
        </if>
        <if test="content!=null">
            content=#{content}
        </if>
        <if test="state!=0">
            state=#{state}
        </if>
    </where>
</select>

测试

@Test
void getArticleInfo3() {
    List<ArticleInfo> list=articleInfoMapper.getArticleInfo3("Java",null,0);
}

在这里插入图片描述

4.4 update中set的使用

首先查看数据库中的原有数据
在这里插入图片描述

定义接口

在这里插入图片描述

定义xml方法

<update id="upArticle">
    update articleinfo
    <set>
        <if test="title !=null">
        title=#{title}
        </if>
        <if test="content!=null">
            content=#{content}
        </if>
    </set>
    where id=#{id}
</update>

测试

void upArticle() {
   int result= articleInfoMapper.upArticle(2,"今天晴天",null);
    System.out.println(result);
}

在这里插入图片描述
在这里插入图片描述

4.5 foreach的使用

定义接口

在这里插入图片描述

定义xml

<delete id="delArticleByIds">
    delete from articleinfo
    where id in
    <foreach collection="ids" item="item" open="(" close=")" separator=",">
        #{item}
    </foreach>
</delete>

在这里插入图片描述

测试

在这里插入图片描述

在这里插入图片描述
再看看查询操作:

<select id="getArticleInfo4" resultType="com.example.demo.model.ArticleInfo">
    select * from articleinfo where id in
    <foreach collection="ids" open="(" close=")" separator="," item="item">
        #{item}
    </foreach>
</select>

在这里插入图片描述

需要注意的是:这里的separator指的是sql语句中的分割:
在这里插入图片描述

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
MyBatis是一个用于完成程序和数据库交互的工具,可以帮助我们更简单和方便地操作和读取数据库。通过配置MyBatis的XML文件,我们可以编写具体的操作SQL来进行数据库查询。在配置文件中,我们需要设置数据库的连接信息,包括URL、用户名、密码和驱动类名等。同时,我们还需要配置MyBatis的mapper文件的位置,以便MyBatis能够找到并读取这些文件。通过这些配置,我们可以轻松地使用MyBatis来进行数据库查询操作。 配置MyBatis环境是基于SpringBoot项目来创建的,相对于普通的SpringBoot项目,我们只需要增加相关的MyBatis依赖和数据库驱动即可。这样,我们就可以在SpringBoot项目中使用MyBatis进行数据库查询操作了。 总结起来,要使用MyBatis进行查询操作,我们需要进行以下几个步骤: 1. 配置MyBatis的XML文件,编写具体的操作SQL。 2. 在配置文件中设置数据库的连接信息。 3. 配置MyBatis的mapper文件的位置。 4. 在SpringBoot项目中引入MyBatis的依赖和数据库驱动。 5. 使用MyBatis提供的API进行数据库查询操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [MyBatis操作数据库查询功能)](https://blog.csdn.net/qq_73471456/article/details/131194829)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值