spring boot 导包
java
配置文件的后缀 改为 ymlmysql
spring: datasource: url: jdbc:mysql://localhost:3306/****?useSSL=false&serverTimezone=Asia/Shanghai username: ***** password: ****** #myBatis #须要配置别名的实体类的包 #mybatis-plu: mybatis-plus: type-aliases-package: com.lanou.spring_boot_mybatis.entity #mapper文件的位置 mapper-locations: classpath:mapper/*Mapper.xml # 打印debug日志 debug: true
在pom.xml 中 导入 mybatis-plusspring
<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.1.1</version> </dependency>
建立实体类 Empsql
package com.lanou.spring_boot_mybatis.entity; import lombok.Data; import java.util.Date; @Data public class Emp { private Long uuid; private String username; private Date birthday; private String email; }
*若是要在spring boot 中写XML文件就 向下面这样作springboot
建立 EmpMappers 继承 MyBatis-plus 的BaseMappers<> 接口mybatis
package com.lanou.spring_boot_mybatis.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.lanou.spring_boot_mybatis.entity.Emp; import java.util.List; //BaseMapper<> 属于baomidou.mybatisplus sql语句能够不用本身来手写 public interface EmpMapper extends BaseMapper<Emp> { List<Emp> findAll(); }
在resources 下建立mapper 包 里面写 xml 文件实现sql语句app
<?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.lanou.spring_boot_mybatis.mapper.EmpMapper"> <select id="findAll" resultType="emp"> select uuid, username,birthday,email from emp </select> </mapper>
在 建立项目时自动生成的实体类中写注解使项目能扫描mapper 接口下的全部包测试
package com.lanou.spring_boot_mybatis; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication //扫描mapper接口所在的包 @MapperScan("com.lanou.spring_boot_mybatis.mapper") public class SpringBootMybatisApplication { public static void main(String[] args) { SpringApplication.run(SpringBootMybatisApplication.class, args); } }
*使用MyBatis-pius 写的SQL语句( 他写的SQL语句只是单表联查,没有多表联查)ui
在测试类中测试SQL语句this
注入 EmpMapper @Resource private EmpMapper empMapper; @Test public void contextLoads() { //查询全部(使用的是方法MyBatis-pius 中的) List<Emp> all = this.empMapper.selectList(null); all.forEach(emp -> log.info("{}", emp)); }
建立MyBatisConfig实体类 来配置 关于MyBatis 的Bean
package com.lanou.spring_boot_mybatis.config; import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; //在springboot中,习惯使用java的形式进行配置 //在之前ssm项目中也可以使用java的形式进行配置 //SSM中也能够集成Mybatis-plus @Configuration public class MyBatisConfig { // Mybatis-plus 中使用分页所作的拦截 @Bean public PaginationInterceptor pagintaionInterceptor(){ return new PaginationInterceptor(); } }
使用MyBatis-pius 中分页方法
// 实现分页 @Test public void findByPage() { Page<Emp> page = new Page<>(2, 3); // 第一个参数写对象, // 第二个参数写空的QueryWrapper 对象 或null // null:查询全部 // QueryWrapper 对象 查询分页 IPage<Emp> empIPage = this.empMapper.selectPage(page, new QueryWrapper<>()); log.info("分页数据总条数:{},集合{}", empIPage.getTotal(), empIPage.getRecords()); }
这只是其中一小部分,上一篇也有关于MyBatis 的小部分