很長時間沒有維護這個專欄了,近來開始實行的一個"DZ"計劃,計劃的其中一個內容就是促使我寫下去,給曾經的"出書夢"作個告終。java
當初在創業公司的時候,使用MyBatis-Plus的版本仍是2.x,現在已經更到了3.4.1,個人做品也應該與時俱進,因而決定所有升級爲3.x,包括我本身的Blog產品。mysql
pom.xmlweb
<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.javaspring
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; }
UserDao.javasql
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>
UsersService.javaapache
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.javamybatis
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 { }
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.javaapp
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("====服務啓動成功===="); } }
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
啓動項目不報錯,一般表示是沒有問題的。maven
測試的目的在於看MyBatis-Plus是否真的整成功了。
寫了一個查詢列表的接口,效果圖以下: