2

很长时间没有维护这个专栏了,近来开始实行的一个"DZ"计划,计划的其中一个内容就是促使我写下去,给曾经的"出书梦"做个了结。

当初在创业公司的时候,使用MyBatis-Plus的版本还是2.x,如今已经更到了3.4.1,我的作品也应该与时俱进,于是决定全部升级为3.x,包括我自己的Blog产品。

一、导入Maven依赖

pom.xml


<properties>
    <java.version>1.8</java.version>
    <druid-spring-boot-starter.version>1.1.13</druid-spring-boot-starter.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.16.20</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.4.1</version>
    </dependency>
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-extension</artifactId>
        <version>3.4.1</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid-spring-boot-starter</artifactId>
        <version>${druid-spring-boot-starter.version}</version>
    </dependency>
    <!-- SpringBoot Web -->
 <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- Mysql Connector -->
 <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.19</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>~~~~

二、编写实体模型

Users.java


package com.blog.tutorial.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import lombok.Data;
import java.util.Date;
/**
 * <p>
 * * </p>
 * * @author youcong
 * @since 2020-04-18
 */@Data
@TableName("wp_users")
public class Users extends Model<Users> {
    private static final long serialVersionUID = 1L;
    @TableId(value = "ID", type = IdType.AUTO)
    private Long id;
    @TableField("user_login")
    private String userLogin;
    @TableField("user_pass")
    private String userPass;
    @TableField("user_nicename")
    private String userNicename;
    @TableField("user_email")
    private String userEmail;
    @TableField("user_url")
    private String userUrl;
    @TableField("user_registered")
    private Date userRegistered;
    @TableField("user_activation_key")
    private String userActivationKey;
    @TableField("user_status")
    private Integer userStatus;
    @TableField("display_name")
    private String displayName;
}

三、编写DAO和对应的XML

UserDao.java

package com.blog.tutorial.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.blog.tutorial.entity.Users;
import org.springframework.stereotype.Repository;
/**
 * <p>
 * Mapper 接口 * </p>
 * * @author youcong
 * @since 2020-04-18
 */@Repository
public interface UsersDao extends BaseMapper<Users> {
}

UserDao.xml

<?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.blog.tutorial.dao.UsersDao">
    <!-- 通用查询映射结果 -->
 <resultMap id="BaseResultMap" type="com.blog.tutorial.entity.Users">
        <id column="ID" property="id" />
        <result column="user_login" property="userLogin" />
        <result column="user_pass" property="userPass" />
        <result column="user_nicename" property="userNicename" />
        <result column="user_email" property="userEmail" />
        <result column="user_url" property="userUrl" />
        <result column="user_registered" property="userRegistered" />
        <result column="user_activation_key" property="userActivationKey" />
        <result column="user_status" property="userStatus" />
        <result column="display_name" property="displayName" />
    </resultMap>
    <!-- 通用查询结果列 -->
 <sql id="Base_Column_List">
        ID AS id, user_login AS userLogin, user_pass AS userPass, user_nicename AS userNicename, user_email AS userEmail, user_url AS userUrl, user_registered AS userRegistered, user_activation_key AS userActivationKey, user_status AS userStatus, display_name AS displayName
    </sql>
</mapper>

四、编写Service和实现类

UsersService.java

package com.blog.tutorial.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.blog.tutorial.entity.Users;
/**
 * @description:
 * @author: youcong
 * @time: 2020/11/14 13:26
 */public interface UsersService extends IService<Users> {
}

UserServiceImpl.java

package com.blog.tutorial.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blog.tutorial.dao.UsersDao;
import com.blog.tutorial.entity.Users;
import com.blog.tutorial.service.UsersService;
import org.springframework.stereotype.Service;
/**
 * @description:
 * @author: youcong
 * @time: 2020/11/14 13:27
 */@Service
public class UsersServiceImpl extends ServiceImpl<UsersDao, Users> implements UsersService {
}

五、编写Controller

package com.blog.tutorial.controller;
import com.blog.tutorial.entity.Users;
import com.blog.tutorial.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
 * @description:
 * @author: youcong
 * @time: 2020/11/14 13:27
 */@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
 private UsersService usersService;
    @GetMapping("/list")
    public List<Users> list() {
        return usersService.list();
    }
}

六、编写启动类

Application.java

package com.blog.tutorial;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;
/**
 * @description:
 * @author: youcong
 * @time: 2020/11/14 11:32
 */
@EnableTransactionManagement
@SpringBootApplication
@MapperScan("com.blog.tutorial.dao")
public class BlogApplication {
    public static void main(String[] args) {
        SpringApplication.run(BlogApplication.class, args);
        System.out.println("====服务启动成功====");
    }
}

七、编写配置文件application.yml

server:
  port: 5050
spring:
  # 配置数据源
 datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/wordpress_test?useUnicode=true&characterEncoding=utf-8&serverTimeZone=GMT
    username: root
    password: 123456
    type: com.alibaba.druid.pool.DruidDataSource
# mybatis-plus相关配置
mybatis-plus:
  # xml扫描,多个目录用逗号或者分号分隔(告诉 Mapper 所对应的 XML 文件位置)
 mapper-locations: classpath:mapper/*.xml
  # 以下配置均有默认值,可以不设置
 global-config:
    db-config:
      #主键类型 AUTO:"数据库ID自增" INPUT:"用户输入ID",ID_WORKER:"全局唯一ID (数字类型唯一ID)", UUID:"全局唯一ID UUID";
 id-type: auto
 #字段策略 IGNORED:"忽略判断"  NOT_NULL:"非 NULL 判断")  NOT_EMPTY:"非空判断"
 field-strategy: NOT_EMPTY
      #数据库类型
 db-type: MYSQL
  configuration:
    # 是否开启自动驼峰命名规则映射:从数据库列名到Java属性驼峰命名的类似映射
 map-underscore-to-camel-case: true
 # 如果查询结果中包含空值的列,则 MyBatis 在映射的时候,不会映射这个字段
 call-setters-on-nulls: true
 # 这个配置会将执行的sql打印出来,在开发或测试的时候可以用
 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

八、启动项目

启动项目不报错,通常表示是没有问题的。

九、测试

测试的目的在于看MyBatis-Plus是否真的整成功了。
写了一个查询列表的接口,效果图如下:
image.png


youcongtech
128 声望15 粉丝

我不仅仅喜欢编程,而且还喜欢读书和旅行还有运动。