在使用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") })