Mybatis-Plus中文网的快速开始可以快速搭建项目,具体就不多介绍,主要讲述遇到的几个问题。
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.4.1version>
dependency>
@MapperScan("com.example.mybatisplus01.mapper")
//
@SpringBootApplication
@MapperScan("com.example.mybatisplus01.mapper")
public class MybatisPlus01Application {
public static void main(String[] args) {
SpringApplication.run(MybatisPlus01Application.class, args);
}
}
public interface UserMapper extends BaseMapper<User> {
}
测试程序:
@Test
public void UserTest(){
List<User> userList = userMapper.selectList(null);
System.out.println(userList);
}
第一个报错:
Public Key Retrieval is not allowed
,在我们使用MySQL8.0的,连接数据库会存在一定的问题当提示:Public Key Retrieval is not allowed 错误的时候,我们可以在连接数据库的配置文件中加上
allowPublicKeyRetrieval=true
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/smbms?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8&useSSL=false
第二个报错:
由于我的数据表名是smbms_user,而bean是user并不对应,需要使用@TableName
注解声明
第三个错误:
Unknown column 'user_code' in 'field list'
在Java Bean对象中使用的是驼峰命名,但是经Mybatis-Plus映射后变为了_
的命名方式(userCode变为user_code),这和数据库字段和pojo的都不一样,需要将java bean和数据库字段按mybatis-plus的命名方式更改吗?
Mybatis-Plus提供了@TableField
注解来解决这一问题:
package com.example.mybatisplus01.domain;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
@TableName("smbms_user")
public class User {
private int id;
@TableField("userCode")
private String userCode;
@TableField("userName")
private String userName;
@TableField("userPassword")
private String userPassword;
private int gender;
private String birthday;
private String phone;
private String address;
@TableField("userRole")
private int userRole;
@TableField("createdBy")
private int createdBy;
@TableField("creationDate")
private String creationDate;
@TableField("modifyBy")
private int modifyBy;
@TableField("modifyDate")
private String modifyDate;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserCode() {
return userCode;
}
public void setUserCode(String userCode) {
this.userCode = userCode;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public int getGender() {
return gender;
}
public void setGender(int gender) {
this.gender = gender;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getUserRole() {
return userRole;
}
public void setUserRole(int userRole) {
this.userRole = userRole;
}
public int getCreatedBy() {
return createdBy;
}
public void setCreatedBy(int createdBy) {
this.createdBy = createdBy;
}
public String getCreationDate() {
return creationDate;
}
public void setCreationDate(String creationDate) {
this.creationDate = creationDate;
}
public int getModifyBy() {
return modifyBy;
}
public void setModifyBy(int modifyBy) {
this.modifyBy = modifyBy;
}
public String getModifyDate() {
return modifyDate;
}
public void setModifyDate(String modifyDate) {
this.modifyDate = modifyDate;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", userCode='" + userCode + '\'' +
", userName='" + userName + '\'' +
", userPassword='" + userPassword + '\'' +
", gender=" + gender +
", birthday='" + birthday + '\'' +
", phone='" + phone + '\'' +
", address='" + address + '\'' +
", userRole=" + userRole +
", createdBy=" + createdBy +
", creationDate='" + creationDate + '\'' +
", modifyBy=" + modifyBy +
", modifyDate='" + modifyDate + '\'' +
'}';
}
}
Mybatis-Plus会自动将驼峰命令改为
_
下划线的,所以在数据库表设计之初可以遵循其规则设计。否则就需要使用@TableField
来覆盖默认。
Mybatis-Plus注解的使用
CRUD操作
可以看到插入操作只有一种方法,那必然是插入一个数据项(一行元素)包含了数据库表的各个字段,这就意味着mybatis-plus帮我们完成了mybatis的如下的模块:
<insert paramerType="pojo">
insert into user(...) values (...)
insert>
同时限定了插入的输入对象只能是Java bean对象。对于开发者来说只需要输入对象实例即可。
测试代码:
import com.example.mybatisplus01.domain.User;
import com.example.mybatisplus01.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
@SpringBootTest
@RunWith(SpringRunner.class)
public class MybatisTest01 {
@Resource
private UserMapper userMapper;
@Test
public void insetTest(){
User user = new User();
user.setId(16);
user.setAddress("北京市海淀区成府路207号");
user.setBirthday("1980-01-01");
user.setCreatedBy(1);
user.setGender(1);
user.setCreationDate("1980-01-01");
user.setModifyBy(1);
user.setModifyDate("2013-03-21 16:52:07");
user.setPhone("18567542321");
user.setUserCode("lihua");
user.setUserName("李华");
user.setUserRole(1);
user.setUserPassword("1234567");
userMapper.insert(user);
}
}
插入类型已限定只能是java bean类型。
2 .删除
delete接口有四个方法,一般之用的上deleteById()
方法传入一个id即可。
@Test
public void deleteTest1(){
userMapper.deleteById(17);
}
updateById()
方法是传入一个Java bean根据id选择修改对应的字段,java bean提供几个字段就修改那几个字段。