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 的小部分