对MyBatis的基础理解

本文详细解读了MyBatis如何通过XML或注解配置SQL映射,包括<select>、<insert>、<update>、<delete>和<resultMap>的使用,以及配置文件中<mapper>元素和动态SQL的运用实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

MyBatis 使用简单的 XML注解用于配置和原始映射

MyBatis 的工作原理

image-20211210150250510

映射器

image-20211214143148739

  • <select> 元素用于映射SQL的select语句
  • <insert> 元素用于映射插入语句
  • <update><delete> 元素用于映射更新和删除语句
  • <sql> 元素的作用在于可以定义 SQL 语句的一部分(代码片段)
  • <resultMap> 元素表示结果映射集,是 MyBatis 中最重要也是最强大的元素

配置

<configuration>
    <!-- 告诉 MyBatis到哪里去找映射文件 -->
    <mappers>
        <mapper resource="easymall/mybatis/UserMapper.xml"/>
        <mapper resource="easymall/mybatis/ProductsMapper.xml"/>     
    </mappers>
</configuration>

select

image-20211214143434266

<!-- 根据uid查询一个用户信息 -->
<select id="selectUserById" parameterType="Integer" resultType="com.po.MyUser">
    select * from user where uid = #{uid}
</select>

insert

<!-- 添加一个用户,成功后将主键值回填给uid(pojo类的属性)-->
<insert id="addUserKey" parameterType="com.pojo.MyUser" keyProperty="uid"  useGeneratedKeys="true">
    insert into user (uname,usex) values(#{uname},#{usex})
</insert>

image-20211214145011906

update 和 delete

<!-- 修改一个用户 -->
<update id="updateUser" parameterType="com.po.MyUser">
    update user set uname = #{uname},usex = #{usex} where uid = #{uid}
</update>
    <!-- 删除一个用户 -->
<delete id="deleteUser" parameterType="Integer"> 
    delete from user where uid = #{uid}
</delete>

sql

<sql id="comColumns">uid,uname,usex</sql>

<select id="selectUser" resultType="com.po.MyUser">
    select <include refid="comColumns"/> from user
</select>

resultMap

<resultMap> 元素的 type 属性表示需要的 POJO,id 属性是 resultMap 的唯一标识。
子元素 <constructor> 用于配置构造方法(当 POJO 未定义无参数的构造方法时使用)。
子元素 <id> 用于表示哪个列是主键。
子元素 <result> 用于表示POJO和数据表普通列的映射关系。
子元素 <association><collection><discriminator> 是用在级联的情况下。

<resultMap type="" id="">
    <constructor><!-- 类在实例化时,用来注入结果到构造方法 -->
	<idArg/><!-- ID参数,结果为ID -->
	<arg/><!-- 注入到构造方法的一个普通结果 -->
    </constructor>
    <id/><!-- 用于表示哪个列是主键 -->
    <result/><!-- 注入到字段或JavaBean属性的普通结果 -->
    <association property=""/><!-- 用于一对一关联 -->
    <collection property=""/><!-- 用于一对多、多对多关联 -->
    <discriminator javaType=""><!-- 使用结果值来决定使用哪个结果映射 -->
        <case value=""/>	<!-- 基于某些值的结果映射 -->
    </discriminator>
</resultMap>

例子

ProductsMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<mapper namespace="easymall.dao.ProductsDao">
<select id="allcategories" resultType="String">
	select distinct(category) from products
</select>

<select id="prodlist" resultType="easymall.po.Products" parameterType="map">
	select * from products where (price between #{minPrice} and #{maxPrice})
	<if test="name!=null and name!=''">
		and name like concat('%',#{name},'%')
	</if>
	<if test="category!=null and category!=''">
		and category=#{category}
	</if>
</select>

<select id="oneProduct" parameterType="String" resultType="easymall.po.Products">
	select * from products where id=#{pid}
</select>

<select id="proclass" parameterType="String" resultType="easymall.po.Products">
	select * from products where category=#{category}
</select>

</mapper>

UserMapper.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="easymall.dao.UserDao">
<!-- 用户登录功能,返回User类对象 -->
<select id="login" parameterType="String" resultType="easymall.po.User">
	select * from user where username=#{username}
</select>
<insert id="regist" parameterType="easymall.po.User" keyProperty="id" useGeneratedKeys="true">
    insert into user(username,password,nickname,email) values(#{username},#{password},#{nickname},#{email})
</insert>

</mapper>

动态SQL

image-20211214155416730

<if> 元素是最常用的元素。它类似于 Java 中的 if 语句。

<trim> 元素的主要功能是可以在自己包含的内容前加上某些前缀,也可以在其后加上某些后缀

<where> 元素的作用是会在写入 <where> 元素的地方输出一个 where 语句

在动态 update 语句中,可以使用 <set> 元素动态更新列

<foreach> 元素主要用在构建 in 条件中,它可以在 SQL 语句中进行迭代一个集合

<bind> 元素解决 SQL 映射文件实现不同数据库的拼接函数或连接符号不同的问题

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值