6
3.1.2 @TableField
在MP中通过@TableField
注解可以指定字段的一些属性,常常解决的问题有2个:
-
对象中的属性名和字段名不一致的问题(非驼峰)
-
对象中的属性字段在表中不存在的问题
@TableField(value = “email”)//指定数据库中的字段名
private String mail;
@TableField(exist = false)//该字段在数据库中不存在
private String address;
其他用法,如果想要某些数据不被查询出来,可以用:
@TableField(select = false)
private String password;
效果:
3.2更新操作 Update
在MP中,更新操作有2种,一种是根据id更新,另一种是根据条件更新。
3.2.1 方法定义
// 根据 ID 修改
int updateById(@Param(Constants.ENTITY) T entity);
// 根据 whereWrapper 条件,更新记录
int update(@Param(Constants.ENTITY) T updateEntity, @Param(Constants.WRAPPER) Wrapper whereWrapper);
3.2.2 updateById
根据 ID 修改
@Test
public void testUpdateById(){
User user=new User();
user.setId(1L);//条件,根据id更新
user.setAge(19);//更新的字段
int result = userMapper.updateById(user);
System.out.println(“result===>”+result);
}
//输出:result===>1
3.2.3 update
根据 wrapper 条件,更新记录
①用QueryWrapper
,只可以设置更新的条件
@Test
public void testUpdate(){
User user=new User();
user.setAge(20);
user.setPassword(“666666”);//要更新的信息
QueryWrapper wrapper=new QueryWrapper<>();
wrapper.eq(“user_name”,“zhangsan”);//根据条件更新,这里是匹配数据库中的user_name字段的值等于zhangsan
int update = userMapper.update(user, wrapper);
System.out.println(“update===>”+update);
}
//输出:update===>1
②用UpdateWrapper
,不仅可以设置条件还可以设置要更新的字段
@Test
public void testUpdate2(){
UpdateWrapper wrapper=new UpdateWrapper<>();
wrapper.set(“age”,“100”).set(“password”,“999999”) //更新的字段
.eq(“user_name”,“zhangsan”); //更新的条件
int update = userMapper.update(null, wrapper);
System.out.println(“update===>”+update);
}
3.3删除操作 Delete
3.3.1 方法定义
// 根据 entity 条件,删除记录
int delete(@Param(Constants.WRAPPER) Wrapper wrapper);
// 删除(根据ID 批量删除)
int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
// 根据 ID 删除
int deleteById(Serializable id);
// 根据 columnMap 条件,删除记录
int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
参数说明:
| 类型 | 参数名 | 描述 |
| :-: | :-: | :-: |
| Wrapper | wrapper | 实体对象封装操作类(可以为 null) |
| Collection<? extends Serializable> | idList | 主键ID列表(不能为 null 以及 empty) |
| Serializable | id | 主键ID |
| Map<String, Object> | columnMap | 表字段 map 对象 |
3.3.2 deleteById
根据 ID 删除
@Test
public void testDeleteById(){
int delete = userMapper.deleteById(7L);
System.out.println(“delete===>”+delete);
}
//输出:delete===>1
3.3.3 deleteByMap
根据 map 条件,删除记录
@Test
public void testDeleteByMap() {
Map<String, Object> map = new HashMap<>();
map.put(“user_name”, “zhangsan”);
map.put(“age”, “100”);
//根据map删除数据,多条件之间是and关系
int deleteByMap = userMapper.deleteByMap(map);
System.out.println(“deleteMap===>” + deleteByMap);
}
//输出:deleteMap===>1
3.3.4 delete
根据 entity 条件,删除记录
用法一:
@Test
public void testDelete() {
QueryWrapper wrapper = new QueryWrapper<>();
wrapper.eq(“user_name”, “xpp1”)
.eq(“password”, “123456”);
//根据包装条件做删除,多条件之间是and关系
int delete = userMapper.delete(wrapper);
System.out.println(“delete===>” + delete);
}
//输出:delete===>1
用法二(更推荐):
@Test
public void testDelete() {
User user = new User();
user.setPassword(“123456”);
user.setUserName(“lisi”);
QueryWrapper wrapper = new QueryWrapper<>(user);
//根据包装条件做删除,多条件之间是and关系
int delete = userMapper.delete(wrapper);
System.out.println(“delete===>” + delete);
}
//输出:delete===>1
3.3.5 deleteBatchIds
根据 ID 批量删除
@Test
public void testDeleteBatchlds() {
//根据id集合批量删除
int deleteBatchIds = userMapper.deleteBatchIds(Arrays.asList(1L, 3L));
System.out.println(“deleteBatchIds===>” + deleteBatchIds);
}
//输出:deleteBatchIds===>2
3.4查询操作 Select
MP提供了多种查询操作,包括根据id查询、批量查询、查询单条数据、查询列表、分页查询等操作。
3.4.1 方法定义
// 根据 ID 查询
T selectById(Serializable id);
// 查询(根据ID 批量查询)
List selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
// 根据 entity 条件,查询一条记录
T selectOne(@Param(Constants.WRAPPER) Wrapper queryWrapper);
// 根据 Wrapper 条件,查询总记录数
Integer selectCount(@Param(Constants.WRAPPER) Wrapper queryWrapper);
// 根据 entity 条件,查询全部记录
List selectList(@Param(Constants.WRAPPER) Wrapper queryWrapper);
// 根据 entity 条件,查询全部记录(并翻页)
IPage selectPage(IPage page, @Param(Constants.WRAPPER) Wrapper queryWrapper);
3.4.2 selectById
根据 ID 查询
@Test
public void testSelectById() {
User user = userMapper.selectById(1L);
System.out.println(user);
}
/*输出:
User(id=1, userName=xpp1, password=null, name=西安, age=11, mail=adad, address=null)
*/
3.4.3 selectBatchIds
根据ID 批量查询
@Test
public void testSelectBatchIds() {
List users = userMapper.selectBatchIds(Arrays.asList(1L, 2L));
for (User user : users) {
System.out.println(user);
}
}
/*输出:
User(id=1, userName=xpp1, password=null, name=西安, age=11, mail=adad, address=null)
User(id=2, userName=xpp2, password=null, name=北京, age=12, mail=adddd, address=null)
*/
3.4.5 selectOne
根据 entity 条件,查询一条记录
@Test
public void testSelectOne() {
QueryWrapper wrapper = new QueryWrapper<>();
//查询条件
wrapper.eq(“id”, “1”)
.eq(“user_name”, “xpp1”);
//查询的数据超过一条时,会抛出异常
User user = userMapper.selectOne(wrapper);
System.out.println(user);
}
/*输出:
User(id=1, userName=xpp1, password=null, name=西安, age=11, mail=adad, address=null)
*/
3.4.6 selectCount
根据 Wrapper 条件,查询总记录数
@Test
public void testSelectCount() {
QueryWrapper wrapper = new QueryWrapper<>();
wrapper.gt(“age”, “13”);//查询年龄大于13的数量
Integer selectCount = userMapper.selectCount(wrapper);
System.out.println(selectCount);
}
//输出:2
3.4.7 selectList
根据 entity 条件,查询全部记录
@Test
public void testSelectList() {
QueryWrapper wrapper = new QueryWrapper<>();
wrapper.like(“email”, “itcast”);
List users = userMapper.selectList(wrapper);
for (User user : users) {
System.out.println(user);
}
}
/*输出:
User(id=2, userName=xpp2, password=null, name=北京, age=12, mail=test4@itcast.cn, address=null)
User(id=4, userName=zhaoliu, password=null, name=赵六, age=21, mail=test4@itcast.cn, address=null)
User(id=5, userName=sunqi, password=null, name=孙七, age=24, mail=test5@itcast.cn, address=null)
*/
3.5.8 selectPage
配置分页插件
@Configuration
public class MybatisPlusConfig {
@Bean //配置分页插件
public PaginationInterceptor paginationInterceptor(){
return new PaginationInterceptor();
}
}
根据 entity 条件,查询全部记录(并翻页)
@Test
public void testSelectPage() {
Page page = new Page<>(1, 1);//查询第一页,查询一条数据
QueryWrapper wrapper = new QueryWrapper<>();
wrapper.like(“email”, “itcast”);//设置查询条件
Page iPage = userMapper.selectPage(page, wrapper);
System.out.println(“数据总条数:” + iPage.getTotal());
System.out.println(“数据总页数:” + iPage.getPages());
System.out.println(“当前页数:” + iPage.getCurrent());
List records = iPage.getRecords();//拿到数据
for (User record : records) {
System.out.println(record);
}
}
/*输出:
数据总条数:4
数据总页数:4
当前页数:1
User(id=2, userName=xpp2, password=null, name=北京, age=12, mail=test4@itcast.cn, address=null)
*/
3.5SQL注入原理
4.1基本配置
4.1.1 configLocation
MyBatis 配置文件位置,如果您有单独的 MyBatis 配置,请将其路径配置到 configLocation 中。 MyBatis Configuration 的具体内容请参考MyBatis 官方文档。
在application.properties
配置文件中:
#指定全局的配置文件
mybatis-plus.config-location= classpath:mybatis-config.xml
在application.yml
配置文件中:
#指定全局的配置文件
mybatis-plus:
config-location: classpath:mybatis-config.xml
4.1.2 mapperLocations
MyBatis Mapper 所对应的 XML 文件位置,如果您在 Mapper 中有自定义方法(XML 中有自定义实现),需要进行该配置,告诉 Mapper 所对应的 XML 文件位置。(简单来说就是我们mybaits和mybatis plus混用需要指定xml文件位置)
在application.properties
配置文件中:
#指定Mapper.xml文件的路径
mybatis-plus.mapper-locations= classpath*:mapper/*.xml
在application.yml
配置文件中:
#指定Mapper.xml文件的路径
mybatis-plus:
mapper-locations: classpath*:mapper/*.xml
具体位置:
测试:
UserMapper.xml
文件:
select * from tb_user where id = #{id}
UserMapper
类:
@Mapper
public interface UserMapper extends BaseMapper {
User findById(Long id);
}
测试方法:
/**
- 自定义的方法
*/
最后
这份清华大牛整理的进大厂必备的redis视频、面试题和技术文档
祝大家早日进入大厂,拿到满意的薪资和职级~~~加油!!
感谢大家的支持!!
mg-blog.csdnimg.cn/600d5516f98a43e4929246aa3d112816.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBATEwuTEVCUk9O,size_20,color_FFFFFF,t_70,g_se,x_16)
测试:
UserMapper.xml
文件:
select * from tb_user where id = #{id}
UserMapper
类:
@Mapper
public interface UserMapper extends BaseMapper {
User findById(Long id);
}
测试方法:
/**
- 自定义的方法
*/
最后
这份清华大牛整理的进大厂必备的redis视频、面试题和技术文档
祝大家早日进入大厂,拿到满意的薪资和职级~~~加油!!
感谢大家的支持!!
[外链图片转存中…(img-NiO4d1n9-1714515520234)]