1、简介
2、支持的数据库
3、框架
4、创建一个springboot项目
目录结构:
4.1 、pom文件中加入依赖
<!-- mybatisPlus的依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<!-- lombox的依赖-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- mysql的依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
这里可能会出现的问题:编译打包不成功(修改版本)。
解决方法如下:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!--在这里修改版本-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
</plugin>
<!---->
</plugins>
</build>
4.2、yml文件的配置
spring:
# 配置数据源信息
datasource:
# 配置数据源类型
type: com.zaxxer.hikari.HikariDataSource
# 配置连接数据库的各个信息
username: root
password: root
url: jdbc:mysql://localhost:3306/mybatis_plus?allowMultiQueries=true&characterEncoding=UTF-8&characterSetResults=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false
driver-class-name: com.mysql.cj.jdbc.Driver
server:
port: 8383
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
4.3 、数据库脚本
创建数据库和表
CREATE DATABASE `mybatis_plus` /*!40100 DEFAULT CHARACTER SET utf8mb4 */;
use `mybatis_plus`;
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 (id, name, age, email) VALUES
(1, 'Jone', 18, 'test1@'),
(2, 'Jack', 20, 'test2@'),
(3, 'Tom', 28, 'test3@'),
(4, 'Sandy', 21, 'test4@'),
(5, 'Billie', 24, 'test5@');
4.4、实体类
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
4.5 、启动类添加包扫描
@SpringBootApplication
//扫描mapper接口所在的包
@MapperScan("com.zyz.mybatisplus.mapper")
public class MybatisplusApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisplusApplication.class, args);
}
}
4.6、 mapper包下的UserMapper接口
@Repository
public interface UserMapper extends BaseMapper<User> {
}
4.7、 测试mybatisplus
@SpringBootTest
public class MyBatisPlusTest {
@Autowired
private UserMapper userMapper;
// @Test
// public void testSelectList() {
//
// List<User> list = userMapper.selectList(null);
// list.forEach(System.out::println);
//
// }
// 测试增加
@Test
public void testAdd(){
//实现用户新增
//INSERT INTO user ( id, name, age, email ) VALUES ( ?, ?, ?, ? )
User user = new User();
// user.setId(5L);
user.setName("张三");
user.setAge(23);
user.setEmail("zhangsan@");
int rs = userMapper.insert(user);
System.out.println("测试结果:"+rs);
System.out.println(user.getId());
}
@Test
public void testDelete(){
/**
* 1、根据id删除信息
* DELETE FROM user WHERE id=?
*/
int rs = userMapper.deleteById(5L);
System.out.println(rs);
/**
* 2、根据map集合中所设置的条件删除用户信息
* DELETE FROM user WHERE name = ? AND age = ?
*/
// HashMap<String, Object> map = new HashMap<>();
// map.put("name","张三");
// map.put("age",23);
// int rs = userMapper.deleteByMap(map);
// System.out.println(rs);
/**
* 通过多个id实现批量删除
* DELETE FROM user WHERE id IN ( ? , ? , ? )
*/
// List<Long> list = Arrays.asList(1L,2L,3L);
// int rs = userMapper.deleteBatchIds(list);
// System.out.println(rs);
}
@Test
public void testUpdate(){
/**
* 修改用户信息
* UPDATE user SET name=?, email=? WHERE id=?
*/
User user = new User();
user.setId(4L);
user.setName("李四");
user.setEmail("lisi@");
int rs = userMapper.updateById(user);
System.out.println(rs);
}
@Test
public void testSelect(){
/**
* 通过id查询用户信息
* SELECT id,name,age,email FROM user WHERE id=?
*/
// User user = userMapper.selectById(4L);
// System.out.println(user);
/**
* 根据id批量查询用户信息
* SELECT id,name,age,email FROM user WHERE id IN ( ? , ? )
*/
// List<Long> list = Arrays.asList(4L,5L);
// List<User> users = userMapper.selectBatchIds(list);
// users.forEach(System.out::println);
/**
* 根据条件查询用户信息
* SELECT id,name,age,email FROM user WHERE name = ? AND age = ?
*/
// Map<String,Object> map = new HashMap<>();
// map.put("name","李四");
// map.put("age",21);
// List<User> users = userMapper.selectByMap(map);
// users.forEach(System.out::println);
//自定义sql语句查询用户信息
Map<String,Object> map = userMapper.selectMapById(1L);
System.out.println(map);
}
}