Spring boot集成MybatisPlus_springboot集成神通-程序员宅基地

技术标签: Spring boot集成MybatisPlus  

1.需要的依赖包

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatisplus-spring-boot-starter</artifactId>
    <version>1.0.5</version>
</dependency>
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus</artifactId>
    <version>2.3</version>
</dependency>
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.1</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

2.数据源配置

##端口号
server.port=8888

##数据库url
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mybatisplus?useUnicode=true&allowMultiQueries=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&serverTimezone=UTC
##数据库用户名
spring.datasource.username=root
##数据库密码
spring.datasource.password=123456
##数据库驱动
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

3.数据库脚本

DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` bigint(20) NOT NULL COMMENT '主键ID',
  `name` varchar(30) DEFAULT NULL COMMENT '姓名',
  `age` int(11) DEFAULT NULL COMMENT '年龄',
  `email` varchar(50) DEFAULT NULL COMMENT '邮箱',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `user` VALUES ('1', 'Jone', '25', '[email protected]');
INSERT INTO `user` VALUES ('2', 'Jack', '20', '[email protected]');
INSERT INTO `user` VALUES ('3', 'Tom', '28', '[email protected]');
INSERT INTO `user` VALUES ('4', 'Sandy', '21', '[email protected]');
INSERT INTO `user` VALUES ('5', 'Billie', '24', '[email protected]');

4.实体类User

public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;

    public User() {
    }

    public User(Long id, String name, Integer age, String email) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.email = email;
    }

    public Long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

5.创建UserMapper,继承BaseMapper<User>

import com.baomidou.mybatisplus.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface UserMapper extends BaseMapper<User> {

}

6.写测试类

@RunWith(SpringRunner.class)
@SpringBootTest(classes = DemoApplication.class,webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class DemoApplicationTests {
    @Autowired(required = false)
    private UserMapper userMapper;
    @Test
    public void contextLoads() {
        List<User> users = userMapper.selectList(null);
        for (User user : users) {
            System.out.println(user);
        }
    }

    @Test
    public void testQuery() {
        Map<String,Object> map = new HashMap();
        map.put("name","Jone");
        List<User> users = userMapper.selectByMap(map);
        for (User user : users) {
            System.out.println(user.getName());
        }
    }

    @Test
    public void testInsert() {
        User user = new User(19l, "test", 23, "qq.com");
        Integer insert = userMapper.insert(user);
        System.out.println(insert);
    }

    @Test
    public void testWrapper() {
        EntityWrapper<User> wrapper = new EntityWrapper<>();
        wrapper.eq("name", "Jone");
        wrapper.or();
        wrapper.like("name", "J");
        List<User> users = userMapper.selectList(wrapper);
        for (User user : users) {
            System.out.println(user.getName());
        }
    }

    @Test
    public void testUpdate() {
        EntityWrapper<User> userEntityWrapper = new EntityWrapper<>();
        userEntityWrapper.eq("name", "Jone");
        User user = new User(15l, "test01", 20, "[email protected]");
        Integer update = userMapper.update(user, userEntityWrapper);
        System.out.println(update);
    }

    @Test
    public void testUpdate2() {
        User user = new User(1l, "Jone", 25, "[email protected]");
        Integer integer = userMapper.updateById(user);
        System.out.println(integer);
    }

    @Test
    public void testDelete() {
        Integer integer = userMapper.deleteById(19l);
        System.out.println(integer);
    }
}

运行测试例子就可以进行CRUD操作了

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_31324077/article/details/100916959

智能推荐

oracle数据库中分页查询-程序员宅基地

最近整理了有关用jsp实现oracle数据库分页的两种方法,贴出来与大家交流一下。第一种是用jsp实现oracle数据库分页。 我的根目录是G:\workspace\hope\WebContent 2.jsp代码如下:&lt;%@ page language="java" contentType="text/html; charset=gb2312"%&gt;&lt;%...

网关 Spring Cloud Zuul 给Zuul路由加上自动重试_connection reset zuul_lakernote的博客-程序员宅基地

文章目录概要依赖导入配置文件概要当Netflix Zuul用作将传入请求转发到后端服务的网关时,总是有一个请求可能无法后端服务的机会。当请求失败时,您可能希望自动重试该请求。为此,在使用Sping Cloud Netflix时,您需要在应用程序的类路径中包括Spring Retry。当出现Spring Retry时,负载平衡的Zuul会自动重试任何失败的请求(如下示例配置,如果后端服务关闭,Zuul将重试2次)。Zuul使用的默认HTTP客户端现在由Apache HTTP客户端而不是已弃用的Ri_connection reset zuul

开源 java CMS - FreeCMS2.8 自定义表单-程序员宅基地

项目地址:http://www.freeteam.cn/自定义表单从FreeCMS 2.5开始支持支持字段分组,后台维护数据,前台提交数据,定位于单表的数据收集。1. 表单管理a. 添加表单从左侧管理菜单点击自定义表单进入。 点击“添加”按钮 输入相关属性点击“保存”即可。b. 编辑表单选择需要编辑的表单。 点击“编辑” 输入相关属性点击“保存”即可。c. 删除表单删除表单功能将删除指定表单下的...

Appweb-7.2.3交叉编译支持html_appweb下载-程序员宅基地

Appweb-7.2.3交叉编译支持html环境:Ubuntu1604编译链:4.8的Android编译链Appweb源码:appweb-src.tgzAndroid模块:RK3288一、官网下载源码并解压源码(注意官网下载的是社区版,功能有阉割,像Building with FastCGI, Building with Proxy,Building with PHP,都没有,但跑个html还是没有问题的)tar -xjvf appweb-src.tgzcd appweb-7.2.3二、修改_appweb下载

应用程序,操作系统,驱动程序的关系_驱动和操作系统的关系-程序员宅基地

硬件和软件 计算机资源分为硬件资源和软件资源,硬件资源包括cpu,内存,显卡,网卡,声卡,硬盘等等。软件资源包括各种程序。每个硬件完成特定的功能,比如显卡完成在显示设备上显示图形,声卡实现声音的处理。再比如,你用qq发送一段文字给一个同学,那么网卡会将信息发送给网络。系统软件和应用软件 系统软件就是包各种硬件驱动程序,它们的任务就是用来驱动各种硬件,使硬件完成正常的功能。..._驱动和操作系统的关系

go代码函数调用关系工具go-callvis_attention: default value of option mesa_glthread o_计科小可爱的博客-程序员宅基地

1. 安装方式操作系统:ubuntu安装gosudo snap install --classic gogo语言要安装最新版本(go version go1.17.2 linux/amd64),不然后面可能会出现包缺失的的错误查看go版本go version查看go环境变量go env修改环境变量GOPROXYexport GOPROXY="https://goproxy.cn,direct"安装gcc编译器sudo apt install gcc安装go-callvis_attention: default value of option mesa_glthread overridden by environment.

随便推点

python学习笔记:浮点数计算问题(20180920)-程序员宅基地

python输出过程:一个十进制数———以二进制形式(近似值)存储到计算机———输出该二进制近似值的十进制近似值举例:浮点数0.3的二进制表示十进制小数转二进制采用"乘2取整,顺序排列"法。具体做法是:用2乘十进制小数,将积的整数部分取出,再用2乘余下的小数部分,再将积的整数部分取出,如此循环,直到积中的小数部分为零,此时0或1为二进制的最后一位。或者达到所要求的精度为止。...

Redis 跳跃表记录_redis中跳跃表-程序员宅基地

首先我们找到Context源码包:_redis中跳跃表

使用charles篡改数据进行测试-程序员宅基地

第一步,设置断点: 第二步,断点设置成功,页面处于loading状态: 第三步,点击Execute后,等数据返回进行修改response的数据,再次点击Execute,即可实现篡改返回数据来进行app的测试,eg:修改订单状态等。 ...

Learn UML with JUDE(中文版)-程序员宅基地

原文地址:http://jude.change-vision.com/jude-web/download/try_uml.html 我希望你能够使用JUDE去学习和体验UML,JUDE是一个建模工具,你可以用它去画UML。下面我会指导你通过一些实例去学习使用JUDE来画UML。一、Overview l UML and UML tools l Description of JUDE

通过代码测试算法的性能--以排序算法为例-程序员宅基地

我们编写一个计算(算法执行时间)的函数来测试算法的性能,(即本文性能把算法执行时间的长短作为标准)这里以排序函数为例作为被测函数# include&lt;iostream&gt;#include&lt;ctime&gt; #include&lt;cassert&gt; using namespace std;template &lt;typename T&gt;void selecti...