mybatis注解的方式映射字段名,可复用

5 篇文章 0 订阅
4 篇文章 0 订阅

在使用mybatis做查询的时候,我的数据库字段和实体类的字段名称并不一样,不知道为什么一样可以查询成功并对应去映射存值,但是这几天项目做其他功能的时候,突然间项目的字段映射又不成功了,没找到原因,但是为了防止错误,还是自己把映射加上了


import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface TemperatureMapper {
	// 根据id查询
	@Results({ 
			@Result(property = "createTime", column = "create_time"),
			@Result(property = "photoUrl", column = "photo_url"),
			@Result(property = "isNormal", column = "is_normal") })
	@Select("select * from temperature_record where id=#{id} order by create_time desc")
	public TemperatureRecord getById(Integer id);

	// 查询全部
	@Results({ 
			@Result(property = "createTime", column = "create_time"),
			@Result(property = "photoUrl", column = "photo_url"),
			@Result(property = "isNormal", column = "is_normal") })
	@Select("select * from temperature_record  order by create_time desc")
	public List<TemperatureRecord> getAll();

}

property 是bean的属性名称,column是数据库字段名
这样写可以成功但是很冗余,如果有多个查询用到同样的映射,改成下面这种复用形式


import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface TemperatureMapper {
	// 根据id查询
	@Results(id = "temperatureResults", value = { 
			@Result(property = "createTime", column = "create_time"),
			@Result(property = "photoUrl", column = "photo_url"),
			@Result(property = "isNormal", column = "is_normal") })
	@Select("select * from temperature_record where id=#{id} order by create_time desc")
	public TemperatureRecord getById(Integer id);

	// 查询全部
    @ResultMap(value = "temperatureResults")
	@Select("select * from temperature_record  order by create_time desc")
	public List<TemperatureRecord> getAll();

}

如果只是某些字段不同,可以只写不同字段的映射,其他会自动映射,当然如果你全写也不会报错

	@Results(id = "temperatureResults", value = { @Result(property = "id", column = "id"),
			@Result(property = "name", column = "name"), @Result(property = "sex", column = "sex"),
			@Result(property = "age", column = "age"), @Result(property = "temperature", column = "temperature"),
			@Result(property = "createTime", column = "create_time"),
			@Result(property = "photoUrl", column = "photo_url"), @Result(property = "isNormal", column = "is_normal"),
			@Result(property = "vin", column = "vin"), @Result(property = "mac", column = "mac"),
			@Result(property = "operation", column = "operation"), @Result(property = "date", column = "date"),
			@Result(property = "floor", column = "floor"), @Result(property = "xPoint", column = "xPoint"),
			@Result(property = "yPoint", column = "yPoint"), @Result(property = "zPoint", column = "zPoint"),
			@Result(property = "pointName", column = "pointName") })
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值