您的位置:首页 > 编程语言 > Java开发

SpringBoot中的MybatisPlus的使用——入门

2020-02-29 19:30 405 查看

MybatisPlus的入门

MybatisPlus是对于Mybatis的一种继承增强,在不改变Mybatis的基础上,增强了Mybatis的使用效率。

1.Jar包的导入

使用IDEA创建一个SpringBoot项目,选择以下的jar包安装。

在项目创建成功之后,在pom.xml中添加:MybatisPlus、druid依赖。

//这是MybatisPlus的依赖Jar包
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.0</version>
</dependency>
// 这是druid连接池的依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.20</version>
</dependency>

2.SpringBoot的配置

SpringBoot的

application.properties
配置内容如下:

// 访问端口号
server.port=8080
// 防止页面显示时出现乱码
server.tomcat.uri-encoding=UTF-8
spring.http.encoding.force=true
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
// 数据库的配置
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

3.数据库SQL代码(MySQL)

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `t_one`
-- ----------------------------
DROP TABLE IF EXISTS `t_one`;
CREATE TABLE `t_one` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(30) CHARACTER SET utf8 NOT NULL,
`value` int(11) NOT NULL,
`is_delete` int(2) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1;

-- ----------------------------
-- Records of t_one
-- ----------------------------
INSERT INTO <
1bb8c
span class="token punctuation">`t_one` VALUES ('1', '苹果', '20', '0');
INSERT INTO `t_one` VALUES ('2', '梨', '30', '0');
INSERT INTO `t_one` VALUES ('3', '菠萝', '50', '1');
INSERT INTO `t_one` VALUES ('4', '哈密瓜', '100', '0');
INSERT INTO `t_one` VALUES ('5', '西瓜', '60', '0');
INSERT INTO `t_one` VALUES ('6', '凤梨', '65', '1');

4.实体类的实现

实体类的代码中使用了lombok,但是仅仅在pom.xml中添加lombok的依赖还是不能使用lombok。还需要在编辑器中添加Lombok插件。
File->选择settings->选择plusins->在输入框输入lombok(以下是已经安装):

安装完成之后,会有一个Restart按钮,要重启IDEA。此时可以选择重启之后再进行以下步骤,也可以先进行以下步骤再重启编辑器。
File->选择Build、Execution、Deployment->选择Compiler->将Enable annotation processing选上即可。

完成以上步骤之后,即可使用Lombok。Lombok是为了节省toString、get、set等的一种工具。

/*
自动为所有字段添加@ToString, @EqualsAndHashCode, @Getter方法,
为非final字段添加@Setter,和@RequiredArgsConstructor
*/
@Data
// 自动生成无参数构造函数。
@NoArgsConstructor
// 自动生成全参数构造函数。
@AllArgsConstructor
@ToString
// 映射的表名
@TableName("t_one")
public class One {
// 主键,自增,需要指定,否则无法新增后拿到回调的id,以及进行删除等操作
@TableId(type = IdType.AUTO)
private Integer id;
private String name;
private int value;
/**
注意:如果数据表中存在有下划线的字段时,如:is_delete
此时,实体类的属性名不再是is_delete,而是isDelete
*/
// TableField注释是指定字段名
@TableField(value = "is_delete")
private int isDelete;

public One(String name,int value){
this.name = name;
this.value = value;
}
}

5.Mapper的实现

此时的Mapper只需要继承MybatisPlus中的BaseMapper,就可以实现简单的增、删、改、查。(如果要自己编写SQL代码,则需要创建Mapper.xml,同时在application.properties中进行mybatis的配置)

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.domain.One;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface OneMapper extends BaseMapper<One> {
}

6.Controller的测试

@Controller
@RequestMapping("test")
public class TestController {
@Autowired
private OneMapper oneMapper;

@RequestMapping("test")
@ResponseBody
public List<One> test(){
// 调用的是BaseMapper中的方法,返回的是一个List
List<One> ones = oneMapper.selectList(null);
return ones;
}
}

7.页面数据

8.总结

到此,MybatisPlus的简单入门已经结束了,虽然没有尝试自己编写SQL代码,但是plus是有这个功能的。

  • 点赞
  • 收藏
  • 分享
  • 文章举报
阿萨德非 发布了1 篇原创文章 · 获赞 0 · 访问量 50 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: