简单说下,MyBatis-plus为我们提供了默认的增删改查和一些基础使用的条件查询等。
直接进入代码环节,
pom.xml 的核心依赖包:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- mybatis-plus--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- druid数据源驱动 1.1.10解决springboot从1.0——2.0版本问题--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency> <!-- lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.10</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
application.yml:
spring: datasource: druid: username: root password: root url: jdbc:mysql://localhost:3306/mylocal?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&zeroDateTimeBehavior=convertToNull # driver-class-name: com.mysql.jdbc.Driver initialSize: 5 minIdle: 5 maxActive: 20 maxWait: 60000 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true maxPoolPreparedStatementPerConnectionSize: 20 useGlobalDataSourceStat: true connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 #配置端口 server: port: 8037
接着直接到了使用mybatis-plus (其实跟普通的mybatis使用没什么区别,只是少写一些方法。)
POJO
MyInfo.java:
PS: 这里的主键设置自增需要注意
import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import lombok.Data; /** * @Author : JCccc * @CreateTime : 2019/12/4 * @Description : **/ @Data public class MyInfo { @TableId(value = "id",type = IdType.AUTO) private Integer id; private Integer num; private String name; private Integer age; }
Mapper
MyInfoMapper.java:
PS:可以看到我只是额外写了一个根据条件查询(模拟实际的业务查询),增删查改都没写,因为BaseMapper都为我们提供了这些。
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.demo.elegant.pojo.MyInfo; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; /** * @Author : JCccc * @CreateTime : 2019/12/4 * @Description : **/ @Mapper public interface MyInfoMapper extends BaseMapper<MyInfo> { @Select("SELECT * FROM my_info where id=#{id} and name=#{name}") MyInfo getMyInfoConditional(@Param("id") Integer id,@Param("name") String name); }
BaseMapper 默认提供的方法:
这里为了演示简单的使用,我就不写service和serviceImpl了。
直接开始接口测试:
MyInfoController.java:
import com.demo.elegant.mapper.MyInfoMapper; import com.demo.elegant.pojo.MyInfo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; /** * @Author : JCccc * @CreateTime : 2019/12/4 * @Description : **/ @RestController public class MyInfoController { @Autowired MyInfoMapper myInfoMapper; @GetMapping("/testPlus") public void testPlus() { MyInfo myInfo1 = new MyInfo(); myInfo1.setNum(1011909); myInfo1.setName("one"); myInfo1.setAge(11); int effectNum1 = myInfoMapper.insert(myInfo1); System.out.println("添加后的影响行数:"+effectNum1+"--- 1主键:"+myInfo1.getId()); MyInfo myInfo2=new MyInfo(); myInfo2.setNum(1011220); myInfo2.setName("two"); myInfo2.setAge(30); int effectNum2 = myInfoMapper.insert(myInfo2); System.out.println("添加后的影响行数:"+effectNum2+"--- 2主键:"+myInfo2.getId()); List<MyInfo> myInfoList = myInfoMapper.selectList(null); System.out.println("查询出来的list:"+myInfoList.toString()); // int effectNumeDelete = myInfoMapper.deleteById(2); // System.out.println("删除后影响行数:"+effectNumeDelete); MyInfo myInfoConditional = myInfoMapper.getMyInfoConditional(2, "two"); System.out.println("根据条件查询出来的数据:"+myInfoConditional.toString()); } }
OK,使用postman调用接口看看效果:
看下控制台打印情况,都是正常使用:
使用mybatis-plus 结合mybatis的注解方式,是不是感觉操作数据库变得特别的精简,基本不需要任何过多的配置,拿来即用。
到此。