SpringBoot整合Mybatis-plus笔记

1.maven依赖


    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>



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

        <!--数据库-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>

        <!-- mybatis-plus -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>2.3</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.6</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.9</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
        </plugins>
    </build>

2.yml配置

spring:
  datasource:
    driverClassName: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/bbs?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
    username: root
    password: root
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      stat-view-servlet:
        url-pattern: /druid/*
        reset-enable: true
        login-username: admin
        login-password: admin
      validation-query: SELECT 'x'

3.实体类

@Data
@TableName(value = "bbs_test")
public class Detail {

    @TableId
    private String id ;  //) NOT NULL编号
    private Integer sortId;  // bigint(20) NOT NULL分类编号
    private String title;  // varchar(50) NOT NULL标题
    private String detail;  // varchar(200) NOT NULL详细内容
    private String author;  // varchar(50) NOT NULL作者;
    @JsonFormat(pattern = "yyyy-MM-dd")
    private Date createDate;  // timestamp NOT NULL发布时间
    private Integer replyCount;  // int(11) NOT NULL回复次数


}

4.Dao层

@Mapper
public interface DetailMapper extends BaseMapper<Detail> {


}

extends BaseMapper<Detail>
里面的参数是实体类名称

5.增删改查测试

@RestController
public class DetailContoller {

    @Autowired
    private DetailMapper mapper;

    @GetMapping("/selectOne/{id}")
    public Object testDetailForId(@PathVariable("id")String id){

        Detail details = mapper.selectById(id);
        return details;
    }

    @GetMapping("/select/{limit}/{page}")
    public Object testDetail(@PathVariable("limit")Integer limit,@PathVariable("page")Integer page){

        //精准查询 eq and detail = detail
//        Map<String, Object> map = new HashMap<>();
//        map.put("detail",detail);
//        List<Detail> details = mapper.selectByMap(map);

//        Wrapper<Detail> wap = new EntityWrapper<>(); //多条件查询
//        //select * form xx where xx like "
//        wap.like("detail",detail); //模糊查询
//        //select * from xxx where orderId between 10 and 20
//        wap.notBetween("orderId",10,20);
//        //select * from xxx where createDate between 2019-1-1 and 20
//        wap.between("createDate","2019-1-1","2019-3-3");
//        wap.eq("detail",detail);
//        wap.orderBy("id"); //asc
//        Collection<String> sa = new ArrayList<>();
//        //select * from xxx where createDate between 2019-1-1 and 20 order by id,name desc
//        ((ArrayList<String>) sa).add("name");
//        wap.orderDesc(sa);
//
//        List<Detail> details = mapper.selectList(wap);

        //分页
        Wrapper<Detail> wap = new EntityWrapper<>();
        RowBounds row = new RowBounds(limit,page);
        List<Detail> details = mapper.selectPage(row, wap);
        return details;
    }

    @GetMapping("/add")
    public String add(){
        Detail detail = new Detail();
        detail.setId(UUID.randomUUID().toString().substring(1,6));
        detail.setAuthor("你猜");
        detail.setReplyCount(321);
        detail.setCreateDate(new Date());
        detail.setDetail("whgfsdjhgfdgfd");
        detail.setSortId(1);
        detail.setTitle("title111");
        System.out.println(detail);
        return String.valueOf(mapper.insert(detail));
    }

    @GetMapping("/update/{id}")
    public String update(@PathVariable("id")String id){
        Detail detail = new Detail();
        detail.setId(id);
        detail.setAuthor("修改--1");
        detail.setReplyCount(321);
        detail.setCreateDate(new Date());
        detail.setDetail("修改--whgfsdjhgfdgfd");
        detail.setSortId(1);
        detail.setTitle("修改--title111");
        return String.valueOf(mapper.updateById(detail));
    }

    @GetMapping("/delete/{id}")
    public String delete(@PathVariable("id")String id){
        return String.valueOf(mapper.deleteById(id));
    }

}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值