在mybatis后面添加plus
mybatis:
type-aliases-package: com.wjs.entity
mapper-locations: classpath:com/wjs/mapper
//复杂条件删除
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.like("username", "xiao1");
userMapper.delete(queryWrapper);
}
改
@Test
public void testUpdate(){
//如果属性为null,则不修改对应的列
User u = new User(1, "xiaohei", null);
userMapper.updateById(u);
}
查
1. id查询
@Test
public void testSelectById(){
User user = userMapper.selectById(3);
System.out.println("user = " + user);
}
2. 多id查询
@Test
public void testSelectByBatchIds(){
Collection ids = Arrays.asList(1,2,3);
List users = userMapper.selectBatchIds(ids);
users.forEach((u)->{System.out.println(u);});
}
3. 多条件等值查询
@Test
public void testSelectByMap(){
// username="xiaohei" and password = "123456"
Map columnMap = new HashMap<>();
columnMap.put("username","xiaohei");
columnMap.put("password", "123456");
List users = userMapper.selectByMap(columnMap);
users.forEach((u)->{System.out.println(u);});
}
三 丶)自定义方法
当要使用的方法MyBatisPlus没有给我们提供时可以自己书写方法,需要自己写接口和映射文件和之前的一样。
1. 在接口中定义查询方法
public interface UserMapper extends BaseMapper {
public User selectUserWithAddressById(Integer id);
}
2. 在xml中提供sql语句和resultMap
四 丶)代码生成器
1. 添加依赖
2. 书写测试类com.baomidou mybatis-plus-generator3.2.0 org.apache.velocity velocity-engine-core2.2
注意修改数据库名,类名,作者名等信息
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import java.util.Scanner;
public class CodeGenerator {
public static void main(String[] args) {
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/auction_auction?useUnicode=true&characterEncoding=utf-8");
// dsc.setSchemaName("public");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("root");
mpg.setDataSource(dsc);
// 全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
gc.setOutputDir(projectPath + "/src/main/java");//生成的java代码放在哪
gc.setAuthor("鲸");//作者
gc.setOpen(false);//是否在代码生成后自动打开目录
// gc.setServiceName("%sService");//去除service接口前的I
mpg.setGlobalConfig(gc);
// 包配置 各种包名
PackageConfig pc = new PackageConfig();
pc.setParent("com.wjs");
pc.setEntity("entity");
pc.setMapper("dao");
pc.setService("service");
pc.setServiceImpl("service.impl");
pc.setController("controller");
mpg.setPackageInfo(pc);
// 生成策略配置
StrategyConfig sc = new StrategyConfig();
sc.setInclude(scanner("表名,多个英文逗号分割").split(","));//哪些表进行自动生成,需要用户输入
sc.setEntityLombokModel(true);
sc.setNaming(NamingStrategy.underline_to_camel);//数据库表映射到实体的命名策略
sc.setColumnNaming(NamingStrategy.underline_to_camel);//数据库表字段映射到实体的命名策略
sc.setRestControllerStyle(true);//设置RestController
sc.setTablePrefix("t_"); //设置表的前缀,比如t_person的前缀是t_
mpg.setStrategy(sc);
mpg.execute();//开始生成
}
public static String scanner(String tip) {
Scanner scanner = new Scanner(System.in);
StringBuilder help = new StringBuilder();
help.append("请输入" + tip + ":");
System.out.println(help.toString());
if (scanner.hasNext()) {
String ipt = scanner.next();
//MybatisPlus提供的工具类,做非空判断
if (StringUtils.isNotEmpty(ipt)) {
return ipt;
}
}
throw new MybatisPlusException("请输入正确的" + tip + "!");
}
}
3. 修改代码生成的位置(如果你用的不是分布式微服务可不做)
默认为父项目,点击修改为当前子项目
4. 一键生成代码(选择你要生成哪个表的代码)点击运行,输入你想生成代码的表,可一次生成多个
点击回车即可看见生成的代码
如果使用自定义的mapper.xml是不会被识别的需要手动设置,不使用可不设置
先修改yml文件再添加依赖配置
#修改前
mybatis-plus:
type-aliases-package: com.wjs.entity
mapper-locations: classpath:com/wjs/dao*.properties
**/*.xml
true
5. 测试生成的代码是否可用
书写一个查询全部的controller
@Autowired
private IAuctionService as;
@GetMapping("/selectAll")
public List selectAll() {
return as.list();
}
注:如果遇见com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(Ljava/lang/CharSequence;)Z的报错就是版本号不兼容
把mybatis-plus-generator换成3.3.0或者更高版本的
成功运行后的效果



