其他分享
首页 > 其他分享> > MyBatis-Plus

MyBatis-Plus

作者:互联网

MyBatis-Plus

官网:https://mybatis.plus/

ORM(hibernate、jpa、mybatis)

MyBatisPlus:只做增强 不做改变

什么是Mybatis Plus?

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

特性

这是官方的原话

支持数据库

本次项目:maven+spring+mybatis

步骤

配置

<dependencies>
  <dependency>
    <!--mybatis-plus:mybatis-plus mybatis spring-mybatis-->
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus</artifactId>
    <version>3.1.1</version>
  </dependency>
  <!--mysql驱动-->
  <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.48</version>
  </dependency>

  <!--日志-->
  <dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
  </dependency>

  <!--数据库连接池-->
  <dependency>
    <groupId>com.mchange</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.5.2</version>
  </dependency>

  <!--spring context-->
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.3.24.RELEASE</version>
  </dependency>

  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-orm</artifactId>
    <version>4.3.24.RELEASE</version>
  </dependency>

创建一张表、一个实体,一 一对应

<?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>

    <!--配置数据库信息-->
    <!--<properties resource="db.properties"/>-->
    <!--配置日志  LOG4J默认的配置文件就是 log4j.properties-->
    <settings>
        <setting name="logImpl" value="LOG4J"/>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>

</configuration>
log4j.rootLogger=DEBUG, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
<!--配置数据源 c3p0-->
<context:property-placeholder location="classpath:db.properties" />

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  <property name="driverClass" value="${driver}"></property>
  <property name="jdbcUrl" value="${url}"></property>
  <property name="user" value="${username}"></property>
  <property name="password" value="${password}"></property>
</bean>

<!--事务管理器-->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource" ref="dataSource"></property>
</bean>

<tx:annotation-driven transaction-manager="dataSourceTransactionManager" />

<!--SqlSessionFactoryBean   MyBatis-->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
  <property name="dataSource" ref="dataSource"></property>
  <!--mybatis配置文件-->
  <property name="configLocation" value="classpath:mybatis.xml"></property>
  <!-- 别名-->
  <property name="typeAliasesPackage" value="com.xingwei.entity"></property>

</bean>

<!--MyBatis在操作时,只写接口不写实现类 -->
<bean  class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  <property name="basePackage" value="com.xingwei.mapper"></property>
</bean>
<?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:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <!--引入db.properties-->
    <context:property-placeholder location="classpath:db.properties" />
    <!--配置数据源 c3p0-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <!--事务管理器-->
    <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <tx:annotation-driven transaction-manager="dataSourceTransactionManager" />

    <!-- MyBatis: SqlSessionFactoryBean <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">-->
    <!--MyBatisPlus: MybatisSqlSessionFactoryBean-->
    <bean id="sqlSessionFactoryBean" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--mybatis配置文件-->
        <property name="configLocation" value="classpath:mybatis.xml"/>
        <!-- 别名-->
        <property name="typeAliasesPackage" value="com.xingwei.entity"/>
    </bean>

    <!--MyBatis在操作时,只写接口不写实现类 -->
    <bean id="configurer"   class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.xingwei.mapper"/>
    </bean>

</beans>

开始操作 CRUD

1、创建实体类

2、创建一个Mapper接口,实现BaseMapper接口,无需编写 mapper.xml 文件,即可获得CRUD功能

package com.xingwei.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xingwei.entity.Student;
//Mapper 继承该接口后,无需编写 mapper.xml 文件,即可获得CRUD功能
public interface StudentMapper  extends BaseMapper<Student> {
}

3、编写测试类

package com.xingwei.test;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xingwei.entity.Student;
import com.xingwei.mapper.StudentMapper;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.sql.SQLException;

public class Test {
    /*增加的方法*/
    public static void testInsert(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //从Ioc容器中获取xxxMapper对象
        StudentMapper studentMapper = context.getBean("studentMapper",StudentMapper.class);
        Student student = new Student("ww",23); //增加数据  还会将数据库中的自增的主键   回写给原对象
        int count = studentMapper.insert(student);
        System.out.println(count);
        System.out.println(student);
    }
    /*删除的方法*/
    public static void testDelete(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //从Ioc容器中获取xxxMapper对象
        StudentMapper studentMapper = context.getBean("studentMapper",StudentMapper.class);
        int count = studentMapper.deleteById(2);
        System.out.println(count);
    }
    /*修改的方法*/
    public static void testUpdate(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //从Ioc容器中获取xxxMapper对象
        StudentMapper studentMapper = context.getBean("studentMapper", StudentMapper.class);
        Student student = new Student(1,"邢伟",20);
        int count = studentMapper.updateById(student);

        System.out.println(count);
    }
    
    /*查询的方法*/
    public static void testSelect(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //从Ioc容器中获取xxxMapper对象
        StudentMapper studentMapper = context.getBean("studentMapper", StudentMapper.class);
        Student student = studentMapper.selectById(1);
        System.out.println(student);
    }
    public static void main(String[] args) throws SQLException {

       //测试获取c3p0数据源连接
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        ComboPooledDataSource ds =(ComboPooledDataSource) context.getBean("dataSource");
        System.out.println("数据源============="+ds);
        Connection connection  = ds.getConnection();
        System.out.println("连接对象------------"+connection);

        testInsert();
        testUpdate();
        testSelect();
        testDelete();
    }
}

标签:xml,studentMapper,StudentMapper,ClassPathXmlApplicationContext,Plus,context,MyBa
来源: https://www.cnblogs.com/xingStudy/p/14143263.html