21.05.05---mybatis---学习记录

本文详细介绍了MyBatis中如何使用resultType和resultMap进行结果映射,以及如何实现动态SQL,包括if、where和foreach标签的运用,展示了在不同场景下如何根据条件构建SQL语句。同时,还提到了SQL片段的定义和复用,以提高代码的可读性和可维护性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

21.05.05

resultType结果类型

指sql语句执行完毕后,数据转为的java对象
处理方式:
1.mybatis执行sql语句,然后mybatis调用类的无参数构造方法,创建对象
2.mybatis把ResultSet指定列值付给了同名的属性

定义别名

在mybatis主配置文件中定义
第一种方式 typeAliases
可以指定一个类型 一个自定义别名
type:自定义类型的全限定名称
alias:别名

<typeAliases>
	<typeAlias type="domain.Student" alias="stu" />
</typeAliases>

第二种方式
name是包名,这个包中的所有类,类名就是别名(类名不区分大小写)

<package name="com.domain">

查询返回map

resultMap:结果映射,指定列名和java对象的属性对应关系
1)你自定义列值赋值给哪个属性
2)当你的列名和属性名不一样是,一定使用resultMap
resultMap和resultType不要一起用

1.列名是map的key,列值是map的value
2.只能最多返回一行记录
使用resultMap
1)先定义resultMap
2)在select标签,使用resultMap来引用定义
id:自定义名称,表示定义的resultMap
type:java类型的全限定名称

<resultMap id="studentMap" type="com.domain.Student">
	<!--
		主键列,使用id标签
		column:列名
		property:java类型的属性名
	-->
	<id column="" property="">
	<!--非主键类,使用result-->
	<result column="name" property="name">
</resultMap>

在这里插入图片描述

动态sql

sql的内容是变化的,可以根据条件获取到不同的sql语句,主要是where部分发生变化
(拼接sql语句)
动态sql的事项,使用的是mybatis提供的标签
1)是判断条件的
例:
dao

List<Student> selectStudentIf(Student student);

dao.xml

<select id="selectStudentIf" resultType="com.node.domain.Student">
        select id,name,age,email from student
        where 1=1
        <if test="name!=null and name!='' ">
            and name=#{name}
        </if>
        <if test="age>0">
            and age>#{age}
        </if>
    </select>

测试代码

@Test
    public void testSelectById(){
        SqlSession sqlSession= MyBatisUtils.getSqlSession();
        StudentDao dao=sqlSession.getMapper(StudentDao.class);

        Student student =new Student();
        student.setName("lisi");
        student.setAge(18);
        List<Student> students=dao.selectStudentIf(student);
        for(Student stu:students){
            System.out.println("if=="+stu);
        }

    }

2)用来包含多个的,当多个if有一个成立的,会自动增加一个where关键字,并去掉if中多余的and,or等。
例:
dao

List<Student> selectStudentWhere(Student student);

dao.xml

<select id="selectStudentWhere" resultType="com.node.domain.Student">
        select id,name,age,email from student
        <where>
            <if test="name!=null and name!='' ">
                name=#{name}
            </if>
            <if test="age>0">
                or age>#{age}
            </if>
        </where>
    </select>

测试代码

@Test
    public void testSelectByWhere(){
        SqlSession sqlSession= MyBatisUtils.getSqlSession();
        StudentDao dao=sqlSession.getMapper(StudentDao.class);

        Student student =new Student();
        student.setName("lisi");
        student.setAge(18);
        List<Student> students=dao.selectStudentWhere(student);
        for(Student stu:students){
            System.out.println("if=="+stu);
        }

    }

3)循环java中的数组,list集合的。主要用于sql的in语句中

collection:表示传入过来的参数的数据类型。该参数为必选。
item: 自定义的 循环体中的具体对象。在 list 和数组中是其中的对象,在 map 中是 value,该参数为必选。(它是每一个元素进行迭代时的别名)
open:表示该语句以什么开始
close:表示该语句以什么结束
separator:表示在每次进行迭代之间以什么符号作为分隔符

例1:
dao

 //foreach 用法一
    List<Student> selectForeachOne(List<Integer> idlist);

dao.xml

 <select id="selectForeachOne" resultType="com.node.domain.Student">
        select * from student where id in
        <foreach collection="list" item="myid" open="(" close=")" separator=",">
            #{myid}
        </foreach>
    </select>

测试代码

@Test
    public void testSelectForEach(){
        SqlSession sqlSession= MyBatisUtils.getSqlSession();
        StudentDao dao=sqlSession.getMapper(StudentDao.class);

        List<Integer> list=new ArrayList<>();
        list.add(1001);
        list.add(1002);
        list.add(1003);

        List<Student> students=dao.selectForeachOne(list);
        for(Student stu:students){
            System.out.println("foreach--one"+stu);
        }

    }

例2:
dao

//foreach 用法二
    List<Student> selectForeachTwo(List<Student> stuList);

dao.xml

<select id="selectForeachTwo" resultType="com.node.domain.Student">
        select * from student where id in
        <foreach collection="list" item="stu" open="(" close=")" separator=",">
            #{stu.id}
        </foreach>
    </select>

测试代码

@Test
    public void testSelectForEachTwo(){
        SqlSession sqlSession= MyBatisUtils.getSqlSession();
        StudentDao dao=sqlSession.getMapper(StudentDao.class);

        List<Student> stuList =new ArrayList<>();
        Student s1=new Student();
        s1.setId(1002);
        stuList.add(s1);

        s1=new Student();
        s1.setId(1003);
        stuList.add(s1);

        List<Student> students=dao.selectForeachTwo(stuList);
        for(Student stu:students){
            System.out.println("foreach--one"+stu);
        }

    }

sql片段

sql代码片段,就是复用一些语句
1)先定义 sql语句 ,表明,字段等
2)再使用,

<!--定义sql片段-->
    <sql id="studentSql">
        select id,name,age,email from student
    </sql>
    
    <select id="selectStudentIf" resultType="com.node.domain.Student">
        <include refid="studentSql"></include>
        ..........
     </select>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值