官网没有整合完整的例子,自己整合起来一通乱试,终于搞定,现在记录一下:
工程图:
1,去官网上找到导入maven的依赖
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.5.RELEASE
com.example
demo
0.0.1-SNAPSHOT
demo
Demo project for Spring Boot
1.8
3.1.1
org.springframework.boot
spring-boot-starter-jdbc
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.0.1
org.springframework.boot
spring-boot-starter-test
test
com.baomidou
mybatis-plus-boot-starter
${baomidou.version}
mysql
mysql-connector-java
runtime
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-maven-plugin
src/main/resources
true
src/main/java
**/*.xml
2,User类
package com.example.demo.entity;
import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
/**
*
* 管理员表
*
*/
@TableName("sys_user")
@Data
public class User implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 主键id
*/
@TableId(value = "USER_ID", type = IdType.ID_WORKER)
private Long userId;
/**
* 头像
*/
@TableField("AVATAR")
private String avatar;
/**
* 账号
*/
@TableField("ACCOUNT")
private String account;
/**
* 密码
*/
@TableField("PASSWORD")
private String password;
/**
* md5密码盐
*/
@TableField("SALT")
private String salt;
/**
* 名字
*/
@TableField("NAME")
private String name;
/**
* 生日
*/
@TableField("BIRTHDAY")
private Date birthday;
/**
* 性别(字典)
*/
@TableField("SEX")
private String sex;
/**
* 电子邮件
*/
@TableField("EMAIL")
private String email;
/**
* 电话
*/
@TableField("PHONE")
private String phone;
/**
* 角色id(多个逗号隔开)
*/
@TableField("ROLE_ID")
private String roleId;
/**
* 部门id(多个逗号隔开)
*/
@TableField("DEPT_ID")
private Long deptId;
/**
* 状态(字典)
*/
@TableField("STATUS")
private String status;
/**
* 创建时间
*/
@TableField(value = "CREATE_TIME", fill = FieldFill.INSERT)
private Date createTime;
/**
* 创建人
*/
@TableField(value = "CREATE_USER", fill = FieldFill.INSERT)
private Long createUser;
/**
* 更新时间
*/
@TableField(value = "UPDATE_TIME", fill = FieldFill.UPDATE)
private Date updateTime;
/**
* 更新人
*/
@TableField(value = "UPDATE_USER", fill = FieldFill.UPDATE)
private Long updateUser;
/**
* 乐观锁
*/
@TableField("VERSION")
private Integer version;
}
3,UserMapper.xml
USER_ID AS userId, AVATAR AS avatar, ACCOUNT AS account, PASSWORD AS password,
SALT AS salt, NAME AS name, BIRTHDAY AS birthday, SEX AS sex, EMAIL AS email, PHONE AS phone,
ROLE_ID AS roleId, DEPT_ID AS deptId, STATUS AS status, CREATE_TIME AS createTime, CREATE_USER AS createUser,
UPDATE_TIME AS updateTime, UPDATE_USER AS updateUser, VERSION AS version
4,UserMapper
package com.example.demo.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface UserMapper extends BaseMapper {
/**
* 通过账号获取用户
*/
User getByAccount(@Param("account") String account);
}
5,DemoApplication
package com.example.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
6,application.properties
server.port=9999
#mysql
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/shirodemo?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=CTT
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#mybatis-plus
mybatis-plus.mapper-locations=classpath*:com/example/demo/mapper/**/*.xml
mybatis-plus.type-aliases-package=com.example.demo.entity
7,sql
CREATE TABLE `sys_user` (
`USER_ID` bigint(20) NOT NULL COMMENT '主键id',
`AVATAR` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '头像',
`ACCOUNT` varchar(45) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '账号',
`PASSWORD` varchar(45) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '密码',
`SALT` varchar(45) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT 'md5密码盐',
`NAME` varchar(45) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '名字',
`BIRTHDAY` datetime(0) DEFAULT NULL COMMENT '生日',
`SEX` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '性别(字典)',
`EMAIL` varchar(45) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '电子邮件',
`PHONE` varchar(45) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '电话',
`ROLE_ID` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '角色id(多个逗号隔开)',
`DEPT_ID` bigint(20) DEFAULT NULL COMMENT '部门id(多个逗号隔开)',
`STATUS` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL COMMENT '状态(字典)',
`CREATE_TIME` datetime(0) DEFAULT NULL COMMENT '创建时间',
`CREATE_USER` bigint(20) DEFAULT NULL COMMENT '创建人',
`UPDATE_TIME` datetime(0) DEFAULT NULL COMMENT '更新时间',
`UPDATE_USER` bigint(20) DEFAULT NULL COMMENT '更新人',
`VERSION` int(11) DEFAULT NULL COMMENT '乐观锁',
PRIMARY KEY (`USER_ID`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '管理员表' ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of sys_user
-- ----------------------------
INSERT INTO `sys_user` VALUES (1, '1', 'admin', '1d6b1208c7d151d335790276a18e3d99', 'q6taw', 'liwl', '2019-01-16 00:00:00', 'M', '[email protected]', '18200000000', '1', 27, 'ENABLE', '2016-01-29 08:49:53', NULL, '2018-12-28 22:52:24', 24, 25);
8,最后调用test,执行搞定
package com.pt.invoice;
import com.pt.invoice.modular.system.entity.User;
import com.pt.invoice.modular.system.mapper.UserMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
public void testSelect() {
System.out.println(("----- selectAll method test ------"));
List userList = userMapper.selectList(null);
userList.forEach(System.out::println);
User user=userMapper.getByAccount("admin");
System.out.println("用户admin======"+user);
}
}
gitee地址:https://gitee.com/TimeToSayGoodbye/MyBatis-Plus-Test.git