"查"操作(企业开发中尽量不使用*,需要哪些字段就写哪些字段,都需要就全写上)
Mybatis 会自动地根据数据库的字段名和Java对象的属性名进行映射,如果名称一样就进行赋值
但是那些名称不一样的,我们想要拿到,该怎么拿呢?
一开始数据库字段名和Java对象属性名如下图所示,名称不一样
解决办法:
1.对 Mysql 的查询结果进行重命名 ,在@Select括号里面把这三个字段名用as修改成Java对象属性名
package com.example.mybatisdemo.mapper;import com.example.mybatisdemo.model.UserInfo;
import org.apache.ibatis.annotations.*;import java.util.List;@Mapper
public interface UserInfoMapper {@Select("select id, username, password, age, gender, phone, delete_flag as deleteflag, create_time as createtime, update_time as updatetime" +" from userInfo")List<UserInfo> selectAll();}
右键,generate,test,勾选,OK
package com.example.mybatisdemo.mapper;import com.example.mybatisdemo.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import java.util.List;import static org.junit.jupiter.api.Assertions.*;@Slf4j
@SpringBootTest
class UserInfoMapperTest {@Autowiredprivate UserInfoMapper userInfoMapper;@Testvoid selectAll() {List<UserInfo> list = userInfoMapper.selectAll();log.info(list.toString());}}
每列数据就都能找到了
2.使用 @Result 注解
package com.example.mybatisdemo.mapper;import com.example.mybatisdemo.model.UserInfo;
import org.apache.ibatis.annotations.*;import java.util.List;@Mapper
public interface UserInfoMapper {@Results({@Result(column = "delete_flag",property = "deleteFlag"),@Result(column = "create_time",property = "createTime"),@Result(column = "update_time",property = "updateTime")})@Select("select * from userInfo")List<UserInfo> selectAll2();}
右键,generate,test,勾选,OK
package com.example.mybatisdemo.mapper;import com.example.mybatisdemo.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import java.util.List;import static org.junit.jupiter.api.Assertions.*;@Slf4j
@SpringBootTest
class UserInfoMapperTest {@Autowiredprivate UserInfoMapper userInfoMapper;@Testvoid selectAll2() {List<UserInfo> list = userInfoMapper.selectAll();log.info(list.toString());}
}
成功
当我们别的时候也需要查找 delete_flag 和 create_time 和 update_time,就也需要写这一段语句
@Results({@Result(column = "delete_flag",property = "deleteFlag"),@Result(column = "create_time",property = "createTime"),@Result(column = "update_time",property = "updateTime") })
这样就太繁琐了
接下来我们学习如何复用 Result 的定义,加上 id 和 value
然后哪里需要用就在前面加上@ResultMap
package com.example.mybatisdemo.mapper;import com.example.mybatisdemo.model.UserInfo;
import org.apache.ibatis.annotations.*;import java.util.List;@Mapper
public interface UserInfoMapper {@Results(id="BaseMap",value = {@Result(column = "delete_flag",property = "deleteFlag"),@Result(column = "create_time",property = "createTime"),@Result(column = "update_time",property = "updateTime")})@Select("select * from userInfo")List<UserInfo> selectAll2();@ResultMap(value = "BaseMap")@Select("select * from userInfo where id = #{id}")UserInfo selectOne(Integer id);}
右键,generate,test.勾选.ok
package com.example.mybatisdemo.mapper;import com.example.mybatisdemo.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import java.util.List;import static org.junit.jupiter.api.Assertions.*;@Slf4j
@SpringBootTest
class UserInfoMapperTest {@Autowiredprivate UserInfoMapper userInfoMapper;@Testvoid selectOne() {log.info(userInfoMapper.selectOne(1).toString());}
}
成功