一.SpringBoot中使用Mybatis

springBoot中使用mybatis跟以前spring中使用方法一样.

1.mybatis配置:

spring:
datasource:
url: jdbc:mysql://localhost:3306/?useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: ******
#mybatis配置
#需要配置别名的实体类的包
mybatis:
type-aliases-package: com.lanou.spring_bootm.entity
##mapper文件所在的位置
mapper-locations: classpath:mapper/*mapper.xml

2.配置mapper接口.

package com.lanou.spring_bootm.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lanou.spring_bootm.entity.Emp;
import java.util.List;
public interface EmpMapper extends BaseMapper {
List<Emp> findAll(); }

3.配置mapper文件.

在resources文件夹下创建mapper包

在mapper包中创建mapper.xml来写sql语句.

<?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_bootm.mapper.EmpMapper">
<select id="findAll" resultType="emp">
select uuid,username,birthday,email from emp
</select>
</mapper>

  

二.MybatisPlus

mybatisPlus是对mybatis的一个扩展.

使用mybatisplus直接将配置文件中mybatis改为mybatis-plus

mybatis-plus:
type-aliases-package: com.lanou.spring_bootm.entity
##mapper文件所在的位置
mapper-locations: classpath:mapper/*mapper.xml

mybatis-plus中定义好了很多对数据库简单的增删改查,和分页功能

用法很简单,将mapper中的接口继承于BaseMapper接口,

使用该接口中的方法不需要经过mapper.xml文件,

package com.lanou.spring_bootm.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lanou.spring_bootm.entity.Emp;
import java.util.List;

//BaseMapper是mybatisplus中的接口
public interface EmpMapper extends BaseMapper {
//自定义的查询方法,对应xml中的id
List<Emp> findAll(); }

mybatis-plus中的分页功能需要要在config文件中配置Bean

//在springBoot中习惯使用java的配置方式进行配置
//SSM项目中也能用java方式进行配置

//这个注解相当于给这个类配置成配置文件.
@Configuration
public class MyBatisConfig { //配置MybatisPlus中的分页拦截
@Bean
public PaginationInterceptor paginationInterceptor(){
return new PaginationInterceptor();
} }

  

最新文章

  1. 爱上MVC系列~前端验证与后端数据有效性验证
  2. [Spring] - 读写分离
  3. java基本加密算法
  4. BZOJ 1266 上学路线route(最小割)
  5. BZOJ 2337 XOR和路径(高斯消元)
  6. java学习笔记_GUI(3)
  7. 定制属于自己的Chrome起始页
  8. HTML之学习笔记(七)列表
  9. excel导出、导入功能
  10. UVA 322 ships (POJ 1138)
  11. [poj2185]Milking Grid_KMP
  12. 分布式定时任务框架——python定时任务框架APScheduler扩展
  13. table表格固定前几列,其余的滚动
  14. 【Linux】gdb调试
  15. Linux内核读书笔记第六周
  16. 【leetcode】58-LengthofLastWord
  17. Java内存泄漏简单说明
  18. mysql mysqld.sock文件丢失问题
  19. JVM中OutOFMemory和StackOverflowError异常代码
  20. RBAC 介绍 (权限)

热门文章

  1. Java-Class-C:org.springframework.http.MediaType
  2. hexo next主题深度优化(六),使用hexo-neat插件压缩页面,大幅度提升页面性能和响应速度。
  3. AsyncAwait 学习
  4. pandas 使用dataframe 索引项相同时出现bug
  5. code rain???
  6. Python自学:第五章 列表解析
  7. [JZOJ3235] 数字八
  8. agc38C LCMs
  9. goconvey测试模块
  10. 关于web前端网站优化