springboot整合mybatisplus

1.创建一个springboot项目  pom.xml引用

<!--简略get、set方法-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.10</version>
        </dependency>
        <!--Mybatis-Plus 注意版本-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.1</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.5.0</version>
        </dependency>
        <!--数据库连接驱动 连接配置修改时间-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!--模板引擎-->
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
            <version>2.6.5</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.3.17</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.3.13</version>
        </dependency>

2.application.yml文件添加配置并且创建测试表

server:
  port: 8087

  mvc:
    static-path-pattern: /**

spring:
  datasource:
    #    驱动
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/educationapp?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
    username: root
    password: 123456

mybatis-plus:
  #控制台打印sql
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    #开启驼峰功能
  map-underscore-to-camel-case: true
  mapper-locations:  classpath*:/mappers/**/*.xml
  type-aliases-package: com.mabp.emptity
create table `user_test1` (
	`id` int (11),
	`username` varchar (60),
	`age` int (11),
	`tel` int (11),
	`create_time` timestamp ,
	`update_time` timestamp ,
	`version` int (11)
); 
insert into `user_test1` (`id`, `username`, `age`, `tel`, `create_time`, `update_time`, `version`) values('1','张三','18','180',NULL,NULL,NULL);
insert into `user_test1` (`id`, `username`, `age`, `tel`, `create_time`, `update_time`, `version`) values('2','李四','20','137',NULL,NULL,NULL);
insert into `user_test1` (`id`, `username`, `age`, `tel`, `create_time`, `update_time`, `version`) values('3','王五','22','138',NULL,NULL,NULL);

3.创建一个实体类

@Data    //使用lombok 简化getset方法
@AllArgsConstructor
@NoArgsConstructor
@TableName(value = "user_test1", autoResultMap = true)
public class UserTest {
    @TableId(value = "id", type = IdType.AUTO)
    private int id;

    private String username;

    private int age;

    private int tel;

}

4.创建mapper继承与   BaseMapper

package com.mabp.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.mabp.emptity.UserTest;
import org.apache.ibatis.annotations.Mapper;


@Mapper
public interface UserTestMapper extends BaseMapper<UserTest> {


}

4.编写测试类测试

package com.mabp.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mabp.SpmpApplication;
import com.mabp.emptity.UserTest;
import com.mabp.mapper.UserTestMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("api/v1/test/my")
@SpringBootTest(classes = SpmpApplication.class) //SpmpApplication启动类名字
@RunWith(SpringJUnit4ClassRunner.class)
@Api(tags = "swagger测试模块")
public class TestMyController {

    //如果出现红色下划线不影响运行,可以设置idea不报红
    @Autowired
    private UserTestMapper userTestMapper;

    @Test
    public void Insert() {
        UserTest userTest = new UserTest();
        userTest.setAge(17);
        userTest.setTel(17);
        userTest.setUsername("我是付林");
        int result = userTestMapper.insert(userTest);
        System.out.println(result);             // 受影响的行数
        System.out.println(userTest);               // 发现,未设置的id会自动回填

    }



    /**
     * 查询所有用户信息
     */

    @GetMapping("list")
    @ApiOperation(value="查询所有用户信息",notes = "查询所有用户信息",httpMethod = "get")
    public List<UserTest> test(){
        QueryWrapper<UserTest> wrapper = new QueryWrapper<>();
//        wrapper.select("id","username");
        //直接调用BaseMapper封装好的CRUD方法,就可实现无条件查询数据
        List<UserTest> list = userTestMapper.selectList(null);

        //循环获取用户数据
        for (UserTest userTest:list){
            //获取用户名称
            System.out.println(userTest.getUsername());
        }
        return list;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

滴滴答答哒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值