Mybatis plus无介绍快使用,自定义sql语句CRUD增删改查附源码(二)
问题背景
因为mybatis plus非常的流行,虽然平常mybatis generator也够用了,但多会一个,看别人的代码就轻松一点 注意事项:
项目搭建
1 很久上篇的CRUD基础,这个篇章讲解自定义CRUD,现在resources文件夹下创建mapper文件夹 2 添加application.yml的mapper的xml扫描路径
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://10.10.195.193:3306/fusion?severTimezone=Asia/Shanghai&allowMultiQueries=true&autoReconnect=true&characterEncoding=UTF-8&useUnicode=true&useSSL=false
username: fusion
password: iEx8c
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: classpath*:/mapper/**/*.xml
3 ctrl+alt+s打开setting,创建mapper模板
<?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="">
</mapper>
4 在mapper文件夹下创建UserMapper.xml文件,名字必须和UserMapper的接口名一致
5 编写自定义sql语句, resultType返回结果类型
<?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.yg.mybatisplus.mapper.UserMapper">
<select id="selectUserById" resultType="com.yg.mybatisplus.entity.User">
select id, name, age, email
from user
where id = #{id}
</select>
<select id="selectMapById" resultType="map">
select id, name, age, email
from user
where id = #{id}
</select>
</mapper>
6 UserMapper接口编写
package com.yg.mybatisplus.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yg.mybatisplus.entity.User;
import org.springframework.stereotype.Repository;
import java.util.Map;
@Repository
public interface UserMapper extends BaseMapper<User> {
Map<String, Object> selectMapById(Long id);
User selectUserById(Long id);
}
7 测试程序
package com.yg.mybatisplus;
import com.yg.mybatisplus.entity.User;
import com.yg.mybatisplus.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@SpringBootTest
public class MybatisPlusTest {
@Autowired
UserMapper userMapper;
@Test
void selectTest(){
List<User> users = userMapper.selectList(null);
users.forEach(System.out::println);
}
@Test
public void testInsert(){
User user = new User();
user.setName("Vz1");
user.setAge(211);
user.setEmail("vz@oz61.cn");
int result = userMapper.insert(user);
System.out.println(result > 0 ? "添加成功!" : "添加失败!");
System.out.println("受影响的行数为:" + result);
System.out.println("id自动获取" + user.getId());
}
@Test
public void testDeleteById(){
int result = userMapper.deleteById(1533011356262625282L);
System.out.println(result > 0 ? "删除成功!" : "删除失败!");
System.out.println("受影响的行数为:" + result);
}
@Test
public void testDeleteBatchIds(){
List<Long> ids = Arrays.asList(1532349895093833729L,1533012537454157826L,1533012657583161345L);
int result = userMapper.deleteBatchIds(ids);
System.out.println(result > 0 ? "删除成功!" : "删除失败!");
System.out.println("受影响的行数为:" + result);
}
@Test
public void testDeleteByMap(){
Map<String,Object> map = new HashMap<>();
map.put("name","Vz1 ");
map.put("age",211);
int result = userMapper.deleteByMap(map);
System.out.println(result > 0 ? "删除成功!" : "删除失败!");
System.out.println("受影响的行数为:" + result);
}
@Test
public void testUpdateById(){
User user = new User();
user.setId(5L);
user.setEmail("Vz@sina.com");
int result = userMapper.updateById(user);
System.out.println(result > 0 ? "修改成功!" : "修改失败!");
System.out.println("受影响的行数为:" + result);
}
@Test
public void testSelectById(){
User user = userMapper.selectById(1L);
System.out.println(user);
}
@Test
public void testSelectBatchIds(){
List<Long> ids = Arrays.asList(1L,2L,3L);
List<User> users = userMapper.selectBatchIds(ids);
users.forEach(System.out::println);
}
@Test
public void testSelectByMap(){
Map<String,Object> map = new HashMap<>();
map.put("age",18);
List<User> users = userMapper.selectByMap(map);
users.forEach(System.out::println);
}
@Test
public void testSelectUserById(){
User user = userMapper.selectUserById(1L);
System.out.println(user);
}
@Test
public void testSelectMapById(){
Map<String, Object> stringObjectMap = userMapper.selectMapById(1L);
System.out.println(stringObjectMap);
}
}
8 项目目录
总结
- 需要自定的增删改查的语句,可以通过写xml配置文件
作为程序员第 150 篇文章,每次写一句歌词记录一下,看看人生有几首歌的时间,wahahaha …
Lyric: 灰狼啃食着水鹿的骨头
|