Mybatis-Plus是一款 MyBatis 动态 sql 自动注入 crud 简化 增 删 改 查 操作中间件。启动加载 XML 配置时注入 mybatis 单表 动态 SQL 操作 ,为简化开发工作、提高生产率而生。Mybatis-Plus 启动注入非拦截实现、性能更优。
【1】点击:file—>new—>project,选择 Spring Initializr,直接点击下一个
【2】填写相关选项,点击Next
【注】如果出现以下情况请按照图中解决方法解决
【3】这个页面选项是选择SpringBoot需要的启动依赖,然后点击Next
【4】保存路径,点击Finish
【5】创建好后目录结构如下
创建controller: com.lhx.controller.QuickController
package com.lhx.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
public class QuickController {
@RequestMapping("/quick2")
public String quick(){
return "SpringBoot";
}
}
运行MyspringbootApplication类中的main方法,控制台显示:
这里为了方便演示,就直接创建一个简单的User表
建表语句如下:
DROP TABLE IF EXISTS USER;
CREATE TABLE USER
(
id INT(20) NOT NULL COMMENT '主键ID',
NAME VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
age INT(11) NULL DEFAULT NULL COMMENT '年龄',
email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (id)
);
插入测试数据:
INSERT INTO user (id,name,age,email) VALUES
(1,'onestar',18,'[email protected]'),
(2,'twostar',18,'[email protected]'),
(3,'threestar',18,'[email protected]'),
(4,'fourstar',18,'[email protected]'),
(5,'fivestar',18,'[email protected]');
这里需要将mybatis-plus、MySQL依赖导入,另外,为了简化实体类,不写get、set、tostring方法,添加lombok依赖,用lombok需要下载插件,可以再settings里面的Plugins搜索下载,下载后重启idea。
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
springboot配置文件,添加以下两个配置,如下:
1.数据库配置:driver、url、username、password
2.mybatisplus日置配置:用来在终端显示数据库执行的详细信息
#mysql
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis_plus?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=x5
#mybatis-plus
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
在com.lhx包下创建两个包,并创建User实体类和UserMapper接口,目录结构如下:
这里还需要让springboot扫描到mapper接口,在MybatisplusdemoApplication类中添加注解@MapperScan(“com.lhx.mapper”)
@SpringBootApplication
@MapperScan("com.lhx.mapper") //扫描到mapper接口
public class MybatisplusdemoApplication {
...}
package com.lhx.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
@Data //可以简化实体类
public class User {
@TableId(type = IdType.AUTO)
private Long id;
private String name;
private Integer age;
private String email;
}
分析:
package com.lhx.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lhx.entity.User;
import org.springframework.stereotype.Repository;
@Repository
public interface UserMapper extends BaseMapper<User> {
}
编写好以上代码后,框架基本就搭建好了,可以在测试类中进行测试了,在test中添加测试,代码如下:
@Autowired
private UserMapper userMapper;
//查询User
@Test
void findUser() {
//查询列表
List<User> user = userMapper.selectList(null);
System.out.println("user:" + user);
//根据ID查询
User user1 = userMapper.selectById(1L);
System.out.println("user1:" + user1);
//通过多个ID批量查询
List<User> users = userMapper.selectBatchIds(Arrays.asList(1, 2, 3));
users.forEach(System.out::println);
//简单的条件查询
HashMap<String, Object> map = new HashMap<>();
map.put("name", "onestar");
map.put("age", 18);
List<User> users2 = userMapper.selectByMap(map);
users2.forEach(System.out::println);
}
//添加User
@Test
void addUser(){
User user = new User();
user.setName("sixstar");
user.setAge(18);
user.setEmail("[email protected]");
int insert = userMapper.insert(user);
System.out.println(insert);
}
//修改User
@Test
void updateUser(){
User user = new User();
user.setId(1L);
user.setAge(12);
int result = userMapper.updateById(user);
System.out.println(result);
}
//删除User
@Test
void deleteUser(){
//根据ID删除
int result = userMapper.deleteById(5L);
System.out.println(result);
//批量删除
int result2 = userMapper.deleteBatchIds(Arrays.asList(1,2,3));
System.out.println(result2);
//简单的条件删除
HashMap<String,Object> map = new HashMap<>();
map.put("name","fourstar");
map.put("age",18);
int result3 = userMapper.deleteByMap(map);
System.out.println(result3);
}
以上就是使用IDEA搭建Mybatis-plus框架并实现数据库增删改查啦!