1.案例详解
首先在Navicat中创建一张表。
创建一个SpringBoot工程,在pom文件中添加所需依赖。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> <version>5.1.9</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.0.5</version> </dependency>
编写数据库中student表对应的实体类。
package com.szh.mybatisplus.entity; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; /** * */ @TableName(value = "student") public class Student { @TableId(value = "id",type = IdType.AUTO) private Integer id; @TableField(value = "name") private String name; @TableField(value = "age") private Integer age; @TableField(value = "email") private String email; @TableField(value = "status") private Integer status; //getter and setter //toString }
编写mapper接口。
之前几篇文章中,我们的mapper接口中没有任何方法,是因为继承了MP框架中的BaseMapper模板类,可以直接使用BaseMapper中的CRUD操作方法。
那么现在我们定义的mapper接口仍然让它继承MP框架中的BaseMapper模板类,但是我对StudentMapper接口做一个扩展,添加我们自定义的CRUD操作方法。
package com.szh.mybatisplus.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.szh.mybatisplus.entity.Student; import org.apache.ibatis.annotations.Param; import java.util.List; /** * */ public interface StudentMapper extends BaseMapper<Student> { //自定义方法 int insertStudent(Student student); Student selectStudentById(@Param("id") Integer id); List<Student> selectAll(); }
因为我们在mapper接口中自定义了CRUD方法,所以我们需要自行编写它对应的mapper映射文件。
在SpringBoot项目中,mapper映射文件有两种存放位置(一种是放在src/main/java目录下;另一种是放在src/main/resources/mapper目录下),我这里存放在第二种情况的位置中,则需要在核心配置文件中指定mybatis-plus映射文件的类路径。
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.szh.mybatisplus.mapper.StudentMapper"> <!-- 使用insert、update、delete、select标签编写sql语句 --> <insert id="insertStudent"> insert into student(name,age,email,status) values(#{name},#{age},#{email},#{status}) </insert> <select id="selectStudentById" resultType="com.szh.mybatisplus.entity.Student"> select id,name,age,email,status from student where id=#{id} </select> <select id="selectAll" resultType="com.szh.mybatisplus.entity.Student"> select id,name,age,email,status from student </select> </mapper>
#配置数据库的相关连接信息 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/springdb?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8 spring.datasource.username=root spring.datasource.password=12345678 #配置对应的日志信息 mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl #指定MyBatis-Plus映射文件的路径 mybatis-plus.mapper-locations=classpath:mapper/*.xml
下面是一系列测试方法。首先使用@Resource注解将StudentMapper注入到Spring容器中。
然后我们分别测试在StudentMapper接口中定义的那三个方法。
@Resource private StudentMapper mapper;
@Test public void testInsert() { Student student=new Student(); student.setName("张三"); student.setAge(30); student.setEmail("zs@163.com"); student.setStatus(1); int rows=mapper.insertStudent(student); System.out.println("insert的结果:"+ rows); }
@Test public void testSelect() { Student student=mapper.selectStudentById(1); System.out.println("select的结果:" + student); }
@Test public void testSelect2() { List<Student> studentList=mapper.selectAll(); studentList.forEach(student -> System.out.println(student)); }