码迷,mamicode.com
首页 > 其他好文 > 详细

mybatis-plus 相关

时间:2020-12-10 10:57:28      阅读:3      评论:0      收藏:0      [点我收藏+]

标签:pen   sql   spl   err   ram   泛型   增删改   source   repo   

集成springboot

依赖

  • 核心依赖spring-boot-starter、lombo、mybatis-plus-boot-starter、mysql
  • 使用Mybatis-plus代替Mybatis的依赖
<dependencies>
		<!--核心 POM,包含自动配置支持、日志库和对 YAML 配置文件的支持-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<!--核心 POM,包含自动配置支持、日志库和对 YAML 配置文件的支持-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

		<!--支持使用 JDBC 访问数据库-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>

		<!-- springboot的启动依赖(集成tomcat)  -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<!-- mysql -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>

		<!-- mybatis plus -->
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-boot-starter</artifactId>
			<version>3.0.5</version>
		</dependency>

		<!--Druid-->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid-spring-boot-starter</artifactId>
			<version>1.1.10</version>
		</dependency>

		<!--Junit-->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

实体类

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Content implements Serializable{
    Long id;
    String author;
    String Content;
    String title;
    Integer category_id;
    Integer user_id;
    Date create_time;
}

Mapper

  • BaseMapper:泛型类,接受一个操作对象,实现该类的增删改查
@Repository
public interface ContentMapper extends BaseMapper<Content> {
}

测试

@RunWith(SpringRunner.class)
@SpringBootTest
public class ContentMapperTest {
    @Resource
    ContentMapper contentMapper;

    @Test
    public void list() throws Exception {
        List<Content> list = contentMapper.selectList(null);
        System.out.println("list = " + list);
    }
}

修改自增策略

  • 默认雪花算法设置id的值
  • @TableId(type = IdType.AUTO):设置自增策略
  • 自增策略包括:自增id(AUTO)、UUID、手动输入(INPUT)、空(NONE)..
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Content implements Serializable{
    @TableId(type = IdType.AUTO)
    Long id;
    String author;
    String Content;
    String title;
    Integer category_id;
    Integer user_id;
    Date create_time;
}

自动填充

在插入或更新时,填充字段值

  • @TableField(fill = FieldFill.INSERT):插入时填充
  • @TableField(fill = FieldFill.INSERT_UPDATE):插入更新时填充
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Content implements Serializable{
    // 主键自增
    @TableId(type = IdType.AUTO)
    Long id;
    String author;
    String Content;
    String title;
    Integer category_id;
    Integer user_id;

    // 字段填充, 插入时填充
    @TableField(fill = FieldFill.INSERT)
    Date create_time;

    // 插入和更新时填充
    @TableField(fill = FieldFill.INSERT_UPDATE)
    Date update_time;
}

实现自定义类

  • 插入操作时调用insertFill方法,setFieldValByName设置属性值

@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    // 插入时填充
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("insertFill");
        Date date = new Date();
        this.setFieldValByName("create_time", date, metaObject);
        this.setFieldValByName("update_time", date, metaObject);
    }

    // 更新时填充
    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("updateFill");
        Date date = new Date();
        this.setFieldValByName("update_time", date, metaObject);
    }
}

测试

  @Test
    public void insert() {
        Content content = new Content();
        content.setAuthor("Tom");
        content.setTitle("Everyone and Everything");
        content.setContent("long long ago..");
        // 返回受影响的行数
        int insert = contentMapper.insert(content);
        System.out.println("insert = " + insert);
    }

    @Test
    public void update() {
        Content content = new Content();
        content.setId(126L);
        content.setAuthor("Tom");
        content.setTitle("Everyone and Everything");
        content.setContent("long long ago..");
        int i = contentMapper.updateById(content);
        System.out.println("i = " + i);
    }

乐观锁

乐观锁实现:当要更新一条记录的时候,希望这条记录没有被别人更新

  • @Version:标记为一个乐观锁字段

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Content implements Serializable{
    // 主键自增
    @TableId(type = IdType.AUTO)
    Long id;

    String author;
    String Content;
    String title;
    Integer category_id;
    Integer user_id;

    // 乐观锁
    @Version
    Integer version;

    // 字段填充, 插入时填充
    @TableField(fill = FieldFill.INSERT)
    Date create_time;

    // 插入和更新时填充
    @TableField(fill = FieldFill.INSERT_UPDATE)
    Date update_time;
}

配置类配置乐观锁插件

/**
 * mybatis plus 配置类
 */
@MapperScan("com.exp.dao")
@EnableTransactionManagement
@Configuration
public class MybatisPlusConfiguration {

    /**
     * 乐观锁插件
     * @return
     */
    @Bean
    public OptimisticLockerInterceptor optimisticLockerInterceptor() {
        return new OptimisticLockerInterceptor();
    }
}

测试

    @Test
    public void lock() {
        // 查询时获取version
        Content content = contentMapper.selectById(126);
        content.setAuthor("Mark");
        // 更新时进行version比对,确定没有被人修改
        contentMapper.updateById(content);
    }
    /**
     *
     * SELECT id,author,content,title,category_id AS category_id,user_id AS user_id,version,create_time AS create_time,update_time AS update_time FROM content WHERE id=? 
     *
     * UPDATE content SET author=?, content=?, title=?, version=?, update_time=? WHERE id=? AND version=? 
     *
     */

测试更新操作被插队

  • update操作会判断version是否被更改,没有则执行更新,并将version递增
    @Test
    public void lock() {
        // 查询时获取version
        Content content = contentMapper.selectById(126);

        // 模拟更新操作被插队(可能时其他线程)
        Content content1 = contentMapper.selectById(126);
        content1.setAuthor("Link");
        contentMapper.updateById(content1); // 此处已经将version更新

        content.setAuthor("Nobita");
        // 更新时进行version比对,确定没有被人修改,再更新version
        contentMapper.updateById(content);
    }
    /**
     *
     * SELECT id,author,content,title,category_id AS category_id,user_id AS user_id,version,create_time AS create_time,update_time AS update_time FROM content WHERE id=?
     *
     * SELECT id,author,content,title,category_id AS category_id,user_id AS user_id,version,create_time AS create_time,update_time AS update_time FROM content WHERE id=?
     *
     * UPDATE content SET author=?, content=?, title=?, version=?, update_time=? WHERE id=? AND version=?
     *
     * UPDATE content SET author=?, content=?, title=?, version=?, update_time=? WHERE id=? AND version=?
     *
     */

mybatis-plus 相关

标签:pen   sql   spl   err   ram   泛型   增删改   source   repo   

原文地址:https://www.cnblogs.com/xiongyungang/p/14091763.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!