温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

mybatis有哪些常用方法

发布时间:2021-07-10 16:15:42 来源:亿速云 阅读:278 作者:chen 栏目:编程语言

这篇文章主要讲解了“mybatis有哪些常用方法”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“mybatis有哪些常用方法”吧!

mybatis常用方法整理

1. 批量插入

java 接口:

Integer insertList(List<Student> list);

xml:

<insert id="insertList" parameterType="List">
    insert into student (
    ID, NAME, BIRTH, SEX
    ) values
    <foreach collection="list" item="item" index="index" open="(" close=")" separator="),(">
        #{item.id}, #{item.name}, #{item.birth}, #{item.sex}
    </foreach>
</insert>

其中使用foreach对list这个列表进行循环,循环中每个对象表示名称为item, 开始为(结束为),中间分割符为),(

2. 查找条件为in的情况

where限制为in的均可适用。

1. 传参为List对象

java:

List<Student> getStudentListByNames(@Param("names") List<String> names);

xml:

<select id="getStudentListByNames" resultType="com.demo.domain.Student">
    select * from student a
    <where>
        <if test="names != null and names > 0">
            a.NAME in
            <foreach collection="names" item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if> 
    </where>
</select>

我们先用test判断names是否为空并且要有数量,然后进行遍历,其实本质就是进行脚本拼接,和我们平时写法很相似。

2. 传参对象为字符串,多个查询参数以逗号拼接

比如:

"张三,李四,王五"

java:

List<Student> getStudentsByNames(@Param("names") String names);

xml:

<select id="getStudentsByNames" resultType="com.demo.domain.Student">
    select * from student a
    <where>
        <if test="names != null and names != ''">
            a.NAME in
            <foreach item="item" index="index" collection="names.split(',')" open="("
                         separator="," close=")">
                    #{item}
                </foreach>
        </if> 
    </where>
</select>

和之前的例子差不多,但是判断条件改了,因为不是列表了,还有就是collection中使用split进行分割,其它基本相同。

3. sql语句复用

1. 复用一段sql语句,避免处处编写

如需要重复使用的sql:

<sql id="Base_Column_List">
    ID, NAME, BIRTH, SEX
</sql>

复用:

select 
<include refid="Base_Column_List"/>
from student where id=#{id}

include中的refid填充值就是上面sqlid的属性值。

2. 复用sql动态填充属性值

如需要别名的sql:

<sql id="Alias_Column_List">
    ${alias}.ID, ${alias}.NAME, ${alias}.BIRTH, ${alias}.SEX
</sql>

使用t1别名:

select 
<include refid="Alias_Column_List">
    <property name="alias" value="t1"></property>
</include>
from student t1 where t1.id=#{id}

4. like的使用

如查找学生名字中带有某某字样的学生:

select 
<include refid="Base_Column_List"/>
from student where name like "%"#{name}"%"

5. 复杂返回对象resultMap的使用

如学生student有属性:id, name, class_id . 班级class有属性:id, class_name, class_teacher_id . 老师teacher(这里我们假设只有班主任,一个班级只有一个班主任)有属性: id, teacher_name .

查询班级以及和班级关联的学生和班主任。

<select id="selectClassDescInfos" resultMap="classDescInfos">

select t1.id as class_id, t1.class_name, t2.id as teacher_id, t2.teacher_name, t3.id as student_id, t3.name as student_name 
from class t1 
left join teacher t2 on t1.class_teacher_id = t2.id
left join student t3 on t1.id = t3.class_id
where class_id = #{classId}

</select>
<resultMap id="classDescInfos"
               type="com.demo.vo.ClassDescInfos">
       <id column="class_id" property="classId"></id>
            <result column="class_name" property="className"/>    
        <association property="teacher" javaType="com.demo.vo.Teacher">
            <id column="teacher_id" property="teacherId"></id>
            <result column="teacher_name" property="teacherName"/>    
        </association>
        <collection property="students" javaType="ArrayList"
                    ofType="com.demo.vo.Student">
            <id column="student_id" property="studentId"></id>
            <result column="student_name" property="studentName"/>    
        </collection>

其中com.demo.vo.ClassDescInfos的java类结构类似如下:

private String classId;
private String className;
private Teacher teacher;
private List<Student> students;

这样上述查询就可以返回一个这样的对象,都帮我们处理好了。

具体可以参考这篇文章:

https://www.cnblogs.com/rollenholt/p/3365866.html

感谢各位的阅读,以上就是“mybatis有哪些常用方法”的内容了,经过本文的学习后,相信大家对mybatis有哪些常用方法这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI