xml数据库操作笔记

这个笔记是在自己学习的过程中根据实际用到的和学到的整理出来的,可能会有缺失,错误等,主要是给激励自己学习,遇到写不下去的情况给自己一个参考,请各位大佬发现问题提出问题时能嘴下留情,也希望多提建议,谢谢。
本笔记长期更新(更新日期2024年9月15日)

XML数据库操作

XML文件固定格式

<?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="">

</mapper>

XML基本用法

xml配置

首先要和Mapper类位置保持相对一致,例如UserMapper.java的位置是在src路径下的com.heima.mapper,那UserMapper.xml的位置应该在resources路径下的com/heima/mapper。

然后****里面写上映射目标JAVA的位置,一般IDEA有提示,一步步选就可以

xml基础写法

注意双引号和单引号的使用,如果外面使用了双引号,里面要写字符串就要用单引号!反之同理!

<select id="" parameterType=""  resultType=""></select>

<insert id=""></select>

<update id=""></select>

<delete id=""></select>

id对应的是Mapper类中定义的方法,这里作为实现

parameter是接收的数据类型,resultType是期望从数据库查询返回的对象类型 。这个数据类型既可以是一个Java类型,例如(java.util.map,int,java.lang.String),也可以是自定义的封装POJO类型。

然后在尖括号标签中就可以写查询语句了

注意事项

语句结束后不要加分号

语句中传参注意事项

例如要写这么一个SQL语句,我们想这么表达:

select * from user where id = id;
前面的id是数据库中的列名
后面的id是我们想查询的id,从java中拿到的

那后面那个id就应该写成SQL参数占位符#{id}

select * from user where id = #{id}

where标签

where表现是可以拆出来的,一般搭配if标签使用,做动态SQL

if标签

if标签一般是搭配where标签使用的,做动态数组,test里面填写判断语句

一般判断是否为空或者是否为空字符串,但是判断为空字符串,也只有在string的情况下会做,例如id是int类型,不用做是否为空字符串判断,而name就需要

有一个很有趣的点,有很多个if语句,第一个标签内部可以不加and,但是后面必须加,第一个加了也没错,因为会动态判断,所以最好都加上and

<if test=""></if>

例如

<select id = "select" resultType = "com.heima.pojo.User">
	select * from user
	<where>
		<if test = "id != null"> and id = #{id} </if>
		<if test = "name != null and name != '' "> and name = #{name} </if>
	</where>
</select>

遍历标签foreach

foreach正如其名,是用来遍历循环集合的,一般用于批量插入,也用于构建IN语句,会循环执行标签中写的内容有如下几个属性

  1. collection:集合名称
  2. item:集合遍历出来的元素/项
  3. separator:每一次遍历使用的分隔符,这个分隔符在MySql中一般是逗号
  4. open:遍历开始前拼接的片段,也是迭代开始时的标记,通常用于打开括号或开始部分
  5. close:遍历结束后拼接的片段,也是迭代结束时的标记,通常用于关闭括号或结束部分

讲一下open和close在不同情况下的区别

IN语句例子

<select id="select" resultType="com.heima.pojo.User">
	select * from user where id in
    <foreach collection="list" item="item" open="(" sparator="," clost=")">
        #{item}
    </foreach>
</select>

在这个语句中,open="(“在迭代项开始时添加左括号,close=”)"会在迭代项结束添加右括号)

如果有数据,假设传递过来了一个list,{1,2,3},那么在数据库执行的语句就是

select * from user where id in (1,2,3);

批量插入例子

<!-- 这是一种写法,如果不用括号框起来values后面的值-->
<insert id="insert">
	insert into user(name,age) values
    <foreach collection="list" item="user" separator="),(" open="(" close=")">
    	#{user.name},#{user,age}
    </foreach>
</insert>

