Mybatis参数与返回值问题

参数问题

最便捷的方法是在传的参数前面加上标记,使用@param注解来进行参数的唯一标记

返回值问题
返回值为单个属性的时候
//接口中方法设计,返回值为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>
返回值为多个属性的时候

方式一:采用对象形式

//接口中方法设计,返回值为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(与跟它对应的sql标签中的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: 查询结束之后的返回类型

<select id="login" resultMap="userResultMap">
        SELECT u.*, r.id rid, r.name rname
        FROM user u
                 LEFT JOIN user_role ur ON u.id = ur.uid
                 LEFT JOIN role r ON r.id = ur.rid
        WHERE u.loginname = #{name}
          AND u.pwd = #{pwd}
    </select>

 <resultMap id="userResultMap" type="com.xszx.beans.User">
        <id property="id" column="id" />
        <result property="name" column="name" />
        <result property="loginname" column="loginname" />
        <result property="pwd" column="pwd" />
        <result property="sex" column="sex" />
        <result property="age" column="age" />
        <result property="hobby" column="hobby" />

        <association property="role" javaType="com.xszx.beans.Role">
            <id property="id" column="rid" />
            <result property="name" column="rname" />
        </association>
    </resultMap>

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

id标签:对方表中的主键

column:数据表中的列名称

property:实体类中的属性

result标签:其他属性

column:数据表中的列名称

property:实体类中的属性

一对多集合映射

如果返回值中有其他对象的属性时使用

属性:

id:固定id

type:查询结果的返回类型

子标签:

id:表中的主键

column:数据表中的列名称

property:实体类中的属性

result:其他属性

column:数据表中的列名称

property:实体类中的属性

collection: 集合映射标签 出现在“一”方

column:数据表中的列名称

property:实体类中的属性

select: 需要指定命名空间.id去查询关联对象

ofType: 查询结束之后的返回类型

方式一:

发送多次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>
方式二(推荐使用):

发送一次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>

一对多与多对一的区别:

两者都是先写一个select标签,然后在select的标签内需要写上resultMap属性,写完后需要另外写一个resultMap标签,并且该标签的id值要等于select标签中的resultMap的值。

之后在resultMap中的type属性中写上此方法返回的值,里面的内容就是写需要返回的字段名字(要注意字段名和列名与其对应的值要一致),id标签表示主键,result标签表示除主键外的其他字段。

一对多与多对一最大的不同就是一对多映射resultMap标签中的获取其他对象值的标签是collection,而多对一映射resultMap标签中的获取其他对象值的标签是association

  • 15
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis 中进行批量更新操作,可以使用 `SqlSession` 的 `update` 方法来执行。是需要注意的是,批量更新操作的返回值是一个整数,表示被修改的记录数。 具体的步骤如下: 1. 获取 `SqlSession` 对象,可以通过 `SqlSessionFactory` 创建。 2. 创建一个包含多个更新操作的 `List`,每个更新操作可以使用相同的 SQL 语句或不同的 SQL 语句。 3. 使用 `SqlSession` 的 `update` 方法执行批量更新操作,并将包含多个更新操作的 `List` 作为参数传入。 4. 提交事务,可以通过 `SqlSession` 的 `commit` 方法来提交,或者在配置文件中设置自动提交。 5. 获取返回值,批量更新操作的返回值是一个整数,表示被修改的记录数。 示例代码如下: ```java SqlSession sqlSession = sqlSessionFactory.openSession(); try { List<MyObject> myObjects = new ArrayList<>(); // 添加多个要更新的对象到 myObjects 列表中 int affectedRows = sqlSession.update("updateStatementId", myObjects); // updateStatementId 是定义在 Mapper XML 文件中的更新 SQL 语句的 id sqlSession.commit(); System.out.println("Affected Rows: " + affectedRows); } finally { sqlSession.close(); } ``` 需要注意的是,批量更新操作的返回值是所有更新操作影响的记录数之和,并不会返回每个具体操作的结果。如果需要获取每个具体操作的结果,可以考虑在执行批量更新之前,先进行单独的批量查询操作来获取相关数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值