Spring Boot整合Mybatis Plus的简单使用

  1. 创建spring boot项目
    项目结构
    在这里插入图片描述

  2. 配置pom.xml文件(只列出了主要配置)

    	<!-- 数据库连接驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!-- Mybatis Plus 依赖 -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.1.0</version>
        </dependency>
        <!-- lombok插件,简化开发 -->
        <!-- 通过@Data等注解可以省略生成其属性的构造器、getter、setter、equals、hashcode、toString方法 -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    
  3. application.yml 配置文件设置Mybatis Plus相关配置

    #连接数据库的配置
    spring:
      datasource:
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&useSSL=false
        username: root
        password: root
    #如果没有xml映射文件的话,可以省略   
    #mybatis-plus:
     # mapper-locations: classpath:mapper/*Mapper.xml
    
    
  4. 创建实体类

        import lombok.Data;
        
        /**
         * @author WangBoYi
         * @date 2019/3/14 16:51
         * @Description
         */
        @Data
        public class User {
            private Integer id;
            private String username;
            private String password;
            private String Phone;
        }
    
  5. 定义mapper接口

    import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    import com.mp.demo.entity.User;
    
    /**
     * @author WangBoYi
     * @date 2019/3/14 16:54
     * @Description
     */
    public interface UserMapper extends BaseMapper<User> {
    //如果有需要,可以在此添加方法并编写相应的xml映射文件,同mybatis
    }
    

    BaseMapper < T > 该接口中封装了CRUD的基础操作方法。

  6. 创建mapper.xml映射文件(格式同mybatis,如果不需要可以省略)

  7. 在启动类中添加@MapperScan注解扫描mapper接口

    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    @MapperScan("com.mp.demo.mapper")
    public class SbmpApplication {
        public static void main(String[] args) {
            SpringApplication.run(SbmpApplication.class, args);
        }
    }
    
  8. 创建IUserService接口

    import com.mp.demo.entity.User;
    import java.util.List;
    
    /**
     * @author WangBoYi
     * @date 2019/3/14 17:04
     * @Description
     */
    
    public interface IUserService {
        /**
         * 显示用户列表
         * @return
         */
        List<User> selectAllUser();
    }
    
    
  9. 创建UserService实现IUserService接口

    import java.util.List;
    
    /**
     * @author WangBoYi
     * @date 2019/3/14 17:07
     * @Description
     */
    @Service
    public class UserServiceImpl implements IUserService {
    
        @Autowired
        private UserMapper userMapper;
    
        @Override
        public List<User> selectAllUser() {
        //selectList(null)方法调用的就是BaseMapper接口里面封装的方法
            return userMapper.selectList(null);
        }
    }
    
  10. 创建UserController类

    import com.mp.demo.entity.User;
    import com.mp.demo.service.IUserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import java.util.List;
    
    /**
     * @author WangBoYi
     * @date 2019/3/14 17:08
     * @Description
     */
    @Controller
    public class UserController {
    
        @Autowired
        IUserService iUserService;
    
        @ResponseBody
        @RequestMapping("/userList")
        public List<User> userList(){
            return iUserService.selectAllUser();
        }
    }
    
  11. 通过postman调用该接口
    在这里插入图片描述

  12. 关键步骤
    pom.xml添加依赖,application.yml添加配置,定义mapper接口继承BaseMapper,
    添加@MapperScan()注解。

  13. lombok常用注解

	@Data:注解在类上;提供类所有属性的 getting 和 setting 方法,此外还提供了equals、canEqual、hashCode、toString 方法
	 @Setter:注解在属性上:为属性提供 setting 方法, 注解再类上表示当前类中所有属性都生成setter方法
	@Getter:注解在属性上:为属性提供 getting 方法,注解再类上表示当前类中所有属性都生成getter方法
	@Log4j :注解在类上;为类提供一个 属性名为log 的 log4j 日志对象
	@NoArgsConstructor:注解在类上;为类提供一个无参的构造方法
	@AllArgsConstructor:注解在类上;为类提供一个全参的构造方法
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值