# application.yml中配置如下
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
@SpringBootTest
public class MyBatisPlusTest {
@Autowired
private UserMapper userMapper;
/**
* 新增的id由mybatis plus使用雪花算法生成
* 新增时没有指定id
*/
@Test
public void testInsert(){
//实现新增用户信息
//INSERT INTO user ( id, name, age, email ) VALUES ( ?, ?, ?, ? )
User user = new User();
user.setName("张三");
user.setAge(23);
user.setEmail("zhangsan@atguigu.com");
int result = userMapper.insert(user);
System.out.println("result:"+result);
System.out.println("id:"+user.getId());
}
@Test
public void testDelete(){
//通过id删除用户信息
//DELETE FROM user WHERE id=?
/*int result = userMapper.deleteById(1492767055210991617L);
System.out.println("result:"+result);*/
//根据map集合中所设置的条件删除用户信息
//DELETE FROM user WHERE name = ? AND age = ?
/*Map<String, Object> map = new HashMap<>();
map.put("name", "张三");
map.put("age", 23);
int result = userMapper.deleteByMap(map);
System.out.println("result:"+result);*/
//通过多个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);
}
@Test
public void testUpdate(){
//修改用户信息
//UPDATE user SET name=?, email=? WHERE id=?
User user = new User();
user.setId(4L);
user.setName("李四");
user.setEmail("lisi@atguigu.com");
int result = userMapper.updateById(user);
System.out.println("result:"+result);
}
@Test
public void testSelect(){
//通过id查询用户信息
//SELECT id,name,age,email FROM user WHERE id=?
/*User user = userMapper.selectById(1L);
System.out.println(user);*/
//根据多个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);*/
//根据map集合中的条件查询用户信息
//SELECT id,name,age,email FROM user WHERE name = ? AND age = ?
/*Map<String, Object> map = new HashMap<>();
map.put("name", "Jack");
map.put("age", 20);
List<User> users = userMapper.selectByMap(map);
users.forEach(System.out::println);*/
//查询所有数据
//SELECT id,name,age,email FROM user
List<User> users = userMapper.selectList(null);
users.forEach(System.out::println);
}
}
# mybatis plus默认的配置为如下
# 当不写如下配置时,如果要写映射文件,则必须将映射文件放在mapper目录下
# 如果想放在其他的目录,则需指定
mybatis-plus:
mapper-locations: classpath:mapper/*.xml # 扫描映射文件
@Repository
public interface UserMapper extends BaseMapper<User> {
/**
* 根据id查询用户信息为map集合
* @param id
* @return
*/
Map<String, Object> selectMapById(Long id);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.atguigu.mybatisplus.mapper.UserMapper">
<!--Map<String, Object> selectMapById(Long id);-->
<select id="selectMapById" resultType="map">
select id,name,age,email from user where id = #{id}
</select>
</mapper>
@Test
public void testSelect(){
Map<String, Object> map = userMapper.selectMapById(1L);
System.out.println(map);
}
public interface UserService extends IService<User> {
}
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}
@SpringBootTest
public class MyBatisPlusServiceTest {
@Autowired
private UserService userService;
@Test
public void testGetCount(){
//查询总记录数
//SELECT COUNT( * ) FROM user
long count = userService.count();
System.out.println("总记录数:"+count);
}
@Test
public void testInsertMore(){
// 批量添加操作
// 没有指定id
//INSERT INTO user ( id, name, age ) VALUES ( ?, ?, ? )
List<User> list = new ArrayList<>();
for (int i = 1; i <= 10; i++) {
User user = new User();
user.setName("ybc"+i);
user.setAge(20+i);
list.add(user);
}
boolean b = userService.saveBatch(list);
System.out.println(b);
}
}