問(wèn)題背景
因?yàn)閙ybatis plus非常的流行胀屿,雖然平常mybatis generator也夠用了溉痢,但多會(huì)一個(gè)僻造,看別人的代碼就輕松一點(diǎn)
注意事項(xiàng):
- 官方網(wǎng)站:https://baomidou.com/
- 官方文檔:https://baomidou.com/pages/24112f/
- 可以自己創(chuàng)建工程,[也可以下載源碼進(jìn)行參考]https://download.csdn.net/download/cucgyfjklx/85547495)
-
MyBatis-Plus在實(shí)現(xiàn)插入數(shù)據(jù)時(shí)孩饼,會(huì)默認(rèn)基于雪花算法的策略生成id髓削,實(shí)體類entity屬性都使用對(duì)象,使用Long镀娶,不能使用long立膛,不然雪花算法會(huì)失效
Mybatis-plus無(wú)介紹快使用,CRUD增刪改查基本使用附源碼(一)
Mybatis-plus無(wú)介紹快使用梯码,自定義sql語(yǔ)句CRUD增刪改查附源碼(二)
Mybatis-plus無(wú)介紹快使用宝泵,自帶封裝service層的使用附源碼(三)
Mybatis-plus無(wú)介紹快使用,注解的使用(四)
Mybatis-plus無(wú)介紹快使用轩娶,Wrapper條件構(gòu)造器的使用附源碼(五)
Mybatis-plus無(wú)介紹快使用儿奶,分頁(yè)插件和樂(lè)觀鎖插件的使用附源碼(六)
Mybatis-plus無(wú)介紹快使用,枚舉變量的使用附源碼(七)
Mybatis-plus無(wú)介紹快使用罢坝,多數(shù)據(jù)源的使用(八)
Mybatis-plus無(wú)介紹快使用廓握,MybatisX自動(dòng)生成代碼插件的使用(九)
Mybatis-plus無(wú)介紹快使用,可繼承通用的基礎(chǔ)實(shí)體類(十)
項(xiàng)目搭建
1 很久上篇的CRUD基礎(chǔ)嘁酿,這個(gè)篇章講解自定義CRUD隙券,現(xiàn)在resources文件夾下創(chuàng)建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
#生成sql日志打印,一般關(guān)閉
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
#classpath*就是路徑resources
mapper-locations: classpath*:/mapper/**/*.xml
3 ctrl+alt+s打開setting闹司,創(chuàng)建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文件夾下創(chuàng)建UserMapper.xml文件娱仔,名字必須和UserMapper的接口名一致
5 編寫自定義sql語(yǔ)句, resultType返回結(jié)果類型
<?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">
<!--selectUserById-->
<select id="selectUserById" resultType="com.yg.mybatisplus.entity.User">
select id, name, age, email
from user
where id = #{id}
</select>
<!--selectMapById-->
<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;
/**
* @Author suolong
* @Date 2022/6/2 17:25
* @Version 2.0
*/
@Repository
public interface UserMapper extends BaseMapper<User> {
/**
* @Caption 根據(jù)id查詢用戶信息為map集合
* @Param id
* @Return map<字段名, 值>
*/
Map<String, Object> selectMapById(Long id);
/**
* @Caption 根據(jù)id查詢用戶信息
* @Param id
* @Return User
*/
User selectUserById(Long id);
}
7 測(cè)試程序
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;
/**
* @Author suolong
* @Date 2022/6/2 17:42
* @Version 2.0
*/
@SpringBootTest
public class MybatisPlusTest {
@Autowired
UserMapper userMapper;
@Test
void selectTest(){
//全查
List<User> users = userMapper.selectList(null);
users.forEach(System.out::println);
}
/**
* 測(cè)試插入一條數(shù)據(jù)
* MyBatis-Plus在實(shí)現(xiàn)插入數(shù)據(jù)時(shí),會(huì)默認(rèn)基于雪花算法的策略生成id
*/
@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("受影響的行數(shù)為:" + result);
//1527206783590903810(當(dāng)前 id 為雪花算法自動(dòng)生成的id)
System.out.println("id自動(dòng)獲取" + user.getId());
}
/**
* 測(cè)試根據(jù)id刪除一條數(shù)據(jù)
*/
@Test
public void testDeleteById(){
int result = userMapper.deleteById(1533011356262625282L);
System.out.println(result > 0 ? "刪除成功!" : "刪除失旐镌鳌筛峭!");
System.out.println("受影響的行數(shù)為:" + result);
}
/**
* 測(cè)試通過(guò)id批量刪除數(shù)據(jù)
*/
@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("受影響的行數(shù)為:" + result);
}
/**
* 測(cè)試根據(jù)Map集合中所設(shè)置的條件刪除數(shù)據(jù)
*/
@Test
public void testDeleteByMap(){
//當(dāng)前演示為根據(jù)name和age刪除數(shù)據(jù)
//執(zhí)行SQL為:DELETE FROM user WHERE name = ? AND age = ?
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("受影響的行數(shù)為:" + result);
}
/**
* 測(cè)試根據(jù)id修改用戶信息, 只修改改變的值, 其他信息保持原值
*/
@Test
public void testUpdateById(){
//執(zhí)行SQL為: UPDATE user SET name=?, age=?, email=? WHERE id=?
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("受影響的行數(shù)為:" + result);
}
/**
* 測(cè)試根據(jù)id查詢用戶數(shù)據(jù)
*/
@Test
public void testSelectById(){
User user = userMapper.selectById(1L);
System.out.println(user);
}
/**
* 根據(jù)多個(gè)id查詢用戶數(shù)據(jù)
*/
@Test
public void testSelectBatchIds(){
//執(zhí)行SQL為:SELECT id,name,age,email FROM user WHERE id IN ( ? , ? , ? )
List<Long> ids = Arrays.asList(1L,2L,3L);
List<User> users = userMapper.selectBatchIds(ids);
users.forEach(System.out::println);
}
/**
* 根據(jù)Map所設(shè)置的條件查詢用戶
*/
@Test
public void testSelectByMap(){
//執(zhí)行SQL為:SELECT id,name,age,email FROM user WHERE age = ?
Map<String,Object> map = new HashMap<>();
map.put("age",18);
List<User> users = userMapper.selectByMap(map);
users.forEach(System.out::println);
}
/**
* 測(cè)試根據(jù)id查詢用戶數(shù)據(jù), 返回User對(duì)象
*/
@Test
public void testSelectUserById(){
User user = userMapper.selectUserById(1L);
System.out.println(user);
}
/**
* 測(cè)試根據(jù)id查詢用戶數(shù)據(jù),返回map
*/
@Test
public void testSelectMapById(){
Map<String, Object> stringObjectMap = userMapper.selectMapById(1L);
System.out.println(stringObjectMap);
}
}
8 項(xiàng)目目錄
總結(jié)
- 需要自定的增刪改查的語(yǔ)句戏售,可以通過(guò)寫xml配置文件
作為程序員第 150 篇文章侨核,每次寫一句歌詞記錄一下,看看人生有幾首歌的時(shí)間灌灾,wahahaha ...