一、简单介绍
Mybatis Plus,简称MP,官网https://mp.baomidou.com/
在MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
意愿
:成为 MyBatis 最好的搭档,就像 魂斗罗 中的 1P、2P,基友搭配,效率翻倍。
二、环境准备和基本配置
1.创建Spring Boot项目,引入依赖
<!--mysql驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <!-- 整合mybatis plus--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.2.0</version> </dependency> <!-- 数据库连接池--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.9</version> </dependency> <!-- test依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
2.修改配置文件
server: port: 8099 spring: datasource: driver-class-name: com.mysql.jdbc.Driver username: root password: 12345 url: jdbc:mysql://127.0.0.1:3306/test type: com.alibaba.druid.pool.DruidDataSource
3.数据库准备
CREATE TABLE `student` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, `age` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
4.项目结构和实体类准备
实体类:
public class Student { private Integer id; private String name; private Integer age; //构造方法...... //Getter、Setter...... }
三、CRUD操作
1.继承BaseMapper
首先将Mapper层的接口集成BaseMapper,其中T就是需要操作的实体类泛型
。
@Mapper @Repository public interface StudentMapper extends BaseMapper<Student> { }
2.创建Service层接口和实现类
接口
public interface StudentService { /** * 保存Student * * @param student * @return */ int saveStudent(Student student); /** * 根据id查找 * * @param id * @return */ Student findStudentById(int id); /** * 查找全部 * * @return */ List<Student> fingAllStudnet(); /** * 删除一个 * * @param id * @return */ int deleteStudentById(int id); /** * 修改一个 * * @param student * @return */ int updateStudentById(Student student); /** * 根据姓模糊查询 * * @param firstName * @return */ List<Student> findStudentByFirstName(Student firstName); }
实现类
@Service public class StudentServiceImpl implements StudentService { @Autowired private StudentMapper studentMapper; @Override public int saveStudent(Student student) { return 0; } @Override public Student findStudentById(int id) { return null; } @Override public List<Student> findAllStudent() { return null; } @Override public int deleteStudentById(int id) { return 0; } @Override public int updateStudentById(Student student) { return 0; } @Override public List<Student> findStudentByFirstName(String firstName) { return null; } }
3.方法的实现
下面我们来一一的实现这些方法
(1)保存一个
@Override public int saveStudent(Student student) { return studentMapper.insert(student); }
测试:
@Autowired private StudentService studentService; @Test void testSaveStudent() { Student student = new Student(1,"张三", 23); System.out.println(studentService.saveStudent(student)); }
结果:
(2)根据id查找
@Override public Student findStudentById(int id) { QueryWrapper<Student> wrapper = new QueryWrapper<>(); wrapper.eq("id", id); return studentMapper.selectOne(wrapper); }
测试
@Test void testFindStuById() { System.out.println(studentService.findStudentById(1)); }
结果:
(3)查找全部
在查找全部之前我们先多插入一点
@Override public List<Student> findAllStudent() { return studentMapper.selectList(new QueryWrapper<>()); }
测试
@Test void testFindAllStu(){ System.out.println(studentService.findAllStudent()); }
结果:
(4)删除一个
@Override public int deleteStudentById(int id) { return studentMapper.deleteById(id); }
测试
@Test void testRemoveStudent(){ System.out.println(studentService.deleteStudentById(1)); }
结果
(5)修改一个
@Override public int updateStudentById(Student student) { return studentMapper.updateById(student); }
测试
@Test void testUpdateStu(){ System.out.println(studentService.updateStudentById(new Student(2,"张小四",200))); }
结果
(6)根据姓模糊查询
找出全部姓张的人
@Override public List<Student> findStudentByFirstName(String firstName) { QueryWrapper<Student> wrapper = new QueryWrapper<>(); wrapper.likeRight("name", firstName); return studentMapper.selectList(wrapper); }
测试
@Test void testLikeFind(){ System.out.println(studentService.findStudentByFirstName("张")); }
结果
四、可能遇到的问题
1.让主键自增
@TableId(type = IdType.AUTO) private Integer id;
2.实体类和表名不一致
//@TableName("数据库表名") @TableName("student")
3.实体类属性和表的字段名不一致
@TableId(value = "id", type = IdType.AUTO) private Integer id; @TableField("name") private String name; @TableField("age") private Integer age;
4.插入数据后让自增id回写(很实用)
@TableId(value = "id", type = IdType.AUTO) //只能有一个@TableId private Integer id; @TableField("name") private String name; @TableField("age") private Integer age;
测试下:
// Service @Override public int saveStudent(Student student) { int result = studentMapper.insert(student); System.out.println(student); return result; } // Test // @Test void testSaveStudent() { Student student = new Student( "张四", 23); System.out.println(studentService.saveStudent(student)); }