MyBatis

一.MyBatis

1.介绍

MyBatis持久层开发框架,简化java数据库操作,类似于JdbcTemplate

2.使用

(1).pom.xml中引入(MyBatis和数据库驱动)依赖

<!--mybatis依赖-->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>3.0.3</version>
</dependency>
<!--PostgreSql驱动依赖-->
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <scope>runtime</scope>
</dependency>

(2).创建表对应的entity

@Data
@NoArgsConstructor
@AllArgsConstructor
public class MUser {
    private Long id;
    private String name;
    private Integer age;
    private String email;
}

(3).创建Mapper接口(MyBatis用于操作数据库)

一.基于注解
@Mapper
public interface MUserMapper {
    //查询全部
    List<User> selectAll();
}
二.基于xml

创建于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">
<!--namespace指定关联的接口-->
<mapper namespace="com.gok.mybatis.mapper.MUserMapper">
    <!--select语句就使用select标签,id指定关联的方法,resultType指定返回类型-->
    <select id="selectAll" resultType="com.gok.mybatis.entity.MUser">
        select * from m_user;
    </select>
</mapper>

(4).从ioc中获取UserMapper并调用方法

ConfigurableApplicationContext ioc = SpringApplication.run(MyBatisApplication.class, args);
MUserMapper mUserMapper = ioc.getBean(MUserMapper.class);
List<MUser> mUsers = mUserMapper.selectAll();

二.MyBatis-Plus

1.介绍

MyBatis-Plus是对MyBatis的封装和增强,简化MyBatis的开发

2.使用

(1).pom.xml中引入(MyBatis-Plus和数据库驱动)依赖

<!--MyBatis-Plus依赖,包含MyBatis和JDBC依赖-->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-spring-boot3-starter</artifactId>
    <version>3.5.5</version>
</dependency>
<!--PostgreSql驱动-->
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <scope>runtime</scope>
</dependency>

(2).创建表对应的entity

@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("m_user")//类名与表名不一致,要显示指定
public class MUser {
    private Long id;
    private String name;
    private Integer age;
    private String email;
}

 (3).创建Mapper接口(继承BaseMapper接口,里面有基础的增删改查,泛型要填写对应entity)

@Mapper
public interface MUserMapper extends BaseMapper<MUser> {
}

(4).从ioc中获取UserMapper并调用方法

ConfigurableApplicationContext ioc= SpringApplication.run(DatabaseMpApplication.class, args);
UserMapper userMapper= ioc.getBean(UserMapper.class);
//selectList中可以传入条件,不传检索全表数据
System.out.println(userMapper.selectList(null));

显示SQL日志:

mybatis-plus.configuration:
  log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

不把null忽略再传进sql文需要加配置:

mybatis-plus:
  global-config:
    db-config:
      update-strategy: always
      insert-strategy: always

三.通用Service

1.介绍

MyBatis-Plus除了为Mapper提供方便的BaseMapper接口,也为Service提供了ServiceImpl类直接生成Service常用的增删改查功能,但本质还是调用Mapper接口中的方法

2.编写通用Service

//ServiceImpl第一个泛型写Mapper,第二个泛型写Mapper操作的entity
@Service
public class MUserService extends ServiceImpl<MUserMapper, MUser> {
}

四.映射关系

1.介绍

MyBatis-Plus自动生成的SQL文是通过与entity的映射关系获取的

规则总结:表名/表字段名匹配entity的类名/字段名

通过@TableName指定正确表名、通过@TableField指定正确字段名、通过@TableField(exist = false),指明该字段不存在于表中

五.条件构造器

1.介绍

MyBatis-Plus使用面向对象思想将条件封装为Wrapper进行查询

最常使用QueryWrapper(字符串表示字段)和LambdaQueryWrapper(方法引用表示字段)

2.区别

//创建Wrapper对象
QueryWrapper<User> wrapper= new QueryWrapper<>();
//设置条件
wrapper.eq("name", "Jack");
//使用条件查询
userMapper.selectOne(wrapper);

QueryWrapper不足:通过字符串表示字段,没有任何约束、有记忆负担,容易出错

//创建Wrapper对象
LambdaQueryWrapper<User> wrapper= new LambdaQueryWrapper<>();
//设置条件
wrapper.eq(User::getName, "Jack");
//使用条件查询
userMapper.selectOne(wrapper);

LambdaQueryWrapper:不易出错,优先使用

3.方法介绍

(1)eq:等于查询

//当name!=null时,eq条件生效
wrapper.eq(name != null, User::getName, name);

(2)ne:不等查询

(3)gt: 大于查询

(4)ge:大于等于查询

(5)lt:  小于查询

(6)le: 小于等于查询

(7)between:范围间查询

(8)noBetween:不在范围间查询

(9)模糊查询:like likeLeft likeRight notLike

(10)判空查询:isNull isNotNull

(11)包含查询:in notIn inSql notInSql

(12)分组查询:groupBy

//与entity字段无法对应,改为Map接收
List<Map<String, Object>> res = userMapper.selectMaps(wrapper);

(13)聚合查询:having

(14)排序查询:升序orderByAsc 降序orderByDesc

(15)逻辑查询:

and:

LambdaQueryWrapper<User> wrapper= new LambdaQueryWrapper<>();
wrapper.gt(User::getAge, 22).lt(User::getAge, 30);
userMapper.selectList(wrapper);

and嵌套:

LambdaQueryWrapper<User> wrapper= new LambdaQueryWrapper<>();
wrapper.eq(User::getName, "Tom").and(w -> w.gt(User::getAge, 20).lt(User::getAge, 25));
userMapper.selectList(wrapper);

or同理

(16)自定义条件:

LambdaQueryWrapper<User> wrapper= new LambdaQueryWrapper<>();
wrapper.apply("name = 'Tom' OR (age > 20 AND age < 25)");
userMapper.selectList(wrapper);

(17)exists notExists

(18)select:

LambdaQueryWrapper<User> wrapper= new LambdaQueryWrapper<>();
wrapper.select(User::getId, User::getAge);
userMapper.selectList(wrapper);

  • 8
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值