Spring Boot:Spring Boot配置Mybatis-Plus

1.首先添加pom文件的依赖:

这仅仅一些所需依赖,不要盲目全直接粘贴到pom文件中。

 
    
        1.1.1
        1.0.5
        2.0.7
        5.1.38
        1.1.12
    
 
 
        
            com.alibaba
            druid
            ${druid.version}
        

        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            ${mybatis-spring-boot.version}
        

        
        
            org.springframework.boot
            spring-boot-starter-jdbc
        

        
        
            mysql
            mysql-connector-java
            ${mysql-connector-java.version}
        

        
        
            com.baomidou
            mybatisplus-spring-boot-starter
            ${mybatisplus-spring-boot-starter.version}
        

        
        
            com.baomidou
            mybatis-plus
            ${mybatisplus.version}
        
 

2、修改application.yml配置文件,添加Mybatis-Plus的配置

### 启动端口号
server:
  port: 8080
### 设置数据库
spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/hrabbit_admin?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false
    username: root
    password: root
### 配置mybatisPlus
mybatis-plus:
  mapper-locations: classpath:mapper/*.xml
  typeAliasesPackage: com.hrabbit.admin.modual.system.bean

3、注入Mybatis-Plus的插件

在数据库配置类,SingleDataSourceConfig.java中添加如下代码:

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.enums.DBType;
import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import com.hrabbit.admin.config.properties.DruidProperties;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * 数据源配置
 *
 * @Auther: hrabbit
 * @Date: 2018-12-19 11:18 AM
 * @Description:
 */
@Configuration
@EnableTransactionManagement
@MapperScan({"com.hrabbit.admin.modual.system.mapper"})
public class SingleDataSourceConfig {

    /**
     * Druid配置
     */
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DruidProperties druidProperties() {
        return new DruidProperties();
    }

    /**
     * 单数据源连接池配置
     */
    @Bean
    public DruidDataSource dataSource(DruidProperties druidProperties) {
        DruidDataSource dataSource = new DruidDataSource();
        druidProperties.config(dataSource);
        return dataSource;
    }

    /**
     * mybatis-plus分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        paginationInterceptor.setDialectType(DBType.MYSQL.getDb());
        return paginationInterceptor;
    }
}

4、 修改实体Bean

添加@TableName@TableId等注解:


/**
 * 系统用户
 * @Auther: hrabbit
 * @Date: 2018-12-19 12:02 PM
 * @Description:
 */
@Data
@TableName(value = "sys_user")
public class SysUser {

    /**
     * 主键id
     */
    @TableId(type = IdType.AUTO)
    private Integer id;

    /**
     * 用户名称
     */
    private String name;

}

5、 修改SysUserMapper.java

主要继承BaseMapper类,实现一些crud

/**
 * 系统用户数据库层
 *
 * @Auther: hrabbit
 * @Date: 2018-12-19 12:02 PM
 * @Description:
 */
public interface SysUserMapper extends BaseMapper {

    /**
     * 根据id查询用户信息
     *
     * @param id
     * @return
     */
    SysUser findSysUserById(@Param("id") Long id);
}

注意 : 如果在xml 中不写SQL的,可以使用注解的方式在此接口当中直接写SQL,实体和数据库表字段不一致,使用@Result注解来映射

6、 修改SysUserService.java

主要继承 IService

/**
 * 用户业务层
 * @Auther: hrabbit
 * @Date: 2018-12-19 11:59 AM
 * @Description:
 */
public interface SysUserService extends IService {

    /**
     * 根据id获取用户信息
     * @param id
     * @return
     */
    SysUser findSysUserById(Long id);

    /**
     * 业务层的根据id获取用户信息
     * @param id
     * @return
     */
    SysUser selectById(Long id);
}

7、 修改SysUserServiceImpl.java

/**
 * 系统用户业务层
 * @Auther: hrabbit
 * @Date: 2018-12-19 12:01 PM
 * @Description:
 */
@Service
@SuppressWarnings("all")
public class SysUserServiceImpl extends ServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 根据id获取用户信息
     * @param id
     * @return
     */
    @Override
    public SysUser findSysUserById(Long id){
        return sysUserMapper.findSysUserById(id);
    }

    /**
     * 调用mybatis-plus中的根据id查询数据
     * @param id
     * @return
     */
    @Override
    public SysUser selectById(Long id){
        return sysUserMapper.selectById(id);
    }

}

8、测试的Controller

/**
 * 系统用户
 *
 * @Auther: hrabbit
 * @Date: 2018-12-17 6:21 PM
 * @Description:
 */
@Controller
@RequestMapping("user")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    /**
     * 根据id获取用户信息
     *
     * @return
     */
    @RequestMapping("/")
    @ResponseBody
    public Object index() {
        return sysUserService.selectById(1L);
    }

}

在Postman访问接口,成功获取到系统用户数据:

image.png

欢迎访问我的博客:http://www.hrabbit.xin

欢迎和大家交朋友,可以添加我的微信!

你可能感兴趣的:(Spring Boot:Spring Boot配置Mybatis-Plus)