MyBatis-Plus

1.简介

MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生

官网:https://mybatis.plus/

2.搭建环境

2.1pom.xml配置

<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>3.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>5.1.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.21</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
            <version>5.1.47</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.1.8</version>
        </dependency>


2.2spring配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- dataSource -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/mybatis??characterEncoding=utf8"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
    </bean>
    <!--配置工厂  -->
    <bean id="sqlSessionFactoryBean" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <!-- sql映射文件路径 -->
<!--        <property name="mapperLocations" value="classpath:com/qianfeng/openapi/web/master/mapper/*.xml"></property>-->
       <property name="configLocation" value="classpath:mybatis/mybatis.xml"/>
    </bean>
    <!-- 配置扫描mapper文件 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.qf.mapper"></property>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"/>
    </bean>
</beans>



2.3mybatis配置

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <!-- 打印查询语句 -->
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>
    <plugins>
        <!--配置分页插件-->
        <plugin interceptor="com.github.pagehelper.PageInterceptor">
            <property name="helperDialect" value="mysql"/>
        </plugin>

    </plugins>
</configuration>



实体配置

//@TableName 注解,帮助mybatis与数据库表关联
@TableName("t_departments")
public class Department {
    @TableId(value = "id",type = IdType.AUTO)//指定自增策略
    private Integer id;
    private String name;
    private String location;
}


3.案例

3.1插入

    /**
     * 测试,mybatisplus的插入功能
     */
    @Test
    public void testInsert() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
        DepartmentMapper departmentMapper = applicationContext.getBean("departmentMapper", DepartmentMapper.class);
        //调用mybatisplus的插入方法
        departmentMapper.insert(new Department(null, "人事部", "杭州"));
    }


3.2修改

/**
     * 根据 ID 修改
     */
    @Test
    public void testupdateById() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
        DepartmentMapper departmentMapper = applicationContext.getBean("departmentMapper", DepartmentMapper.class);
        Department d = new Department();
        d.setId(15);
        d.setName("abc");
        d.setLocation("abc");
        departmentMapper.updateById(d);
    }

    /**
     * 根据 whereEntity 条件,更新记录
     *
     * 实体对象 (set 条件值,可以为 null)
     * 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句)
     */
    @Test
    public void testupdate() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
        DepartmentMapper departmentMapper = applicationContext.getBean("departmentMapper", DepartmentMapper.class);
        Department d = new Department();
        d.setId(16);
        d.setName("123");
        d.setLocation("123");
        departmentMapper.update(d,new UpdateWrapper<Department>()
                .eq("name","abc")
                .eq("location","abc"));

    }


3.3删除

@Test
    public void testDeleteByMap(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
        DepartmentMapper departmentMapper = applicationContext.getBean("departmentMapper", DepartmentMapper.class);
        Map map = new HashMap<>();
        map.put("id",11);
        map.put("name","人事部");
        departmentMapper.deleteByMap(map);
    }

    /**
     * 根据 entity 条件,删除记录
     *
     * 实体对象封装操作类(可以为 null)
     */
    @Test
    public void testDelete() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
        DepartmentMapper departmentMapper = applicationContext.getBean("departmentMapper", DepartmentMapper.class);
        departmentMapper.delete(new QueryWrapper<Department>().eq("id",12));
    }

    /**
     * 删除多条数据
     * 传入参数为collection
     */
    @Test
    public void testdeleteBatchIds() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
        DepartmentMapper departmentMapper = applicationContext.getBean("departmentMapper", DepartmentMapper.class);
        departmentMapper.deleteBatchIds(Arrays.asList(10,13,14));
    }


3.4查询

/**
     * 根据 ID 查询
     *
     * id 主键ID
     */
    @Test
    public void testselectById(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
        DepartmentMapper departmentMapper = applicationContext.getBean("departmentMapper", DepartmentMapper.class);
        Department department = departmentMapper.selectById(1);
        System.out.println(department);
    }

    /**
     * 查询(根据ID 批量查询)
     *
     *  主键ID列表(不能为 null 以及 empty)
     */
    @Test
    public void testselectBatchIds(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
        DepartmentMapper departmentMapper = applicationContext.getBean("departmentMapper", DepartmentMapper.class);
        List<Department> list = departmentMapper.selectBatchIds(Arrays.asList(1,2,15));
        for (Department department : list) {
            System.out.println(department);
        }
    }

    /**
     * 查询(根据 columnMap 条件)
     *
     * 表字段 map 对象
     */
    @Test
    public void testselectByMap(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
        DepartmentMapper departmentMapper = applicationContext.getBean("departmentMapper", DepartmentMapper.class);
        Map map = new HashMap();
        map.put("name","研发部");
        map.put("location","上海");
        List<Department> list = departmentMapper.selectByMap(map);
        for (Department department : list) {
            System.out.println(department);
        }
    }

    /**
     * 根据 entity 条件,查询一条记录
     * 实体对象封装操作类(可以为 null)
     */
    @Test
    public void testselectOne(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
        DepartmentMapper departmentMapper = applicationContext.getBean("departmentMapper", DepartmentMapper.class);
        Department department = departmentMapper.selectOne(new QueryWrapper<Department>().gt("id",10));
        System.out.println(department);
    }

    /**
     * 根据 Wrapper 条件,查询总记录数
     * 实体对象封装操作类(可以为 null)
     */
    @Test
    public void testselectCount(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
        DepartmentMapper departmentMapper = applicationContext.getBean("departmentMapper", DepartmentMapper.class);
        int count = departmentMapper.selectCount(null);
        System.out.println(count);
    }

    /**
     * 根据 entity 条件,查询全部记录
     *
     * 实体对象封装操作类(可以为 null)
     */
    @Test
    public void testselectList(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
        DepartmentMapper departmentMapper = applicationContext.getBean("departmentMapper", DepartmentMapper.class);
        List<Department> list = departmentMapper.selectList(null);
        for (Department department : list) {
            System.out.println(department);
        }
    }

    /**
     * 根据 Wrapper 条件,查询全部记录
     *
     *  实体对象封装操作类(可以为 null)
     */
    @Test
    public void testselectMaps(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
        DepartmentMapper departmentMapper = applicationContext.getBean("departmentMapper", DepartmentMapper.class);
        List<Map<String,Object>> list = departmentMapper.selectMaps(new QueryWrapper<Department>().ne("id",100));
        for (Map<String, Object> stringObjectMap : list) {
            System.out.println(stringObjectMap);
        }
    }

    /**
     * 根据 Wrapper 条件,查询全部记录
     * <p>注意: 只返回第一个字段的值</p>
     *
     * 实体对象封装操作类(可以为 null)
     */
    @Test
    public void testselectObjs(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
        DepartmentMapper departmentMapper = applicationContext.getBean("departmentMapper", DepartmentMapper.class);
        List<Object> list = departmentMapper.selectObjs(null);
        System.out.println(list);
    }

    /**
     * 分页查询
     */
    @Test
    public void testselectpage(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
        DepartmentMapper departmentMapper = applicationContext.getBean("departmentMapper", DepartmentMapper.class);
        PageHelper.startPage(1,2);
        List<Department> list = departmentMapper.selectList(null);
        System.out.println(list);
    }
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,271评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,275评论 2 380
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,151评论 0 336
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,550评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,553评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,559评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,924评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,580评论 0 257
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,826评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,578评论 2 320
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,661评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,363评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,940评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,926评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,156评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,872评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,391评论 2 342

推荐阅读更多精彩内容