定时任务
- 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
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)
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);
System.out.println(mp.getTid());
}
@Test
public void update(){
TestMybatisplus mp = new TestMybatisplus();
mp.setTid(123L);
mp.setTname("测试9");
mp.setTversion(1);
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);
System.out.println(testMybatispluses);
}
}
自定义配置类,配置分页插件及乐观锁插件
@Configuration
public class MyConfiguration {
@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;
}
}
逆向工程
<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);
}
}