您的当前位置:首页正文

mybaits-plus入门及工程建立

2023-07-16 来源:钮旅网


mybaits-plus入门及工程建立

文章目录

• 准备工作

创建数据库

创建数据库以及表

创建数据库并设置字符集为utf-8:

CREATE DATABASE `mp` CHARACTER SET 'utf8' COLLATE 'utf8_general_ci';

创建表和测试数据:

--

-- 表的结构 `tb_user`

--

CREATE TABLE IF NOT EXISTS `tb_user` (

`id` bigint(20) NOT NULL COMMENT '主键ID',

`user_name` varchar(20) NOT NULL COMMENT '用户名',

`password` varchar(20) NOT NULL COMMENT '密码',

`name` varchar(30) DEFAULT NULL COMMENT '姓名',

`age` int(11) DEFAULT NULL COMMENT '年龄',

`email` varchar(50) DEFAULT NULL COMMENT '邮箱',

`birthday` datetime DEFAULT NULL

) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

--

-- 表中的数据 `tb_user`

--

INSERT INTO `tb_user` (`id`, `user_name`, `password`, `name`, `age`, `email`, `birthday`) VALUES

(1, 'zhangsan', '123456', '张三',18,'************','2022-03-0900:00:00'),

(2, 'lisi', '123456', '李四',20,'************','2022-03-0900:00:00'),

(3, 'wangwu', '123456', '王五',28,'************','2022-03-0900:00:00'),

(4, 'zhaoliu', '123456', '赵六',21,'************','2022-03-0900:00:00'),

(5, 'sunqi', '123456', '孙七',24,'************','2022-03-0900:00:00');

工程搭建

工程结构

创建工程

创建maven工程,分别填写GroupId ArtifactId和Version,如下:

cn.yyl.mp

yyl-mp-springboot

1.0-SNAPSHOT

导入依赖

导入maven依赖,由于本例采用Spring boot技术,使用mybatis-plus-boot-starter能与其便捷集成: 版本规划:

Spring boot: 2.1.3.RELEASE

mybatis-plus: 3.1.0

pom文件如下:

xmlns:xsi=\"/2001/XMLSchema-instance\"

xsi:schemaLocation=\"/POM/4.0.0 /xsd/maven-4.0.0.xsd\">

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.1.3.RELEASE

org.springframework.boot

spring-boot-starter

org.springframework.boot

spring-boot-starter-test

test

org.projectlombok

lombok

true

com.baomidou

mybatis-plus-boot-starter

3.1.0

mysql

mysql-connector-java

8.0.11

编写application.properties

spring.application.name = yyl-mp-springboot

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mp?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=false

spring.datasource.username=root

spring.datasource.password=root

# Logger Config

logging.level.root: debug

编写pojo

package cn.yyl.mp.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.AllArgsConstructor;

import lombok.Data;

import lombok.NoArgsConstructor;

import lombok.ToString;

import java.time.LocalDateTime;

/**

* @author Administrator

* @version 1.0

**/

@Data

@ToString

@NoArgsConstructor

@AllArgsConstructor

