MyBatisPlus入门案例

MyBatis-Plus

MyBatisPlus

第一个整合案例

CREATE TABLE user
(
    id BIGINT(20) NOT NULL COMMENT '主键ID',
    name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
    age INT(11) NULL DEFAULT NULL COMMENT '年龄',
    email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
    PRIMARY KEY (id)
);

INSERT INTO user (id, name, age, email) VALUES
(1, 'Jone', 18, '[email protected]'),
(2, 'Jack', 20, '[email protected]'),
(3, 'Tom', 28, '[email protected]'),
(4, 'Sandy', 21, '[email protected]'),
(5, 'Billie', 24, '[email protected]');

pom.xml


        <dependency>
            <groupId>com.baomidougroupId>
            <artifactId>mybatis-plus-boot-starterartifactId>
            <version>3.5.1version>
        dependency>

mybatis-plus的yml配置

mybatis-plus:
  type-aliases-package: com.example.mybatisplus.beans
  mapper-locations: classpath*:mapper/*.xml

java

@Data
public class User {
   
    private Long id;
    private String name;
    private Integer age;
    private String email;
}
@Mapper
public interface UserMapper extends BaseMapper<User> {

}

Test

import org.junit.jupiter.api.Assertions;
@SpringBootTest
public class SampleTest {

    @Autowired
    private UserMapper userMapper;

    @Test
    public void testSelect() {
        System.out.println(("----- selectAll method test ------"));
        List<User> userList = userMapper.selectList(null);
        Assert.assertEquals(5, userList.size());
        userList.forEach(System.out::println);
    }

}

表与实体对应

@TableName 表名不一致

表名和vo不一样是需要制定表名

@TableName(value = "tbl_employee")
public class Employee {
   
    private Integer id ;
    private String lastName;
    private String email ;
    private Integer gender ;
    private Integer age ;
}

@TableId id生成方式

开启自增长

	@TableId(type = IdType.AUTO)
	private Long id;

@TableField

  1. 对象中的属性名和字段名不一致的问题(非驼峰)
  2. 对象中的属性字段在表中不存在的问题
	@TableField(value = "email") //字段名不一致
	private String mail;

	@TableField(exist = false) //数据库中不存在
    private String address;

	@TableField(select = false) //不会查询出来
    private String password;	

注意

TableId 和 TableField 不可以同时使用

重命名和自增在一起时需要这样写

@TableId(value = "pk_id" ,type = IdType.AUTO)

CRUD

插入

@Test
void testInsert(){
   
    User user = new User();
    user.setMail("[email protected]");
    user.setAge(30);
    user.setUserName("哈喽");
    user.setPassword("123");
    user.setAddress("北京");
    userMapper.insert(user);
}

更新操作

根据id更新

@Test
void testUodateById() {
   
    User user = new User();
    user.setId(6L);
    user.setAge(99);

    userMapper.updateById(user);
}

根据条件更新

两种方式

@Test
void testUodate() {
   
    User user = new User();
    user.setAge(98);

    QueryWrapper<User> wrapper = new QueryWrapper<>();
    wrapper.eq("user_name","zhangsan");

    userMapper.update(user,wrapper);
}

@Test
void testUodate2() {
   
    UpdateWrapper<User> wrapper = new UpdateWrapper<>();

    wrapper.set("age",97)
                    .eq("user_name","zhangsan");

    userMapper.update(null,wrapper);
}

删除操作

根据id删除

void testDeleteById() {
   
    userMapper.deleteById(8L);
}

根据条件删除之Map

多条件之间是and关系

@Test
void testDeleteByMap() {
   
    HashMap<String, Object> map = new HashMap<>();
    map.put("user_name", "zhangsan");
    userMapper.deleteByMap(map);
}

根据条件删除之Wrapper

   @Test
    void testDelete() {
   
        //用法一:
//        QueryWrapper wrapper = new QueryWrapper<>();
//        wrapper.eq("user_name","哈喽1");

        //用法二:
        User user = new User();
        user.setUserName("哈喽2");
        QueryWrapper<User> wrapper = new QueryWrapper<>(user);


        userMapper.delete(wrapper);
    }

批量删除

@Test
void testDeleteBatchIds() {
   
    userMapper.deleteBatchIds(Arrays.asList(6L,

你可能感兴趣的:(Java基础,java,mybatis)