Mybatis

1.模糊查询和动态sql语句

在查询的时候我们需要用到resultMap关键字,这样查出来的每一行的数据都会映射这个类型的对象,再resultMap中最好写类的全名,这样肯定不会出错,但是不写类的全名有可能会出错,在查询的时候一般会用到条件语句,我们mybatis提供了一些动态SQL语句供我们使用,是一些动态sql的标签例如:where,if,bind 等等  

<select id="getStaff" resultType="com.easy.bean.Staff">
select * from staff
<!-- 根据参数不同,组合出不同的sql语句  动态SQL语句   提供很多动态SQL语句标签 -->
<where>
<!-- 编写条件语句,如果where标签中有内容  会自动添加where关键字 -->
<if test="checktext !=null and checktext!=''">
<!-- 重新定义参数内容 -->
<bind value="'%'+checktext+'%'" name="liketext"></bind>
             name like #{liketext}

</if>
</where>
</select>

上面bind标签中是重新定义参数内容,这样传入的参数前面或者后边可以有任意个字符,就达到了模糊查询的条件

在接口中我们就需要有与上面id相同的方法名

List<Staff> getStaff(String checktext);

在controller类里面 

    @GetMapping("staff")
    public CommonResult getStraff(String checktext) {
        List<Staff> list=dao.getStaff(checktext)
        return CommonResult.success(list);
    }

2.修改数据 

mapper文件:

<update id="editStaffItem">
    update staff
    <set>
        <if test='name!=null and name!=""'>
            name=#{name},
        </if>
        <if test="salary!=null">
            salary=#{salary},
        </if>
    </set>
    <where>
        id=#{id}
    </where>
</update>

接口: 

int editStaffItem(Staff staff);

controller类: 

@PutMapping("staffitem")
public String editStaffItem(Staff staff) {
    dao.editStaffItem(staff);
    return "success";
}

 3.一对一/一对多查询

一对一关联查询:

        通过<resultMap>元素的子元素<association>处理一对一级联关系

        <association>元素中通常使用以下属性:

                property:指定映射到实体类的对象属性

                column:指定表中对应的字段(即查询返回的列名)

                javaType:指定映射到实体对象属性的类型

                select:指定引入嵌套查询的子SQL语句,该属性用于关联映射中的嵌套查询

<!-- 一对一查询或者一对多查询需要指定映射方式 -->
<resultMap type="com.easy.bean.Staff" id="satffAndDep">
<association property="dep"  column="dep_id" select="getStaffDep" ></association>
</resultMap>
<select id="getStaffDep" resultType="com.easy.bean.Department">
select * from department where id=#{dep_id};
</select>
<select id="getStaffAndDep" resultMap="satffAndDep">

select * from staff
</select>

一对多关联查询:

<resultMap type="com.easy.bean.Department" id="departmentAndStaff">
  <!-- 把id列映射成com.easy.bean.Department的depid -->
<!--<id column="id" property="depid"></id>
<result column="name" property="depname"></result>-->
<result column="id" property="id"></result>
<collection  column="id" select="depstaff" property="stafflist"></collection>
</resultMap>

<select id="depstaff" resultType="com.easy.bean.Staff">
select * from staff where dep_id=#{id}
</select>
<select id="getDep" resultMap="departmentAndStaff">
select * from department
</select>

 上面的result标签是将id在映射到list中,

List<Department> getDep();
	@GetMapping("depart")
	//@Transactional
	//一级缓存要保证是一个SQLsession
	public CommonResult getDep() {
		List<Department> list =e.getDep();
		System.out.println("------------");
		list =e.getDep();
		return CommonResult.success(list);
	}
//这里查询的第二遍是为了做一级缓存的,已经注释掉的注解是要确保这个是一个事务,如果只做查询,中间两句不看

 

 4.筛选查询

mapper文件:

choose...when...otherwise相当于if...else,choose和when一起使用

<select id="getstaffBysalary" resultType="com.easy.bean.Staff">
select * from staff
<where><!-- 参数名 salarytext -->

<choose>
<when test='salarytext == "低"'>
salary &lt;5000
</when>
<when test='salarytext == "中"'>
salary &gt;5000 and salary &lt;=8000 
</when>
<otherwise>salary &gt;8000</otherwise>
</choose>
</where>
</select>

