整体的工程结构
雪花算法,自动生成id传送门
乐观锁讲解传送门,点击!!!
自动填充处理 点击!!
分页查询操作
DROP TABLE IF EXISTS user;
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>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
1.这里UserMapper,不需要在配置繁琐的xml文件
package com.xyj.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xyj.pojo.User;
import org.springframework.stereotype.Repository;
/**
* @author xyj
* @date 2020/4/24 -10:20
*/
@Repository
public interface UserMapper extends BaseMapper<User>{
}
2.pojo层里的User,与数据库对应
package com.xyj.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author xyj
* @date 2020/4/24 -10:17
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
3.这里注意MybatisPlusApplication这里需要添加扫描mapper文件
@MapperScan(“com.xyj.mapper”)对应你的包位置
package com.xyj;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//扫描mapper文件夹
@MapperScan("com.xyj.mapper")
@SpringBootApplication
public class MybatisPlusApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisPlusApplication.class, args);
}
}
4.application.properties的相关配置,与之前没有什么不同
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis_plus?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
5.测试一下在MybatisPlusApplicationTests
package com.xyj;
import com.xyj.mapper.UserMapper;
import com.xyj.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
class MybatisPlusApplicationTests {
//继承basemapper,所有方法来自父类
@Autowired
private UserMapper userMapper;
@Test
void contextLoads() {
//查询全部
List<User> users = userMapper.selectList(null);
users.forEach(System.out::println);
}
}
@Test
public void testInsert(){
User user = new User();
user.setName("xyj");
user.setAge(18);
user.setEmail("[email protected]");
int result = userMapper.insert(user);//帮我们自动生成id
System.out.println(result);
System.out.println(user);
System.out.println(user.getId());
}
更新操作
//通过条件自动拼接动态sql
@Test
public void testUpdate(){
User user = new User();
user.setId( 5L);
user.setAge(60);
user.setName("青青草原");
user.setEmail("[email protected]");
int result = userMapper.updateById(user);
System.out.println(result);
}
//单个查询
@Test
public void testSelectById(){
User user = userMapper.selectById(1L);
System.out.println(user);
}
//批量查询
@Test
public void testSelectById1(){
List<User> user = userMapper.selectBatchIds(Arrays.asList(1, 2, 3));
user.forEach(System.out::println);
}
//条件查询 map
@Test
public void testSelectByBatchIds(){
HashMap<String, Object> hashmap = new HashMap<>();
//自定义查询,这里可以根据不同条件设置
hashmap.put("name","xyj");
hashmap.put("age",18);
List<User> user = userMapper.selectByMap(hashmap);
user.forEach(System.out::println);
}
删除操作
//删除
@Test
public void testDelete(){
int i = userMapper.deleteById(5L);
}
// 通过id批量删除
@Test
public void testDeleteBatchId(){
userMapper.deleteBatchIds(Arrays.asList(1L,2L));
}
// 通过map删除
@Test
public void testDeleteMap(){
HashMap<String, Object> map = new HashMap<>();
map.put("name","xyj");
userMapper.deleteByMap(map);
}