@TableName(\"tb_user\")

public class User {

@TableId(value = \"id\指定主键的字段名

private Long id;

@TableField(\"user_name\")//指定对应字段名

private String userName;

@TableField(\"PASSWORD\")

private String password;

@TableField(\"NAME\")

private String name;

@TableField(\"AGE\")

private Integer age;

@TableField(\"EMAIL\")

private String email;

@TableField(\"BIRTHDAY\")

private LocalDateTime birthday;

}

注解说明:

lomlok:

@Data:lombok的注解,使用它可以省略getter/setter方法。

@NoArgsConstructor: 生成无参构造 方法

@AllArgsConstructor:生成所有参数构造 方法,参数顺序与属性定义顺序一致。

mybaits-plus:

@TableName:指定表名

@TableId:指定主键名

@TableField:指定列名

编写mapper

package cn.yyl.mp.mapper;

import cn.yyl.mp.pojo.User;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;

/**

* Created by Administrator.

*/

public interface UserMapper extends BaseMapper {

}

该mapper类继承mybatisplus中的BaseMapper类

BaseMapper类中有一些常用的数据库查询方法,使用Base类之外的查询方法可以自己手动配置xml实现:

//

// Source code recreated from a .class file by IntelliJ IDEA

// (powered by Fernflower decompiler)

//

package com.baomidou.mybatisplus.core.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;

import com.baomidou.mybatisplus.core.metadata.IPage;

import java.io.Serializable;

import java.util.Collection;

import java.util.List;

import java.util.Map;

import org.apache.ibatis.annotations.Param;

public interface BaseMapper {

int insert(T entity);

int deleteById(Serializable id);

int deleteByMap(@Param(\"cm\") Map columnMap);

int delete(@Param(\"ew\") Wrapper wrapper);

int deleteBatchIds(@Param(\"coll\") Collection idList);

int updateById(@Param(\"et\") T entity);

int update(@Param(\"et\") T entity, @Param(\"ew\") Wrapper

updateWrapper);

T selectById(Serializable id);

List selectBatchIds(@Param(\"coll\") Collection idList);

List selectByMap(@Param(\"cm\") Map columnMap);

T selectOne(@Param(\"ew\") Wrapper queryWrapper);

Integer selectCount(@Param(\"ew\") Wrapper queryWrapper);

List selectList(@Param(\"ew\") Wrapper queryWrapper);

ListObject>> selectMaps(@Param(\"ew\") Wrapper

List selectObjs(@Param(\"ew\") Wrapper queryWrapper);

IPage queryWrapper);

selectPage(IPage page, @Param(\"ew\") Wrapper

IPage> selectMapsPage(IPage page, @Param(\"ew\") Wrapper queryWrapper);

}

编写启动类

package cn.yyl.mp;

import org.mybatis.spring.annotation.MapperScan;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

/**

* @author Administrator

* @version 1.0

**/

@SpringBootApplication

@MapperScan(\"cn.yyl.mp.mapper\") //设置mapper接口的扫描包

public class MyApplication {

public static void main(String[] args) {

SpringApplication.run(MyApplication.class,args);

}

}

注解说明:

@SpringBootApplication springboot启动入口标识

@MapperScan(\"cn.yyl.mp.mapper\") mapper接口的扫描包

测试

编写测试用例

编写UserMapper的测试用例 ,使用UserMapper查询用户列表。

在test下创建测试类,包名为 cn.yyl.mp。

package cn.itcast.mp;

import cn.itcast.mp.mapper.UserMapper;

import cn.itcast.mp.pojo.User;

import

com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;

import com.baomidou.mybatisplus.core.metadata.IPage;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.test.context.SpringBootTest;

import org.springframework.test.context.junit4.SpringRunner;

import java.time.LocalDateTime;

import java.time.format.DateTimeFormatter;

import java.util.Arrays;

import java.util.List;

/**

* @author Administrator

* @version 1.0

**/

@RunWith(SpringRunner.class)

@SpringBootTest

public class UserMapperTest {

@Autowired

UserMapper userMapper;

x

@Test

public void testSelect(){

//查询tb_user记录

List users = userMapper.selectList(null);

System.out.println(users.toString());

}

}

运行测试

[

User(id=1, userName=zhangsan, password=123456, name=张三, age=18, email=***************,birthday=2022-03-09T00:00),

User(id=2, userName=lisi, password=123456, name=李四, age=20, email=***************,birthday=2022-03-09T00:00),

User(id=3, userName=wangwu, password=123456, name=王五, age=28,

email=***************,birthday=2022-03-09T00:00),

User(id=4, userName=zhaoliu, password=123456, name=赵六, age=21, email=***************,birthday=2022-03-09T00:00),

User(id=5, userName=sunqi, password=123456, name=孙七, age=24, email=***************,birthday=2022-03-09T00:00)

]

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- 版权所有