Mybatis高级

动态sql

if

格式:

<if test="条件">
    sql 语句
</if>
当条件成立的时候,会执行sql语句

choose…when…otherwise…

格式:

<choose>
    <when test="条件1"> 
    	sql语句1
    </when>
    
    <when test="条件2"> 
    	sql语句2
    </when>
    
    <otherwise>
        sql语句3
    </otherwise>
</choose>

和我们java的if...else if ...else格式一样
当条件1成立,那么就不会执行后面的代码

where

  • 说明
    • 标签里边的if 至少有一个成立,就会动态添加一个where,如果都不成立,不添加where
    • 第一个if条件成立的,会自动去除连接符and 或者 or
<where>
<if test="条件">
    sql 语句
</if>
...
</where>

set

说明: 动态添加了set字段,也会动态的去掉最后一个逗号
格式:

    <set>
        <if test="条件">
    		sql 语句
		</if>
		...
    </set>
    where 条件

foreach

格式:

循环遍历标签。适用于多个参数或者的关系。比如sql中的 in(1,2,3)
<foreach collection=“”open=“”close=“”item=“”separator=“”>
    获取参数
</foreach>

trim

格式:
prefix:前缀,比如where,set
suffix:后缀,
prefixoverrides:前缀删除第一个 比如and
suffixoverrides:后缀删除最后一个 比如,

<trim prefix=''   prefixoverrides='' suffix=''   suffixoverrides=''>

Sql片段

使用的时候
格式:

<sql id="别名">
    查询的所有字段
</sql>

二、分页

1 导入maven依赖

<!--        pageHelper依赖-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>4.1.6</version>
        </dependency>
        <dependency>
            <groupId>com.github.jsqlparser</groupId>
            <artifactId>jsqlparser</artifactId>
            <version>0.9.6</version>
        </dependency>

2 mybatis配置文件中指定方言

<plugins>
    <!-- 注意:分页助手的插件  配置在通用mapper之前 -->
    <plugin interceptor="com.github.pagehelper.PageHelper">
        <!-- 指定方言 -->
        <property name="dialect" value="mysql"/>
    </plugin>
</plugins>

3 java代码测试

@Test
public void test02(){

    User user = new User();
    PageHelper.startPage(1, 3);
    List<User> users = userMapper.queryUsersByCondition(user);
    PageInfo<User> pageInfo = new PageInfo<User>(users);

    System.out.println("总条数:"+pageInfo.getTotal());
    System.out.println("总页数:"+pageInfo.getPages());
    System.out.println("当前页:"+pageInfo.getPageNum());
    System.out.println("每页显示长度:"+pageInfo.getPageSize());
    System.out.println("是否第一页:"+pageInfo.isIsFirstPage());
    System.out.println("是否最后一页:"+pageInfo.isIsLastPage());
    List<User> list = pageInfo.getList();
    for (User user1 : list) {
        System.out.println(user1);
    }

}

三、 mybatis多表查询

1.一对一

场景: 一个用户有一个关联的详细信息。
1、创建 Java 类

// User.java
public class User {
    private Long id;
    private String username;
    private UserProfile profile; // 一对一关联
    // 其他属性和对应的 getter 和 setter 方法
}

// UserProfile.java
public class UserProfile {
    private Long userId;
    private String email;
    private String phone;
    // 其他属性和对应的 getter 和 setter 方法
}

2、创建 Mapper 接口:

// UserMapper.java
public interface UserMapper {
    User getUserWithProfile(Long userId);
}

3.、创建 MyBatis 映射文件:

<!-- UserMapper.xml -->
<resultMap id="userResultMap" type="com.example.model.User">
    <id property="id" column="id" />
    <result property="username" column="username" />
    <association property="profile" column="user_id" javaType="com.example.model.UserProfile">
        <id property="userId" column="user_id" />
        <result property="email" column="email" />
        <result property="phone" column="phone" />
    </association>
</resultMap>

<select id="getUserWithProfile" resultMap="userResultMap">
    SELECT u.id, u.username, up.user_id, up.email, up.phone
    FROM users u
    LEFT JOIN user_profiles up ON u.id = up.user_id
    WHERE u.id = #{userId}
</select>

2.一对多

场景: 一个用户可以有多个订单。
1、创建 Java 类:

// User.java (同上)

// Order.java
public class Order {
    private Long id;
    private Long userId;
    private String productName;
    // 其他属性和对应的 getter 和 setter 方法
}

2、创建 Mapper 接口:

// UserMapper.java
public interface UserMapper {
    User getUserWithProfile(Long userId);
}

3、创建 MyBatis 映射文件:

<!-- UserMapper.xml -->
<resultMap id="userResultMap" type="com.example.model.User">
    <id property="id" column="id" />
    <result property="username" column="username" />
    <collection property="orders" ofType="com.example.model.Order">
        <id property="id" column="order_id" />
        <result property="productName" column="product_name" />
    </collection>
</resultMap>

<select id="getUserWithOrders" resultMap="userResultMap">
    SELECT u.id, u.username, o.id AS order_id, o.product_name
    FROM users u
    LEFT JOIN orders o ON u.id = o.user_id
    WHERE u.id = #{userId}
</select>

3.多对多

场景: 一个用户可以有多个角色,一个角色也可以对应多个用户。
1、创建 Java 类:

// User.java (同上)

// Role.java
public class Role {
    private Long id;
    private String roleName;
    // 其他属性和对应的 getter 和 setter 方法
}


2、创建中间表
假设中间表为 user_roles,包含 user_id 和 role_id 字段。
3、创建 Mapper 接口:

// UserMapper.java
public interface UserMapper {
    User getUserWithProfile(Long userId);
}

4、创建 MyBatis 映射文件:

<!-- UserMapper.xml -->
<resultMap id="userResultMap" type="com.example.model.User">
    <id property="id" column="id" />
    <result property="username" column="username" />
    <collection property="roles" ofType="com.example.model.Role">
        <id property="id" column="role_id" />
        <result property="roleName" column="role_name" />
    </collection>
</resultMap>

<select id="getUserWithRoles" resultMap="userResultMap">
    SELECT u.id, u.username, r.id AS role_id, r.role_name
    FROM users u
    LEFT JOIN user_roles ur ON u.id = ur.user_id
    LEFT JOIN roles r ON ur.role_id = r.id
    WHERE u.id = #{userId}
</select>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值