定时任务
- springboot内部集成,不需要引入jar包,不需要配置。
- 定时任务使用务必存进redis或mysql,以防服务器挂掉定时任务丢失。
- 使用(基本同spring):
- 在Application.java添加注解
@EnableScheduling
- 编写定时任务
@Component
public class ScheduleService {
@Scheduled(cron = "*/5 15 18 * * ?")
public void scheduledTest(){
System.out.println("定时任务");
}
}
mybatis基于注解开发
- 使用注解开发不需要xml文件,所以pom文件的resources不必再配置,yml文件的mapper-locations也不用再配置。别名和驼峰映射也不需要。
- mapper接口代码
public interface ApplySimpleMapper {
@Delete("delete from aftersale_return_apply_simple where aras_id = #{arasId}")
int deleteByPrimaryKey(Integer arasId);
@Insert("<script>" +
"insert into aftersale_return_apply_simple" +
"<trim prefix='(' suffix=')' suffixOverrides=','>" +
"<if test='arasName != null'>aras_name,</if>" +
"<if test='arasOrderId != null'>aras_order_id,</if>" +
"</trim>" +
"<trim prefix='values (' suffix=')' suffixOverrides=','>" +
"<if test='arasName != null'>#{arasName},</if>" +
"<if test='arasOrderId != null'>#{arasOrderId},</if>" +
"</trim>" +
"</script>")
@Options(useGeneratedKeys = true, keyProperty = "arasId")
int insertSelective(ApplySimple record);
@Select("select * from aftersale_return_apply_simple where aras_id = #{arasId}")
ApplySimple selectByPrimaryKey(Integer arasId);
@Select("<script>" +
"select * from aftersale_return_apply_simple" +
"<where>" +
"<if test='arasName != null'>and aras_name = #{arasName}</if>" +
"<if test='arasOrderId != null'>and aras_order_id = #{arasOrderId}</if>" +
"</where>" +
"</script>")
List<ApplySimple> selectByPrimaryKeyList(ApplySimple applySimple);
@Update("<script>update aftersale_return_apply_simple" +
"<set >" +
"<if test='arasName != null'>aras_name = #{arasName},</if>" +
"<if test='arasOrderId != null'>aras_order_id = #{arasOrderId},</if>" +
"<if test='arasRecordName != null'>aras_record_name = #{arasRecordName},</if>" +
"<if test='arasStatus != null'>aras_status = #{arasStatus},</if>" +
"<if test='arasCreatetime != null'>aras_createtime = #{arasCreatetime},</if>" +
"<if test='arasUpdatetime != null'>aras_updatetime = #{arasUpdatetime},</if>" +
"</set>" +
"where aras_id = #{arasId}" +
"</script>")
int updateByPrimaryKeySelective(ApplySimple record);
}
mybatis-plus
- 引入mybatis-plus的pom,基于mybatis,所以不需要重复引入mybatis的pom。
- 与pagehelper包冲突,排出冲突的jar包(pagehelper不需要就删掉)
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.13</version>
<exclusions>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</exclusion>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</exclusion>
</exclusions>
</dependency>
- Application.java配置MapperScan
@MapperScan("com.javasm.mapper")
@EnableTransactionManagement
- 配置datasource
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.14.241:3306/crm?characterEncoding=UTF8&useSSL=true&serverTimezone=Asia/Shanghai
username: root
password: root
实体类用到的注解
@TableName("test_mybatisplus")
public class TestMybatisplus {
@TableId(type = IdType.ASSIGN_ID) // 分布式唯一id,雪花算法
private Long tid;
private String tname;
private String tadd;
private String tcreatetime;
private String tupdatetime;
@TableField(exist = false) // 非数据库字段
private String authControl;
@Version // 乐观锁注解
private Integer tversion;
}
crud测试
@SpringBootTest
public class MyBatisPlusTest {
@Resource
private ApplySimpleMapper2 sm;
@Test
public void add(){
TestMybatisplus mp = new TestMybatisplus();
mp.setTname("测试");
int insert = sm.insert(mp);
// mybatisplus会自动把当前插入对象在数据库中的id写回到该实体中
System.out.println(mp.getTid());
}
@Test
public void update(){
TestMybatisplus mp = new TestMybatisplus();
mp.setTid(123L);
mp.setTname("测试9");
// 乐观锁插件配置之后,更新成功version会自增加一,version不一致更新返回值为0
mp.setTversion(1);
//根据id进行更新,没有传值的属性就不会更新
int i = sm.updateById(mp);
System.out.println(i);
}
@Test
public void select(){
// 条件构造器
QueryWrapper wrapper = new QueryWrapper();
wrapper.between("tcreatetime","2020-08-20","2020-09-20");
wrapper.like("tname","测试");
List<TestMybatisplus> testMybatispluses = sm.selectList(wrapper);
// Map<String, Object> map = new HashMap<>();
// map.put("tname","测试4");
// List<TestMybatisplus> testMybatispluses = sm.selectByMap(map);
System.out.println(testMybatispluses);
}
}
自定义配置类,配置分页插件及乐观锁插件
@Configuration
public class MyConfiguration {
//Jackson中通过ObejctMapper对象的writeValueAsString
// 由于分布式id过长,js会精度损失,将id转为string再序列化
@Bean
ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
ObjectMapper build = builder.createXmlMapper(false).build();
build.setSerializationInclusion(JsonInclude.Include.NON_NULL);//非空才序列化
SimpleModule module = new SimpleModule();
module.addSerializer(Long.class,ToStringSerializer.instance);
module.addSerializer(long.class,ToStringSerializer.instance);
build.registerModule(module);
return build;
}
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//分页拦截器
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
//乐观锁拦截器
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
return interceptor;
}
}
逆向工程
- pom引入,使用freemarker模板引擎
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.30</version>
</dependency>
- 生成代码使用官网模板,稍作修改即可。
分页插件使用
@RestController
@RequestMapping("/generator")
public class TestMybatisplusController {
@Resource
private ITestMybatisplusService ms;
@GetMapping("mp")
public ResponseEntity getmp(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "2")Integer pageSize){
IPage<MP> page = new Page<>(pageNum,pageSize);
IPage<MP> page1 = ms.page(page);
return ResponseEntity.ok(page1);
}
}
版权声明:本文为qq_43050847原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。