Mybatis笔记的后续补充

返回值问题

1.返回值为单个属性的时候

//接口中方法设计,返回值为String:
String getUser(int id);
//XML中语法:
<select id="getUser" parameterType="int" resultType="String">
    select username from t_user where id = #{id}
</select>
//测试类执行:
String username = userDao.getUser(2);
//返回int类型
int id  =  userMapper.getuserid(336);

<select id="getuserid"  resultType="int" parameterType="int">
    select id from t_user where id=#{id}
</select>

2.返回值为多个属性的时候

方式一:采用对象形式

//接口中方法设计,返回值为User:
User getUser(int id);
//XML中语法:
<select id="getUser" parameterType="int" resultType="com.zx.entity.User">
    select username,age from t_user where id = #{id}
</select>
//测试类执行:
User user = userDao.getUser(2);

方式二:采用map形式处理返回结果

//接口中方法设计,返回值为HashMap:
HashMap getUser02(int id);
//XML中语法:
<select id="getUser02" parameterType="int" resultType="map">
    select username,age from t_user where id = #{id}
</select>
//测试类执行:
HashMap map = userDao.getUser02(2);

返回map的时候,键是属性名称,值就是具体的值,只能返回一条数据,返回多条就会报错

resultType一般都是返回已有的类型,或者你写好的实体类型,可以直接对应的类型。若是字段或者类型,或者不是能很好的直接对应,就需要用自定义对应的类型。

多对一关联映射

resultMap 如果返回值中有其他对象的属性时使用
属性:
id:固定id
type:查询结果的返回类型
子标签:
id:表中的主键
column:数据表中的列名称
property:实体类中的属性
result:其他属性
column:数据表中的列名称
property:实体类中的属性
方式一:发送多次sql语句,每张表进行单独查询,通过select属性向对方映射文件中进行查询

association: 关联标签 出现在“多”方
column:数据表中的列名称
property:实体类中的属性
select: 需要指定命名空间.id去查询关联对象
javaType: 查询结束之后的返回类型

<select id="getUser04" parameterType="int" resultMap="userResult">
    select * from t_user where id = #{id}
</select>
<resultMap id="userResult" type="com.zx.entity.User">
    <id column="id" property="id"></id>
    <result column="username" property="username"></result>
    <result column="password" property="password"></result>
    <result column="age" property="age"></result>
    <association column="gid" property="group" select="com.zx.dao.GroupDao.getGroupByid" javaType="com.zx.entity.Group"></association>
</resultMap>

这个方法需要注意,select放的是你要二次执行的接口,(简单来说就是一个完整的查询接口,需要有mapper,有xml),查询的内容通常是用了关联字段或外键,没有中间表,但是查询完成以后,还是可以显示关联字段的所有信息。若是没有关联字段,任然需要联查

方式二:发送一次sql语句,进行联表查询,需要注意SQL语句的性能,不需要使用select属性

association: 关联标签 出现在“多”方
column:数据表中的列名称
property:实体类中的属性
javaType: 查询结束之后的返回类型

association打开,可以嵌入子标签:

id:对方表中的主键
column:数据表中的列名称
property:实体类中的属性
result:其他属性
column:数据表中的列名称
property:实体类中的属性

<select id="getUser05" parameterType="int" resultMap="userResult05">
    select u.id uid,u.username username,u.password password,u.age age,g.id gid,g.gname gname from t_user u,t_group g where u.gid = g.id and u.id = #{id};
</select>

<resultMap id="userResult05" type="com.zx.entity.User">
    <id column="uid" property="id"></id>
    <result column="username" property="username"></result>
    <result column="password" property="password"></result>
    <result column="age" property="age"></result>
    <association column="gid" property="group" javaType="com.zx.entity.Group">
        <id column="gid" property="id"></id>
        <result column="gname" property="gname"></result>
    </association>
</resultMap>
一对多集合映射

方式一:发送多次sql语句,每张表进行单独查询,通过select属性向对方映射文件中进行查询

<select id="getGroupByid02" resultMap="groupResult02" parameterType="int">
    select * from t_group where id = #{id}
</select>

<resultMap id="groupResult02" type="com.zx.entity.Group">
    <id column="id" property="id"></id>
    <result column="gname" property="gname"></result>
    <collection column="id" property="users" select="com.zx.dao.UserDao.getUsersByGid" ofType="com.zx.entity.User" ></collection>
</resultMap>

对象对应多个用户 用list展示

方式二:发送一次sql语句,进行联表查询,需要注意SQL语句的性能,不需要使用select属性

collection打开,可以嵌入子标签:

id:对方表中的主键
column:数据表中的列名称
property:实体类中的属性
result:其他属性
column:数据表中的列名称
property:实体类中的属性

<select id="getGroupByid03" resultMap="groupResult03" parameterType="int">
    select g.id gid,g.gname gname,u.id uid,u.username username,u.password password,u.age age from t_user u,t_group g where u.gid = g.id and g.id = #{gid}
</select>

<resultMap id="groupResult03" type="com.zx.entity.Group">
    <id column="gid" property="id"></id>
    <result column="gname" property="gname"></result>
    <collection column="gid" property="users" ofType="com.zx.entity.User" >
        <id column="uid" property="id"></id>
        <result column="username" property="username"></result>
        <result column="password" property="password"></result>
        <result column="age" property="age"></result>
    </collection>
</resultMap>

动态SQL

