@Test
public void testInsert(){
//实现新增数据的功能
//INSERT INTO user ( id, name, age, email ) VALUES ( ?, ?, ?, ? )
User user=new User();
user.setName("张三");
user.setAge(18);
user.setEmail("[email protected]");
int result = userMapper.insert(user);
System.out.println("result:"+result);
System.out.println("id"+user.getId());
}
Ps:这个id是通过雪花算法算出来的id
在BaseMapper中有三种删除方式
1、根据id删除
@Test
public void testDelete(){
int i = userMapper.deleteById(1503267791384346626L);
System.out.println("result"+i);
}
如果单纯加入id则会保错,原因是数字过大,需要在数字后面添加L,表示为Long类型
2、根据集合删除
@Test
public void testDelete2(){
//通过map删除用户信息
//DELETE FROM user WHERE name = ? AND age = ?
Map<String,Object> map=new HashMap<>();
map.put("name","张三");
map.put("age",18);
int result=userMapper.deleteByMap(map);
System.out.println("result:"+result);
}
@Test
public void testDelete3(){
//通过多个ID进行删除
//DELETE FROM user WHERE id IN ( ? , ? , ? )
List<Long> list = Arrays.asList(1L, 2L, 3L);
int result = userMapper.deleteBatchIds(list);
System.out.println("result:"+result);
}
1L:数字后面带个L没问题,因为本身id就定义为Long类型
@Test
public void testUpdate(){
//根据id进行修改
//UPDATE user SET name=?, email=? WHERE id=?
User user=new User();
user.setId(4L);
user.setName("李四");
user.setEmail("[email protected]");
int result = userMapper.updateById(user);
System.out.println("result:"+result);
}
把数据库信息添加代码重新运行下(可看上一篇博客)
1、根据id进行查询操作
@Test
public void testselect1(){
//根据id进行查询操作
//SELECT id,name,age,email FROM user WHERE id=?
User user = userMapper.selectById(1L);
System.out.println(user);
}
@Test
public void testselect2(){
//根据多个id进行查询操作
//SELECT id,name,age,email FROM user WHERE id IN ( ? , ? , ? )
List<Long> list = Arrays.asList(1L, 2L, 3L);
List<User> users = userMapper.selectBatchIds(list);
users.forEach(System.out::println);
}
3、根据map集合的条件查询
@Test
public void testselect3(){
//根据map集合条件进行查询操作
//SELECT id,name,age,email FROM user WHERE name = ? AND age = ?
Map<String,Object> map=new HashMap<>();
map.put("name","jone");
map.put("age","18");
List<User> users = userMapper.selectByMap(map);
users.forEach(System.out::println);
}
@Test
public void testSelect() {
System.out.println(("----- selectAll method test ------"));
//SELECT id,name,age,email FROM user
//查询所有的用户
List<User> userList = userMapper.selectList(null);
userList.forEach(System.out::println);
}
有时候这些基本的crud如法满足我们的需求,需要我们自己自定义功能
先自定义模板,在file–>setting然后按照以下操作即可
<mapper namespace="">
mapper>
最后在resource包右键即可找到,创建文件UserMapper
修改UserMapper接口
package com.example.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.pojo.User;
import java.util.Map;
public interface UserMapper extends BaseMapper<User> {
//根据id查询用户信息为map集合
Map<String,Object> selectMapById(Long id);
}
修改UserMapper.xml文件
<mapper namespace="com.example.mapper.UserMapper">
<select id="selectMapById" resultType="map">
select * from user where id=#{id}
select>
mapper>
测试
@Test
public void test2(){
//测试自定义功能
//select * from user where id=?
Map<String, Object> map = userMapper.selectMapById(1L);
System.out.println(map);
}