springboot集成mybatisplus

关键代码:

yml:

  datasource:
    driver-class-name: org.gjt.mm.mysql.Driver
    url: jdbc:mysql://localhost:3306/shishi?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: root

mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

pom:

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <!-- mybatis plus -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>2.3</version>
        </dependency>

main:

@SpringBootApplication
@MapperScan(basePackages = "com.shishi.mapper")
public class MainApplication {
    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class, args);
    }
}

mapper类:

@Repository
public interface HappyGirlMapper extends BaseMapper<HappyGirl> {
}

bean:

@TableName("happy_girl")
public class HappyGirl implements Serializable {

    @TableField(value = "id")
    private int id;

    @TableField("happy_thing")
    private String happyThing;

    @TableField("like_what")
    private String likeWhat;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getHappyThing() {
        return happyThing;
    }

    public void setHappyThing(String happyThing) {
        this.happyThing = happyThing;
    }

    public String getLikeWhat() {
        return likeWhat;
    }

    public void setLikeWhat(String likeWhat) {
        this.likeWhat = likeWhat;
    }
}

使用:

@RestController
public class TestController {
    @Autowired
    private HappyGirlMapper mapper;

    @RequestMapping("/insert")
    public String insert() {
        HappyGirl girl = new HappyGirl();
        girl.setHappyThing("hey");
        girl.setLikeWhat("sleep");
        girl.setId(3);
        mapper.insert(girl);
        return "success";
    }
    @RequestMapping("/delete")
    public int delete() {
        HappyGirl girl = new HappyGirl();
        girl.setId(1);
        int num = mapper.deleteById(girl);
        return num;
    }
}

 简单版本这样即完成

 

posted @ 2021-08-13 00:06  圣金巫灵  阅读(72)  评论(0编辑  收藏  举报