Spring整合Mybatis-Plus问题总结

121 阅读2分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第6天,点击查看活动详情

前言

由于项目的重构,想试试Mybatis-Plus带来的舒适,看了下目前整合Mybatis-Plus有好几种方法,本次只记录我自己实际操作的整合,本次整合只用application.properties配置文件、mybatis-config.xml两个配置文件(不整合log4j)。

首先就是创建项目啦,由于start.spring.io需要科学上网,本次采用阿里提供的创建链接https://start.aliyun.com,刚好有Mybatis-Plus依赖可以选择

项目结构:

pom文件如下

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.2</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

至于为啥要有lombok的依赖,不做过多的解释,释放你书写pojo类 在将依赖导入之后,就需要写application配置文件了,代码如下:

# mybatis配置
# mybatis的配置文件
mybatis-plus.config-locations=classpath:mybatis-config.xml
# resources中mapper的xml文件位置
mybatis-plus.mapper-locations=classpath:mapper/*.xml
# java项目中mapper的位置,写到mapper包就行
mybatis-plus.type-aliases-package=com.jk.mapper
# mybatis-plus驼峰命名法开启与否(true or false)
mybatis-plus.configuration.map-underscore-to-camel-case=false
# 数据库类型设置
mybatis-plus.global-config.db-config.db-type=mysql
mybatis-plus.global-config.db-config.column-underline=true
mybatis-plus.global-config.db-config.logic-delete-value=true
mybatis-plus.global-config.db-config.logic-not-delete-value=false

在配置完application.properties 到这里,Mybatis-Plus的配置就完成啦,接下里就是验证

验证

写一个基本逻辑吧

pojo层,lombok的常用作用就是这三个注解

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Admin {
    private Integer AdminId;        //管理员ID
    private String AdminName;       //管理员账号
    private String AdminPassword;   //管理员密码
    private String AdminSex;        //管理员性别
    private String AdminIphone;     //管理员手机
    private String AdminEmail;      //管理员邮箱
    private String AdminRbac;       //管理员权限
}

controller层

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    // 管理员登录方法
    @RequestMapping(method = RequestMethod.POST,value = "test")
    public Object login() throws Exception {
        try {
            return adminService.findAdminAll();
        }catch (Exception e){
            System.out.println("error:"+e.getMessage());
        }
       return "error";
    }
}

Service层

service接口

public interface AdminService {
    // 查询所有管理员用户
    List<Admin> findAdminAll() throws Exception;
}

service实现类

@Service
@Transactional
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public List<Admin> findAdminAll() throws Exception {

        List<Admin> adminList= adminMapper.findAdminAll();
        return adminList;
    }
}

mapper层

@Mapper
public interface AdminMapper {

    //查询所有
    List<Admin> findAdminAll();

}

resources下mapper的sql实现

<mapper namespace="com.jk.mapper.AdminMapper">

    <resultMap id="Admin" type="com.jk.pojo.Admin">

        <!--column的值是数据库的字段,property的值是java的pojo实体类的参数-->

        <id column="XX" property="AdminId"/>
        <id column="XX" property="AdminName"/>
        <id column="XX" property="AdminPassword"/>
        <id column="XX" property="AdminSex"/>
        <id column="XX" property="AdminIphone"/>
        <id column="XX" property="AdminEmail"/>
        <id column="XX" property="AdminRbac"/>
    </resultMap>

    <!--由于数据库的字段跟pojo的实体类不一样,所以采用映射的方式-->
    <select id="findAdminAll" resultMap="Admin">
        select * from admin
    </select>
</mapper>
```java

#### 启动类
```java
@MapperScan("com.jk.mapper")        //记得写扫描包位置
@SpringBootApplication
public class XXApplication {

    public static void main(String[] args) {
        SpringApplication.run(XXApplication.class, args);
    }

}

测试

使用postman测试,看是否能获取到自己要的数据

到此就彻底完成了Mybatis-Plus的整合

此贴记录帖,非教程帖,如有不妥之处,还望告知,先行谢过!!!