这一节我们来学习一下,插入和更新。
1、基于一个实体设置要保存的值,插入一条数据
方法:
int insert(T entity);
代码:
@Test
public void testInsert() {
System.out.println(("----- insert method test ------"));
User user = new User();
user.setId(6L);
user.setAge(20);
user.setEmail("123@163.com");
user.setName("张三");
userMapper.insert(user);
System.out.println("插入的Id:"+user.getId());
}
输出SQL:
==> Preparing: INSERT INTO user ( id, name, age, email ) VALUES ( ?, ?, ?, ? )
==> Parameters: 6(Long), 张三(String), 20(Integer), 123@163.com(String)
<== Updates: 1
System.out.println输出:
插入的Id:6
2、根据配置的查询条件,更新一条或多条数据
方法:
// 根据 whereEntity 条件,更新记录
int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper<T> updateWrapper);
具体来说第一个参数T entity,用来设置被更新的列的值,第二个参数rapper updateWrapper)配置的是SQL的where条件。这个方法可以同时更新多条数据。
代码:
@Test
public void testUpdate() {
System.out.println(("----- update method test ------"));
User user = userMapper.selectById(5);
System.out.println("修改前数据:" +user);
User user2 = new User();
user2.setEmail("12346@163.com");
user2.setName("张三");
QueryWrapper qw = new QueryWrapper();
qw.eq("ID",5L);
int result = userMapper.update(user2,qw);
System.out.println("更新记录数:" + result);
User user3 = userMapper.selectById(5);
System.out.println("修改后数据:" +user3);
}
sql和println的输出:
==> Preparing: SELECT id,name,age,email FROM user WHERE id=?
==> Parameters: 5(Integer)
<== Columns: ID, NAME, AGE, EMAIL
<== Row: 5, Billie, 24, test5@baomidou.com
<== Total: 1
修改前数据:User(id=5, name=Billie, age=24, email=test5@baomidou.com)
==> Preparing: UPDATE user SET name=?, email=? WHERE (ID = ?)
==> Parameters: 张三(String), 12346@163.com(String), 5(Long)
<== Updates: 1
更新记录数:1
==> Preparing: SELECT id,name,age,email FROM user WHERE id=?
==> Parameters: 5(Integer)
<== Columns: ID, NAME, AGE, EMAIL
<== Row: 5, 张三, 24, 12346@163.com
<== Total: 1
修改后数据:User(id=5, name=张三, age=24, email=12346@163.com)
3、根据主键ID更新单条数据
用法就是new一个实体,主键id赋值将作为where的条件,实体其他非空属性将作为更新值。
方法:
int updateById(@Param(Constants.ENTITY) T entity);
代码:
@Test
public void testUpdateById() {
System.out.println(("----- updateById method test ------"));
User user = userMapper.selectById(5);
System.out.println("修改前数据:" +user);
User user2 = new User();
user2.setEmail("12346@163.com");
user2.setName("张三");
user2.setId(5L);
int result = userMapper.updateById(user2);
System.out.println("更新记录数:" + result);
User user3 = userMapper.selectById(5);
System.out.println("修改后数据:" +user3);
}
sql和println的输出:
----- updateById method test ------
==> Preparing: SELECT id,name,age,email FROM user WHERE id=?
==> Parameters: 5(Integer)
<== Columns: ID, NAME, AGE, EMAIL
<== Row: 5, Billie, 24, test5@baomidou.com
<== Total: 1
修改前数据:User(id=5, name=Billie, age=24, email=test5@baomidou.com)
==> Preparing: UPDATE user SET name=?, email=? WHERE id=?
==> Parameters: 张三(String), 12346@163.com(String), 5(Long)
<== Updates: 1
更新记录数:1
==> Preparing: SELECT id,name,age,email FROM user WHERE id=?
==> Parameters: 5(Integer)
<== Columns: ID, NAME, AGE, EMAIL
<== Row: 5, 张三, 24, 12346@163.com
<== Total: 1
修改后数据:User(id=5, name=张三, age=24, email=12346@163.com)