这篇文章主要讲解了“mybatis有哪些常用方法”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“mybatis有哪些常用方法”吧!
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, 开始为(
结束为)
,中间分割符为),(
。
where限制为in的均可适用。
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是否为空并且要有数量,然后进行遍历,其实本质就是进行脚本拼接,和我们平时写法很相似。
比如:
"张三,李四,王五"
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
进行分割,其它基本相同。
如需要重复使用的sql:
<sql id="Base_Column_List">
ID, NAME, BIRTH, SEX
</sql>
复用:
select
<include refid="Base_Column_List"/>
from student where id=#{id}
include
中的refid
填充值就是上面sql
中id
的属性值。
如需要别名的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}
如查找学生名字中带有某某字样的学生:
select
<include refid="Base_Column_List"/>
from student where name like "%"#{name}"%"
如学生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有哪些常用方法这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/itazi/blog/5012684