<select id="getUsersif" resultType="com.zx.entity.User" parameterType="com.zx.entity.User">
    select * from t_user where 1=1
    <if test="username != null">
        and username = #{username}
    </if>
    <if test="age != 0">
        and age = #{age}
    </if>
</select>

<select id="getUserschoose" resultType="com.zx.entity.User" parameterType="com.zx.entity.User">
    select * from t_user where 1=1
choose就是一个分支选择标签,when就是每一个分支,不管有多少分支,只会执行一个。
<choose>
        <when test="username!=null">
            and username = #{username}
        </when>
        <when test="age!=0">
            and age = #{age}
        </when>
默认选项,所有选项不匹配则进入。
        <otherwise>
            and password = #{password}
        </otherwise>
    </choose>
</select>

<select id="getUserswhere" resultType="com.zx.entity.User" parameterType="com.zx.entity.User">
    select * from t_user   where  1=1
    <where>他就是咱们的字段 where 
<where>标签会自动处理WHERE子句中的AND和OR关键字,因此在使用时应该合理安排条件语句的位置,避免出现意外结果。
Where只能去除最前面的关键字,他会自动去除第个条件的and或者or 的关键字,
        <if test="username != null">
            and username = #{username}
        </if>
        <if test="age != 0">
            and age = #{age}
        </if>
    </where>
</select>

<select id="getUserstrim" resultType="com.zx.entity.User" parameterType="com.zx.entity.User">
    select * from t_user
        <trim prefix="where" prefixOverrides="and" suffix="order by">
            <if test="username != null">
                and username = #{username}
            </if>
            <if test="age != 0">
                and age = #{age}
            </if>
        </trim>
        id desc
</select>

<update id="updateUser" parameterType="com.zx.entity.User">
    update t_user
    <set>
        <if test="username != null">
            username = #{username},
        </if>
        <if test="age != 0">
            age = #{age},
        </if>
        <if test="password != null">
            password = #{password},
        </if>
    </set>
    where id = #{id}
</update>

<select id="getUsersforeach" resultType="com.zx.entity.User">
    select * from t_user where id in <foreach collection="array" item="id" open="(" close=")" separator=",">#{id}</foreach>
</select>

<insert id="addUser" >
    <foreach collection="list" item="user" separator=";">
        insert into t_user values (null,#{user.username},#{user.password},#{user.age})
    </foreach>
</insert>

Insert into user(username,pwd,) values(username,pwd,)

标签也是Mabits动态SQL中常用的一个标签,主要用于快速生成包含WHERE、SET等关键字的SQL语句,同时还能够自动处理SQL语句中的逗号(,)和AND/OR等连接符。

标签通常包含以下属性:

prefixOverrides:表示在生成SQL语句前需要忽略的字符前缀。

suffixOverrides:表示在生成SQL语句后需要忽略的字符后缀。

prefix:表示在生成SQL语句前需要添加的字符串。

suffix:表示在生成SQL语句后需要添加的字符串。

suffixOverrides:表示在生成SQL语句中需要忽略掉的字符串。 // as

<select id="getUserstrim" resultType="com.zx.entity.User" parameterType="com.zx.entity.User">
    select * from t_user
        <trim prefix="where" prefixOverrides="and" suffix="order by">
            <if test="username != null">
                and username = #{username}
            </if>
            <if test="age != 0">
                and age = #{age}
            </if>
        </trim>
        id desc
</select>

通过使用标签和标签,可以根据用户传入的参数动态生成SQL语句的SET子句。如果参数不为空,则会包含相应的更新内容;否则,该更新内容会被忽略

需要注意的是,由于标签会自动处理SET子句中的逗号(,),因此在使用时应该合理安排更新内容的位置,避免出现意外结果

标签通过将SET子句中的所有条件用逗号(,)连接起来,可以根据不同的更新情况动态生成满足需求的SQL语句

<update id="updateUser" parameterType="com.zx.entity.User">
    update t_user
    <set>
        <if test="username != null">
            username = #{username},
        </if>
        <if test="age != 0">
            age = #{age},
        </if>
        <if test="password != null">
            password = #{password},
        </if>
    </set>
    where id = #{id}
</update>

标签是 MyBatis 中的一个迭代标签,可以对集合对象进行遍历,并生成多条 SQL 语句(如 INSERT、UPDATE、DELETE 等)。

批量添加,导入文件exsl 多条 添加多条,查询列表得到list,把list的内容添加到别的表中

在java代码中遍历添加,sql

批量删除,

使用 标签,我们可以将一个集合对象的元素依次取出,作为 SQL 语句中的参数进行插入、更新或删除操作。常用的语法如下:

其中,各个属性和元素的含义如下

collection:指定要遍历的集合对象的属性名。 参数名

item:指定在遍历过程中每个元素所对应的变量名。 起名字,对应变量名

separator:指定在生成多条 SQL 语句时,不同语句之间的分隔符,默认为英文逗号。

open:指定生成的 SQL 语句的头部。

close:指定生成的 SQL 语句的尾部。

List是单个参数

Item是单个参数的时候用单个参数,

List是对象,

则item对应的是对象名,用法是对象.内容

<select id="getUsersforeach" resultType="com.zx.entity.User">
    select * from t_user where id in <foreach collection="array" item="id" open="(" close=")" separator=",">#{id}</foreach>
</select>

<insert id="addUser" >
    <foreach collection="list" item="user" separator=";">
        insert into t_user values (null,#{user.username},#{user.password},#{user.age})
    </foreach>
</insert>
  • 10
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值