Springboot整合mybatis-plus(mybatis增强版)

一、 ORM思想

对象关系映射(英语:Object Relational Mapping,简称ORM,或O/RM,或O/R mapping),是一种程序设计技术,用于实现面向对象编程语言里不同类型系统的数据之间的转换。从效果上说,它其实是创建了一个可在编程语言里使用的“虚拟对象数据库”。
实质: 是以对象的方式操作数据库.

1.MybatisPlus

MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
特性:太多不解释一堆语言繁杂。

二、 MybatisPlus 入门案例

1.在pom.xml中导入jar包

<!--
			spring整合mybatisplus
			只保留mybatisplus的包,删除mybatis的包
		-->
		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-boot-starter</artifactId>
			<version>3.2.0</version>
		</dependency>

2.编辑POJO对象

在这里插入图片描述

@Data     //get/set/toString/equals
@Accessors(chain = true)    //实现链式加载
@AllArgsConstructor         //全参构造
@NoArgsConstructor          //无参构造
@TableName("user")   //这里表示关联表名
public class User {
    //注意对象使用包装类型
    //要对象与数据表完成映射,对象名称与表名以及对象的属性与表(数据表)的字段
    //我们可以使用如下注解
    @TableId(type= IdType.AUTO)  //设置主键自增
    private Integer id;   //id默认值0 null
    //@TableField(value ="name")  这里是可以省略,省略时名称必须相同
    private String name;
    //@TableField(value="age")
    private Integer age;
    private String sex;

}

3.继承公共接口

@Mapper
public interface UserMapper extends BaseMapper<User> {

    List<User> findAll(); //查询全部的用户记录
}

这里继承了BaseMapper<>尖括号里面是某对象,继承了BaseMapper之后,就不用自己手写sql增删查语句,但是复杂sql手写比较好,不太使用复杂sql语句。

4.编辑YML配置文件!

server:
  port: 8090
  servlet:
    context-path: /
spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/jtdb?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
    username: root
    password: root

#springboot整合mybatis-plus
mybatis-plus:
  #定义别名包
  type-aliases-package: com.jt.pojo
  #加载Mapper映射文件
  mapper-locations: classpath:/mybatis/mappers/*.xml
  #开启驼峰映射
  configuration:
    map-underscore-to-camel-case: true

 # 添加日志信息
logging:
  level:
    com.jt.mapper: debug

注意mybatis-plus与pom.xml中依赖的对应,否则出现绑定异常现象。

4.UserMapper.xml配置

<?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.jt.mapper.UserMapper">
	<!--
		1.通过别名包定义用户的类型,
			原理:当返回值结果resultType封装对象时,
				自动的拼接别名包路径 com.jt.pojo.User

		2.开启驼峰规则的说明
			字段:   user_id,user_name,user_age
			属性:	userId ,userName ,userAge

		设定:开启驼峰映射规则
		  	user_id 去除多余的_  之后首字母大写  userId

		注意事项: 如果开启了驼峰映射规则,则按照要求实现.
	-->
	<select id="findAll" resultType="User">
		select * from user
	</select>
</mapper>

5.测试

package com.jt;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
class SpringbootDemo2ApplicationTests {

    @Autowired
    private UserMapper userMapper;

    @Test
    public void test01(){
        List<User> userList = userMapper.findAll();
        System.out.println(userList);
    }

    @Test
    public void testInsert(){
        User user=new User();
        user.setName("齐田大神").setAge(1000).setSex("男");
        userMapper.insert(user);
    }

    /**
     * 测试1:  查询id=5的数据
     * 测试2:  根据name="唐僧"查询数据
     */

    @Test
    public void testSelect(){
//        User user=userMapper.selectById(5);
//        System.out.println(user);
//
//        //查询所有数据
//        List<User> userList = userMapper.selectList(null);
//        System.out.println(userList);

        //3.查询name="唐僧"数据 where name ="xxx"
        //常见关系运算符 = eq , > gt ,< lt, >= ge ,<= le

        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("name","唐僧");
        List<User> userList2=userMapper.selectList(queryWrapper);//这里会创建
                                                             //where条件构造器
       
        System.out.println(userList2);

    }
    /**
     * 条件: 查询名字中包含'精'字的数据
     * sql:  select ..... where name like '%精%'
     * 									   '王%'
     */


    @Test
    public void testSlect03(){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.like("name","精");
        System.out.println(userMapper.selectList(queryWrapper));

        //2.模糊查询2



        QueryWrapper<User> queryWrapper2=new QueryWrapper<>();
        queryWrapper2.likeRight("name", "王");
        System.out.println(userMapper.selectList(queryWrapper2));
    }

    /**
     *
     * 将数据按照age降序排列,如果年龄相同 按照ID降序排列
     */

    @Test
    public void testSelect04(){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.orderByDesc("age","id");
        System.out.println(userMapper.selectList(queryWrapper));
    }

    /**
     * 查询name中包含"君"字 or sex为女性 ,按照Id降序排列
     * sql: where name like "%君%" and xxxx
     */

    @Test
    public void testSelect05(){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", "君")
                .or()	//默认是and 如果需要调用or
                .eq("sex", "女")
                .orderByDesc("id");
        System.out.println(userMapper.selectList(queryWrapper));

    }



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值