很长时间没有维护这个专栏了,近来开始实行的一个"DZ"计划,计划的其中一个内容就是促使我写下去,给曾经的"出书梦"做个了结。
当初在创业公司的时候,使用MyBatis-Plus的版本还是2.x,如今已经更到了3.4.1,我的作品也应该与时俱进,于是决定全部升级为3.x,包括我自己的Blog产品。
一、导入Maven依赖
pom.xml
1.8
1.1.13
org.projectlombok
lombok
1.16.20
provided
com.baomidou
mybatis-plus-boot-starter
3.4.1
com.baomidou
mybatis-plus-extension
3.4.1
com.alibaba
druid-spring-boot-starter
${druid-spring-boot-starter.version}
org.springframework.boot
spring-boot-starter-web
mysql
mysql-connector-java
8.0.19
org.springframework.boot
spring-boot-maven-plugin
repackage
~~~~
二、编写实体模型
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;
/**
*
* *
* * @author youcong
* @since 2020-04-18
*/@Data
@TableName("wp_users")
public class Users extends Model {
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;
/**
*
* Mapper 接口 *
* * @author youcong
* @since 2020-04-18
*/@Repository
public interface UsersDao extends BaseMapper {
}
UserDao.xml
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
四、编写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 {
}
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 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 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
八、启动项目
启动项目不报错,通常表示是没有问题的。