SpringBoot_Mybatis MyBatisPlus

A .SpringBoot used Mybatis

springBoot used mybatis as before spring in use.

1.mybatis configuration:

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/?useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password: ******
#mybatis Configuration 
# configure an alias package entity classes of
the mybatis:
of the type-aliases-Package Penalty for: com.lanou.spring_bootm.entity
position ## mapper file is located
mapper-locations: classpath: mapper / * mapper.xml

2. Configure the mapper interface.

package com.lanou.spring_bootm.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lanou.spring_bootm.entity.Emp;
import java.util.List;
public interface EmpMapper extends BaseMapper { List<Emp> findAll(); }

3. Configure the mapper file.

Create a mapper package in the resources folder

Creating mapper.xml in mapper package to write sql statement.

<?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.lanou.spring_bootm.mapper.EmpMapper">
    <select id="findAll" resultType="emp">
        select uuid,username,birthday,email from emp
    </select>
</mapper>

  

Two .MybatisPlus

mybatisPlus is an extension of mybatis.

Use mybatisplus directly to the configuration file mybatis changed mybatisplus

PLUS-the mybatis: 
  of the type-aliases-Package Penalty for: com.lanou.spring_bootm.entity 
  position ## mapper file is located 
  mapper-locations: classpath: mapper / * mapper.xml

mybatis-plus in a lot better define the database simple CRUD and paging capabilities

Usage is very simple, in the mapper interfaces inherited from BaseMapper interfaces,

The method of using the interface does not require mapper.xml file,

com.lanou.spring_bootm.mapper Package; 

Import com.baomidou.mybatisplus.core.mapper.BaseMapper; 
Import com.lanou.spring_bootm.entity.Emp; 
Import java.util.List; // BaseMapper is mybatisplus Interface 
public interface the extends BaseMapper {EmpMapper 
    // custom query method, corresponding to the xml ID 
    List <Emp> the findAll (); 
}

Paging function mybatis-plus in the Bean needs to be configured in the config file

// configured using the java configuration in springBoot customary 
// SSM project can also be configured java way // this comment is equivalent to the class configured profiles. 
@Configuration 
public class MyBatisConfig { 
    // configuration in MybatisPlus tab intercept 
    @Bean 
    public PaginationInterceptor paginationInterceptor () { 
        return new new PaginationInterceptor (); 
    } 
}

  

Guess you like

Origin www.cnblogs.com/zhouchangyang/p/11125327.html