springboot篇】五. springboot与mybatis-plus整合

springboot与mybatis-plus整合

项目准备
1. 新建项目day63-springboot-03-mybatisplus
2. 点war包
3. 依赖

springboot篇】五. springboot与mybatis-plus整合_第1张图片

4. 将base项目下载到自己项目中,然后打成jar包放在自己的maven仓库中
<dependency>
	<groupId>com.wpjgroupId>
	<artifactId>baseartifactId>
	<version>1.0-SNAPSHOTversion>
dependency>
5. 导入mybatis-plus的依赖
<dependency>
	<groupId>com.baomidougroupId>
	<artifactId>mybatis-plus-boot-starterartifactId>
	<version>2.3version>
dependency>
6 设置mysql依赖版本
<dependency>
	<groupId>mysqlgroupId>
	<artifactId>mysql-connector-javaartifactId>
	<scope>runtimescope>
	<version>5.1.44version>
dependency>
7. 创建数据库day63-springboot-mybatisplus
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) DEFAULT NULL,
  `pwd` varchar(20) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  `sex` int(1) DEFAULT NULL COMMENT '0 男 1 女',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

INSERT INTO `user` VALUES ('1', 'admin', 'admin', '22', '0');
INSERT INTO `user` VALUES ('2', 'wpj', 'wpj', '22', '1');
8. 定义实体类User
package com.wpj.bean;

import com.baomidou.mybatisplus.annotations.*;
import com.baomidou.mybatisplus.enums.*;
import lombok.*;

/**
 * ClassName: User
 * Description:
 *
 * @author JieKaMi
 * @version 1.0
 * @date: 2020\1\19 0019 20:18
 * @since JDK 1.8
 */
@Data
@TableName("user")
public class User {

    @TableId(type= IdType.AUTO)
    private Integer id;

    private String name;

    private String pwd;

    private Integer age;
    
    private Integer sex; // 0男 1女
}
9. 定义Mapper
package com.wpj.mapper;

import com.baomidou.mybatisplus.mapper.*;
import com.wpj.bean.*;

/**
 * ClassName: IUserMapper
 * Description:
 *
 * @author JieKaMi
 * @version 1.0
 * @date: 2020\1\19 0019 20:23
 * @since JDK 1.8
 */
public interface IUserMapper extends BaseMapper<User> {

}
10. 定义service以及实现类
package com.wpj.service;

import com.wpj.baseservice.*;
import com.wpj.bean.*;

/**
 * ClassName: IUserService
 * Description:
 *
 * @author JieKaMi
 * @version 1.0
 * @date: 2020\1\19 0019 20:21
 * @since JDK 1.8
 */
public interface IUserService extends IBaseService<User> {
    
}
package com.wpj.service.impl;

import com.baomidou.mybatisplus.mapper.*;
import com.wpj.baseservice.impl.*;
import com.wpj.bean.*;
import com.wpj.service.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.stereotype.*;

/**
 * ClassName: UserServiceImpl
 * Description:
 *
 * @author JieKaMi
 * @version 1.0
 * @date: 2020\1\19 0019 20:22
 * @since JDK 1.8
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    
    @Autowired
    private IUserMapper iUserMapper;
    
    @Override
    protected BaseMapper<User> getBaseMapper() {
        return null;
    }
}
11. 写application.yml
# 配置数据源
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/day63-springboot-mybatisplus
    username: root
    password: 123456
    
# 设置com.wpj.mapper日志级别为debug
logging:
  level:
    com.wpj.mapper: debug

# mybatis-plus设置
mybatis-plus:
  type-aliases-package: com.wpj.bean # 设置别名
12. 配置包扫描
package com.wpj.day63springboot03mybatisplus;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(scanBasePackages = "com.wpj")
@MapperScan(basePackages = "com.wpj.mapper")
public class Day63Springboot03MybatisplusApplication {

	public static void main(String[] args) {
		SpringApplication.run(Day63Springboot03MybatisplusApplication.class, args);
	}
}
13. 测试环境

springboot篇】五. springboot与mybatis-plus整合_第2张图片

1 模拟登陆功能

1.1 mapper定义login方法

public interface IUserMapper extends BaseMapper<User> {
    User login(@Param("name") String username, @Param("pwd") String password);
}

1.2 service定义login方法,impl重写该方法

public interface IUserService extends IBaseService<User> {
    User login(String username, String password);
}
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private IUserMapper iUserMapper;
    @Override
    protected BaseMapper<User> getBaseMapper() {
        return iUserMapper ;
    }
    @Override
    public User login(String username, String password) {
        return iUserMapper.login(username,password);
    }
}

1.3 新建mapper对应映射文件




<mapper namespace="com.wpj.mapper.IUserMapper">

    <select id="login" resultType="user">
        select * from user where name = #{name} and pwd = #{pwd}
    select>

mapper>

springboot篇】五. springboot与mybatis-plus整合_第3张图片

1.4 扫描mapper文件

# mybatis-plus设置
mybatis-plus:
  type-aliases-package: com.wpj.bean # 设置别名
  mapper-locations: classpath:mapper/*.xml # mapper扫描

1.5 测试功能

springboot篇】五. springboot与mybatis-plus整合_第4张图片

2. 设置分页sql

2.1 定义配置类给spring容器添加分页插件

package com.wpj.config;

import com.baomidou.mybatisplus.plugins.*;
import org.springframework.context.annotation.*;

/**
 * ClassName: PageConfig
 * Description:
 *
 * @author JieKaMi
 * @version 1.0
 * @date: 2020\1\19 0019 20:56
 * @since JDK 1.8
 */
@Configuration
public class PageConfig {
    // spring容器添加分页插件
    @Bean
    public PaginationInterceptor paginationInterceptor(){
        return new PaginationInterceptor();
    }
    
}

2.2 测试功能

springboot篇】五. springboot与mybatis-plus整合_第5张图片

你可能感兴趣的:(springboot,spring,boot,mybatisPlus,mybatisplus,mybatis-plus)