接口中:

需要将String类型的参数输入,将符合的放入list集合中,然后返回Staff类型的 

List<Staff> getstaffBysalary(String salarytext);

 controller类:

 @GetMapping("staff/salarytext")
	    public CommonResult getstaffBysalary(String salarytext) {
	    	List<Staff> list=dao.getstaffBysalary(salarytext);
	    	return CommonResult.success(list);
	    	
	    }

 这样在查询的时候输入参数低就会输出低于5000的信息

5.批量添加数据:

<insert id="addlist">
insert into staff (code,name,salary,username,userpass)
values
<foreach collection="list" item="item"  separator=",">
     (#{item.code},#{item.name},#{item.salary},#{item.username},#{item.userpass})
</foreach>
</insert>
int addlist(List<Staff> staff);
    @PostMapping("staff")
	    public String addStaff(Staff staff){
	    	staff=new Staff();
	    	staff.setCode("10001");
	    	staff.setName("李思思");
	    	staff.setSalary(new BigDecimal(2000));
	    	staff.setUsername("lisisi");
	    	staff.setUserpass("123123");
	    	List list=new ArrayList();
	    	list.add(staff);
	    	staff=new Staff();
	    	staff.setCode("10002");
	    	staff.setName("李思思878787");
	    	staff.setSalary(new BigDecimal(2000));
	    	staff.setUsername("lisdfjhigijisi");
	    	staff.setUserpass("123154523");
	    	list.add(staff);
	    	
	    	
	    	//dao.addStaff(staff);
	    	dao.addlist(list);
	    	return "success";
	    }

 这样就完成了批量添加多条数据

 6.resultType和resultMap的区别:

MyBatis的每一个查询映射的返回类型都是resultMap,只是当我们提供的返回类型是resultType时,MyBatis会自动把对应的值赋给resultType所指定对象的属性,而当我们提供的返回类型是resultMap时,MyBatis会将数据库中的列数据复制到对象的相应属性上,可用于复制查询。

7.一级缓存和二级缓存

缓存就是指存在内存中的临时数据,使用缓存能够减少和数据库交互的次数,提高效率。将相同查询条件的sql语句执行一遍后得到的结果存在内存或者某种缓存介质中,当下次遇到一模一样的查询sql时候不在执行sql与数据库交互,而是直接从缓存中获取结果,减少服

务器的压力;

mybatis缓存包括:

● ⼀级缓存:将查询到的数据存储到SqlSession中。范围比较小,正对于一次sql会话

● ⼆级缓存:将查询到的数据存储到SqlSessionFactory中。范围比较大,针对于整个数据库级别的。

● 或者集成其它第三⽅的缓存:⽐如EhCache【Java语⾔开发的】、Memcache【C语⾔开发的】等。
缓存只针对于DQL语句,也就是缓存机制只对应select语句。

8.接收多个参数

如果想接收多个参数需要使用@Param注解给每个参数起一个别名

9. <resultMap> 的一些关键特性和用法:

1.映射结果集:<resultMap> 允许您将 SQL 查询的结果集列映射到 Java 对象的属性。


类型安全:通过使用 <resultMap>,您可以确保类型安全,因为 MyBatis 会自动处理从数据库列到 Java 对象属性的转换。


复杂映射:<resultMap> 支持复杂的映射需求,例如关联查询和嵌套查询。

提高性能:使用 <resultMap> 可以提高性能,因为它允许 MyBatis 重用预先配置的映射规则。

扩展性:<resultMap> 可以继承其他 <resultMap>,实现映射规则的重用和扩展。

配置方式:<resultMap> 通常在 MyBatis 的映射 XML 文件中定义。

id 属性:每个 <resultMap> 必须有一个唯一的 id 属性,用于标识这个映射规则。

result 元素:<resultMap> 内部使用 <result> 元素来指定列到属性的映射。

association 元素:使用 <association> 元素来映射关联查询的结果,例如一对一关系。

collection 元素:使用 <collection> 元素来映射集合查询的结果,例如一对多或多对多关系。

discriminator 元素:使用 <discriminator> 元素来实现基于某列值的区分映射,常用于处理继承或多表查

  • 20
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值