<!-- 这是一种写法,如果用括号框起来values后面的值-->
<insert id="insert">
	insert into user(name,age) values
    <foreach collection="list" item="user" separator="," open="" close="">
    	(#{user.name},#{user.age})
    </foreach>
</insert>

假设传过来的数据是

list:[
    {
        "name":"zs",
        "age":18
    },
    {
        "name":"ls",
        "age":19
    }
]

那么生成的sql语句是

insert into user(name,nage) values('zs',18),('ls',19);

choose标签

choose标签和switch-case的用法非常相似,提供多个条件分支

choose相当于switch标签,传递的参数在接口Mapper里面

wen相当于case,test里面依旧写判断语句

例如

<choose>
	<when test="status=='active'">status='active'</when>
    <when test="status=='inactive'">status='inactive'</when>
    <otherwise>status in not null</otherwise>
</choose>

trim标签

trim标签和String提供的String.trim()方法非常相似

trim标签用于去除多余的空白字符或者调整SQL语句结构,比如自动添加或删除AND/OR等

<trim prefix="where" prefixOverrides="AND|OR">
    <if test="name != null">name = #{name}</if>
    <if test="age != null">and age = #{age}</if>
</trim>

set标签

通常用于update语句中,根据条件设置字段

<update>
	update user
    <set>
    	<if test="name != null and name != ''">name = #{name}</if>
        <if test="age != null">age = #{age}</if>
    </set>
    where id = #{id}
</update>

include标签

和引入CSS,JS等类似,引入另一个XML的片段,可以有效降低代码重复率

<include refid="填写要引用的SQL片段的ID"/>

ResultMap标签

ResultMap标签允许开发者制定映射规则,将SQL查询结果集中的字段和JAVA对象中的属性进行映射,处理一对一,一丢多,多对一关系时经常使用

基本用法:

<resultMap id="id名称" type="类型名称">
    <!--id标签用于映射主键-->
	<id column="column值" proerty="proerty值"/>
    <!--result标签用于映射非主键列-->
    <result column="column值" proerty="proerty值"/>
    <result column="column值" proerty="proerty值"/>
</resultMap>
属性值作用
id名称定义了唯一辨识ID
类型名称定义了要映射的JAVA类
column值指定数据库表中的列名
proerty值制定了JAVA对象中的属性名

一对多:

<resultMap id="id名称" type="类型名称">
    <!--id标签用于映射主键-->
	<id column="column值" proerty="proerty值"/>
    <!--result标签用于映射非主键列-->
    <result column="column值" proerty="proerty值"/>
    <result column="column值" proerty="proerty值"/>
    <!--collection标签主要用于Mybatis的ResultMap中,处理一对多的映射,是专门给ResultMap设计的-->
    <collection property="proerty值" ofType="类型名称">
    	<id column="column值" proerty="proerty值"/>
        <result column="column值" proerty="proerty值"/>
    </collection>
</resultMap>
属性作用
ofType制定了集合中元素的类型,通常是一个List或Set

多对一:

<resultMap id="id名称" type="类型名称">
    <!--id标签用于映射主键-->
	<id column="column值" proerty="proerty值"/>
    <!--result标签用于映射非主键列-->
    <result column="column值" proerty="proerty值"/>
    <result column="column值" proerty="proerty值"/>
    <!--当有一个对象属性引用自另一个对象时,就可以使用association标签-->
    <association property="proerty值" javaType="类型名称">
    	<id column="column值" proerty="proerty值"/>
    </association>
</resultMap>

多对一例子:

假设现在有一个student表,表中有id,name,tid。还有一个teacher表,表中有id,name

我们就可以这样定义ResultMap映射两个的关系

<!--先映射teacher-->
<resultMap id="teacherMap" type="com.heima.pojo.Teacher">
	<id column="id" property="id"/>
    <result column="name" property="name"/>
</resultMap>

<!--映射student,关联teacher-->
<resultMap id="studentMap" type="com.heima.pojo.Student">
	<id column="id" property="id"/>
    <result column="name" property="name"/>
    <association property="teacher" javaType="com.heima.pojo.Teacher" resultMap="teacherMap">
    	<id column="teacher_id" property="id"/>
        <result column="teacher_name" property="name">
    </association>
</resultMap>

<!--select语句-->
<select id="selectStudent" resultMap="studentMap">
	select s.*,t.id teacher_id,t.name teacher_name
    from student s
    left join teacher t on s.tid=t.id;
</select>

插入,更新,删除的一些特殊操作

useGeneratedKeys和keyProperty(实现@Options注解)

一般用于插入,更新,删除等操作的时候,特别是在需要自动生成主键的情况下。

useGeneratedKeys:

useGeneratedkeys是一个布尔类型的值,用于制定是否适用数据库自动生成的键,通常是主键。特别是使用的数据库支持自增逐渐的时候,如果设置为true,MyBatis会尝试使用数据库的自动生成机制来获取新生成的键值。

keyProperty:

keyProperty是一个字符串类型的值,用于指定要自动生成的目标对象的属性名称,通常与useGeneartedKeys配合使用。

使用@Options注解后或者在XML中使用了上面两个属性后,在完成数据库操作后会获取数据库自动成成的键值并将这个值设置到对应的对象属性上。

示例:

<insert id="add" useGeneratedKeys="true" keyProperty="id">
    insert into user(name, phone)
    values(#{name},#{phone})
</insert>
@RestController
@RequestMapper("/users")
public class Controller {
    
    @Autowried
    private Service service;
    
    @PostMapping
    public Result add(Param param){
        service.add(param);
        return Result.success();
    }
}


@Service
public class Service {
    
    @Autowried
    private Mapper mapper;
    
    public void add(Param param){
        mapper.add(prarm);
    }
}


@Mapper
public class Mapper {
    
    void add(Param param);
    
}

在这个示例中,当前端发送请求后,传入一个没有id的param,但是在mapper咋洗完成add方法后,所有的param都会给ID返回数据库对应的值。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值