MyBatis-Plus

MyBatis-Plus


1.1 MyBatis-Plus简介

官网:https://mybatis.plus/

作者:苞米豆
在Mybatis基础上只增强,不改变。

1.2 使用流程
  1. POJO配置
@TableName("user")//指定表名,写在类上。
@TableId(type = IdType.NONE)//IdType设置主键生成策略
@TableField(select = false)//查询不返回该字段
@TableField(value = "email")//解决列名,属性不对应(别名)
@TableField(exist = false)//表中不存在该列。如果该字段不赋值,则不影响插入等操作,赋值,必须加注解。
  1. Dao继承 BaseMapper,可以设置泛型,然后对数据进行操作(增删改查)
public interface UserDao extends BaseMapper<User>
  1. 编写相关代码进行测试
1.3 具体流程
  1. 导入相关依赖
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.2.13</version>
</dependency>

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.3.2</version>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.1.10</version>
</dependency>
  1. 配置 application.properties
#配置mysql
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.url=jdbc:mysql:///java2002?serverTimezone=Asia/Shanghai
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#连接池
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#mybatis
#扫描mapper.xml文件
mybatis-plus.mapper-locations=classpath:mapper/*.xml
#sql语句显示到控制台
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
1.4CRUD操作

POJO

package com.qf.pojo;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

@Data
@TableName("tb_music")
public class TbMusic {

    @TableId(type = IdType.NONE)
    @TableField("music_id")
    private String musicId;
    @TableField("music_name")
    private String musicName;
    @TableField("music_album_name")
    private String musicAlbumName;
    @TableField("music_album_picUrl")
    private String musicAlbumPicurl;
    @TableField("music_mp3Url")
    private String musicMp3url;
    @TableField("music_artist_name")
    private String musicArtistName;
    @TableField("sheet_id")
    private Integer sheetId;

}

Dao

package com.qf.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.qf.pojo.TbMusic;

import java.util.List;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface TbMusicMapper extends BaseMapper<TbMusic> {

}

Contorller

package com.qf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.pojo.TbMusic;
import com.qf.service.TbMusicService;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("music")
public class TbMusicController {

    @Autowired
    private TbMusicService tbMusicService;

    //根据条件修改
    @RequestMapping("updateMusic")
    public void updateMusic(){

        TbMusic tbMusic = new TbMusic();
        tbMusic.setMusicId("3");
        tbMusic.setMusicName("好久都不见");

        tbMusicService.updateMusic(tbMusic);
    }


    //根据id修改
    @RequestMapping("updateById")
    public void updateById(String musicId){

        TbMusic tbMusic = new TbMusic();
        tbMusic.setMusicId(musicId);
        tbMusic.setMusicName("平凡之路");
        tbMusic.setMusicArtistName("朴树");

        tbMusicService.updateById(tbMusic);
    }


    //根据id删除
    @RequestMapping("deleteById")
    public void deleteById(String musicId){
        tbMusicService.deleteById(musicId);
    }

    //批量删除
    @RequestMapping("deleteByIds")
    public void deleteById(String [] ids){
        tbMusicService.deleteByIds(ids);
    }


    //添加
    @RequestMapping("add")
    public void add(){
        TbMusic tbMusic = new TbMusic();
        tbMusic.setMusicId("1001");
        tbMusic.setMusicName("少年");

        tbMusicService.add(tbMusic);
    }


    //查询所有
    @RequestMapping("findAll")
    public List<TbMusic> findAll(){
        List<TbMusic> tbMusics = tbMusicService.findAll();
        return tbMusics;
    }
    //模糊查询
    @RequestMapping("findByName")
    public List<TbMusic> findByName(String musicName){
        List<TbMusic> tbMusics = tbMusicService.findByName(musicName);
        return tbMusics;
    }
    //根据id查询单个
    @RequestMapping("findById")
    public TbMusic findById(String musicId){
        return tbMusicService.findById(musicId);
    }

    //根据条件查询单个
    @RequestMapping("findOne")
    public TbMusic findOne(){
        return tbMusicService.findOne();
    }

    //根据map查询单个
    @RequestMapping("findOneByMap")
    public List<TbMusic> findOneByMap(){
        return tbMusicService.findOneByMap();
    }

    //根据条件查询记录数
    @RequestMapping("findCount")
    public Integer findCount(){
        return tbMusicService.findCount();
    }

    //分页查询
    @RequestMapping("findByPage")
    public PageInfo findByPage(@RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "4") Integer pageSize){

        PageHelper.startPage(pageNum,pageSize);

        List<TbMusic> tbMusics = tbMusicService.findAll();

        PageInfo<TbMusic> pageInfo = new PageInfo<>(tbMusics);

        return pageInfo;
    }

    //指定条件查询
    @RequestMapping("findByIds")
    public List<TbMusic> findByIds(String [] ids){
        List<TbMusic> tbMusics = tbMusicService.findByIds(ids);
        return tbMusics;
    }

}

Service

package com.qf.service;

import com.qf.pojo.TbMusic;

import java.util.List;

public interface TbMusicService {

    List<TbMusic> findAll();

    TbMusic findById(String musicId);

    List<TbMusic> findByName(String musicName);

    List<TbMusic> findByIds(String [] ids);

    TbMusic findOne();

    List<TbMusic> findOneByMap();

    Integer findCount();

    void add(TbMusic tbMusic);

    void deleteById(String musicId);

    void deleteByIds(String[] ids);

    void updateById(TbMusic tbMusic);

    void updateMusic(TbMusic tbMusic);
}

ServiceImpl

package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.dao.TbMusicMapper;
import com.qf.pojo.TbMusic;
import com.qf.service.TbMusicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@Service
public class TbMusicServiceImpl implements TbMusicService {

    @Autowired
    private TbMusicMapper tbMusicMapper;

    @Override
    public List<TbMusic> findAll() {
        return tbMusicMapper.selectList(null);
    }

    @Override
    public TbMusic findById(String musicId) {
        return tbMusicMapper.selectById(musicId);
    }

    @Override
    public List<TbMusic> findByName(String musicName) {

        QueryWrapper<TbMusic> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("music_name",musicName);
        return tbMusicMapper.selectList(queryWrapper);
    }

    @Override
    public List<TbMusic> findByIds(String [] ids) {
        return tbMusicMapper.selectBatchIds(Arrays.asList(ids));
    }

    @Override
    public TbMusic findOne() {
        QueryWrapper<TbMusic> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("music_artist_name","陈奕迅")
        .eq("music_name","十年");
        return tbMusicMapper.selectOne(queryWrapper);
    }

    @Override
    public List<TbMusic> findOneByMap() {

        HashMap<String, Object> map = new HashMap<>();
        map.put("music_artist_name","陈奕迅");

        return tbMusicMapper.selectByMap(map);
    }

    @Override
    public Integer findCount() {
        QueryWrapper<TbMusic> queryWrapper = new QueryWrapper<>();
        queryWrapper.gt("music_id",2);
        return tbMusicMapper.selectCount(queryWrapper);
    }

    @Override
    public void add(TbMusic tbMusic) {
        tbMusicMapper.insert(tbMusic);
    }

    @Override
    public void deleteById(String musicId) {
        tbMusicMapper.deleteById(musicId);
    }

    @Override
    public void deleteByIds(String[] ids) {
        tbMusicMapper.deleteBatchIds(Arrays.asList(ids));
    }

    @Override
    public void updateById(TbMusic tbMusic) {
        tbMusicMapper.updateById(tbMusic);
    }

    @Override
    public void updateMusic(TbMusic tbMusic) {
        QueryWrapper<TbMusic> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("music_id",3);

        tbMusicMapper.update(tbMusic,queryWrapper);
    }

}
1.5代码生成器

  1. 导入依赖
<dependency>
    <groupId>org.apache.velocity</groupId>
    <artifactId>velocity-engine-core</artifactId>
    <version>2.2</version>
</dependency>

<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
</dependency>

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
</dependency>

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.3.2</version>
</dependency>

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-generator</artifactId>
    <version>3.3.2</version>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>
  1. 编写配置类进行测试
package com.qf;

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 com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

public class MybatisPlusGenerator {
    public static void main(String[] args) {

        AutoGenerator mpg = new AutoGenerator();
        // 选择 freemarker 引擎,默认 Veloctiy
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        gc.setAuthor("yangl");
        gc.setOutputDir("D:\\java2002-springboot\\springboot-06\\src\\main\\java\\com\\qf");
        gc.setFileOverride(true);// 是否覆盖同名文件,默认是false
        gc.setEnableCache(false);// XML 二级缓存
        gc.setBaseResultMap(true);// XML ResultMap
        gc.setBaseColumnList(true);// XML columList
        gc.setServiceName("%sService");//去掉Service的前缀I
        //gc.setSwagger2(true); 实体属性 Swagger2 注解
        mpg.setGlobalConfig(gc);

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("root");
        dsc.setUrl("jdbc:mysql://localhost:3306/java2002?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai");
        mpg.setDataSource(dsc);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        //手动指定映射表名
        strategy.setInclude("tb_music");
        mpg.setStrategy(strategy);

        // 包配置
        PackageConfig pc = new PackageConfig();
        //pc.setModuleName("xxx");
        pc.setParent("yangl");
        //手动设置包名
        pc.setEntity("pojo");
        pc.setMapper("dao");
        pc.setXml("mapper");
        mpg.setPackageInfo(pc);

        // 执行生成
        mpg.execute();
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值