跳转至

MyBatis-Plus框架支持

MyBatis-Plus框架支持

本文档描述了 SeaboxSQL 数据库对于MyBatis-Plus框架的支持情况和配置说明,面向所有使用SeaboxSQL数据库的用户,主要是数据库管理员和应用程序开发人员。

有关 MyBatis-Plus 的更多信息,请参阅以下资源:

MyBatis-Plus 官方网站https://baomidou.com/

概述

MyBatis-Plus 是一个 Mybatis 的增强工具,在 Mybatis 的基础上只做增强不做修改。

MyBatis-Plus 配置说明

Mybatis-Plus的jar包可以从官方网站下载,Mybatis-Plus所使用的SeaboxSQL JDBC驱动包seaboxsql-jdbc-xxx.jar可以从公司获取,使用时将Mybatis包和 JDBC包导入到项目的Libraries中并定义相关配置项即可。

Mybatis-Plus使用原理图

定义 Mybatis-Plus 配置文件,根据用户选择,更改一下配置文件。以 SpringMVC 工程整合 Mybatis-Plus3.x 为例:,配置 spring 配置文件:

配置 MapperScan:

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.baomidou.mybatisplus
    .Mybatis_plus_Sample.mapper" />
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>

调整 SqlSessionFactory 为 Mybatis-Plus 的 SqlSessionFactory:

<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension
.spring.MybatisSqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="typeAliasesPackage" value="com.baomidou.mybatisplus
    .Mybatis_plus_Sample.entity" />
    <property name="plugins">
        <array>
        <!-- 分页插件配置 -->
        <bean id="paginationInterceptor"
        class="com.baomidou.mybatisplus.extension.plugins
        .PaginationInterceptor">
            <property name="dialectType" value="postgresql" />
        </bean>
        </array>
    </property>
</bean>

MyBatis-Plus 程序示例

  • 配置实体类:
@TableName(value = "tb_employee") /* 指定表名 */
public class Employee {
    /* value 与数据库主键列名一致,若实体类属性名与表主键列名一致可省略 value */
    @TableId(value = "id", type = IdType.AUTO) /* 指定自增策略 */
    private Integer id;
    /* 若没有开启驼峰命名,或者表中列名不符合驼峰规则,可通过该注解 */
    /* 指定数据库表中的列名,exist 标明数据表中有没有对应列 */
    @TableField(value = "last_name", exist = true)
    private String lastName;
    private String email;
    private boolean gender;
    private Integer age;
    private Date date;

    public Integer getId() {
    	return id;
    }

    public void setId(Integer id) {
    	this.id = id;
    }

    public String getLastName() {
    	return lastName;
    }

    public void setLastName(String lastName) {
    	this.lastName = lastName;
    }

    public String getEmail() {
    	return email;
    }
    public void setEmail(String email) {
    	this.email = email;
    }

    public boolean isGender() {
    	return gender;
    }

    public void setGender(boolean gender) {
    	this.gender = gender;
    }

    public Integer getAge() {
    	return age;
    }

    public void setAge(Integer age) {
    	this.age = age;
    }

    public Date getDate() {
    	return date;
    }

    public void setDate(Date date) {
    	this.date = date;
    }

    @Override
    public String toString() {
    	return "Employee [id=" + id + ", lastName=" + lastName + ",
    	email=" + email + ", gender=" + gender +", age=" + age + ",
    	date=" + date + "]";
    }
}
  • 定义 Mapper 接口
@Mapper
public interface EmplopyeeDao extends BaseMapper {
}
  • 在 Java 中进行表的增删改查操作:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "classpath:spring/spring-dao.xml" })
public class test {
    @Autowired
    private DataSource dataSource;
    
    @Test
    public void testDataSource() throws SQLException {
    	System.out.println(dataSource.getConnection());
    }
    
    @Autowired
    private EmplopyeeDao emplopyeeDao;
    
    @Test
    public void testInsert() {
        Employee employee = new Employee();
        employee.setLastName("tom");
        employee.setEmail("dfbb@163.com");
        employee.setGender(false);
        employee.setAge(22);
        employee.setDate(new Date(System.currentTimeMillis()));
        emplopyeeDao.insert(employee);
        /* mybatisplus 会自动把当前插入对象在数据库中的 id 写回到该实体中 */
        System.out.println(employee.getId());
    }
    @Test
    public void testUpdate() {
        Employee employee = new Employee();
        employee.setId(1);
        employee.setLastName(" 更新测试");
        /* 根据 id 进行更新,没有传值的属性就不会更新 */
        emplopyeeDao.updateById(employee);
        /* 根据 id 进行更新,没传值的属性就更新为 null */
        emplopyeeDao.update(employee, null);
    }
    
    @Test
    public void testSelectById() {
        Employee employee = emplopyeeDao.selectById(2);
        System.out.println(employee.toString());
    }
    
    @Test
    public void testSelect() {
        Map columnMap = new HashMap();
        /* 写表中的列名 */
        columnMap.put("last_name", " 更新测试");
        columnMap.put("gender", false);
        List employees = emplopyeeDao.selectByMap(columnMap);
        System.out.println(employees.size());
    }
    
    @Test
    public void testSelectByPage() {
        IPage employees = emplopyeeDao.selectPage(new Page(1, 5),
        new QueryWrapper().between("age", 18, 50).eq("gender",
        false).eq("last_name", " 更新测试"));
        System.out.println(employees);
    }
    
    @Test
    public void testDeleteById() {
    	System.out.println(emplopyeeDao.deleteById(1));
    }
    
    @Test
    public void testDelete() {
        Map columnMap = new HashMap();
        columnMap.put("gender", false);
        columnMap.put("age", 18);
        emplopyeeDao.deleteByMap(columnMap);
    }
    
    @Test
    public void testSelectByPage1() {
        Integer[] ints = { 1, 18, 19, 20, 21, 22, 23, 24, 25 };
        IPage employees = emplopyeeDao.selectPage(new Page(1, 5),
            new QueryWrapper().between("age", 18, 50).eq("gender",false).eq("last_name", " 更新测试").in(true,"age", Arrays.asList(ints)));
        System.out.println(employees.toString());
        System.out.println(employees.getTotal());
        System.out.println(employees.getPages());
        System.out.println(employees.getRecords());
    }
}

MyBatis-Plus 注意点

SeaboxSQL适配 Mybatis-Plus 时,由于 Mybatis-Plus 无法识别 SeaboxSQL 数据库类型,故在相关配置中需 将其配置成 postgresql,如:在使用分页插件时,配置方言类型为 postgresql

  • 示例:
<property name="plugins">
    <array>
        <!-- 分页插件配置 -->
        <bean id="paginationInterceptor" class="com.baomidou.mybatisplus
        .extension.plugins.PaginationInterceptor">
            <property name="dialectType" value="postgresql" />
        </bean>
    </array>
</property>