标签:pen sql spl err ram 泛型 增删改 source repo
<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;
}
@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);
}
}
@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;
}
在插入或更新时,填充字段值
@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;
}
实现自定义类
@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);
}
乐观锁实现:当要更新一条记录的时候,希望这条记录没有被别人更新
@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=?
*
*/
测试更新操作被插队
@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=?
*
*/
标签:pen sql spl err ram 泛型 增删改 source repo
原文地址:https://www.cnblogs.com/xiongyungang/p/14091763.html