共计 6652 个字符,预计需要花费 17 分钟才能阅读完成。
很长时间没有保护这个专栏了,近来开始履行的一个 ”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 是否真的整胜利了。
写了一个查问列表的接口,效果图如下:
正文完