配置

  1. spring.datasource.url=jdbc:mariadb://localhost:3306/test
  2. spring.datasource.driver-class-name=com.mariadb.jdbc.Driver
  3. spring.datasource.username=root
  4. spring.datasource.password=

添加 mybatis-plus

bulid.gradledependencies 中添加以下内容:

  1. compile group:'com.baomidou', name: 'mybatis-plus-boot-starter', version: '3.4.2'

在项目的入口处添加以下注解:

  1. @springBootApplication
  2. @MapperScan("com.example.test.mapper")
  3. public class TestApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(TestApplication.class, args);
  6. }
  7. }

test.mapper 下创建 ImagesMapper 文件

  1. public interface ImageMapper extends BaseMapper<Images> {}

使用:

  1. @Service
  2. public class imageService {
  3. @Resource
  4. ImagesMapper imagesMapper
  5. public Images page() {
  6. List<Images> imagesList = imagesMapper.selectList(null);
  7. }
  8. }

获取配置中的值

在Java使用SpringBoot开发的时候,我们有时候需要获取application.properties文件中的值。这个时候我们就需要使用一些方法了。话不多说,上代码:

  1. import org.springframework.core.io.ClassPathResource;
  2. import org.springframework.core.io.Resource;
  3. import org.springframework.core.io.support.PropertiesLoaderUtils;
  4. import java.io.IOException;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7. import java.util.Properties;
  8. public class Example {
  9. private static Map<String,String> getDatabaseSourceConfig() throws IOException {
  10. Map<String,String> map = new HashMap<>();
  11. Resource resource = new ClassPathResource("application.properties");
  12. Properties props = PropertiesLoaderUtils.loadProperties(resource);
  13. String url = props.getProperty("spring.datasource.url");
  14. String username = props.getProperty("spring.datasource.username");
  15. String password = props.getProperty("spring.datasource.password");
  16. map.put("url",url);
  17. map.put("username",username);
  18. map.put("password",password);
  19. return map;
  20. }
  21. }

拦截器

在SpringBoot的开发过程中,我们难免会用到拦截器。我们要在SpringBoot中使用基本的拦截器一般只要实现两个类就行。废话少说,先上代码:
实现HandlerInterceptor:

  1. @Component
  2. public class WebInterceptor implements HandlerInterceptor {
  3. @Override
  4. public boolean preHandle(HttpSerletRequest request, HttpSerletResponse response, Object handler) throws Exception {
  5. return true;
  6. }
  7. @Override
  8. public void postHandle(HttpSerletRequest request, HttpSerletRespone response, Object handler,ModelAndView mv) throws Exception {}
  9. @Override
  10. public void afterCompeletion(HttpSerletRequest request, HttpSerletResponse response, Object handler, Exception exception) throws Exception {}
  11. }
  1. `HandlerInterceptor`类中,`preHandle`在请求到达Controller之前拦截,`postHanlde`Controller处理完成后,返回ModelView之前拦截,`afterCompeletion`在返回ModelView之后拦截。<br />preHandle的返回值,决定了是否要继续把请求向下传播,如果返回`false`,则请求在拦截器中中止,如果返回`true`,请求继续向下传播。<br />实现`WebMvcConfigurer`:
  1. @Component
  2. public class WebConfig implements WebMvcConfigurer {
  3. @Autowired
  4. WebInterceptor webInterceptor;
  5. @Override
  6. public void addInterceptor(InterceptorRegistry registry) {
  7. registry.add(webInterceptor);
  8. }
  9. }

Mybatis-Plus

自动填充

在使用Mybatis-Plus的时候,有时我们需要使用自动填充的功能,比如自动更新创建时间和更新时间等。在使用Mybatis_Plug的自动填充的功能的时候,只要实现两个功能就好。1. 实现元对象处理器接口:com.baodidou.mybatisplus.core.handlers.MetaObjectHandler.2. 给指定的字段添加@TableField(.. fill = FieldFill.INSERT)注解。
示例代码:

实现接口

  1. package com.silence.realworldspring.config;
  2. import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
  3. import org.apache.ibatis.reflection.MetaObject;
  4. import org.springframework.stereotype.Component;
  5. import java.time.LocalDateTime;
  6. @Component
  7. public class MyMetaObjectHandler implements MetaObjectHandler {
  8. @Override
  9. public void insertFill(MetaObject metaObject) {
  10. final LocalDateTime now = LocalDateTime.now();
  11. this.fillStrategy(metaObject, "createdAt", now);
  12. this.fillStrategy(metaObject,"updatedAt", now);
  13. }
  14. @Override
  15. public void updateFill(MetaObject metaObject) {
  16. final LocalDateTime now = LocalDateTime.now();
  17. this.fillStrategy(metaObject, "updatedAt", now);
  18. }
  19. }

添加注解

  1. package com.silence.realworldspring.persistent.entity;
  2. import com.baomidou.mybatisplus.annotation.*;
  3. import java.io.Serializable;
  4. import java.time.LocalDateTime;
  5. import lombok.Data;
  6. import lombok.EqualsAndHashCode;
  7. import lombok.experimental.Accessors;
  8. /**
  9. * <p>
  10. * 文章
  11. * </p>
  12. *
  13. * @author silence_zhpf
  14. * @since 2021-08-12
  15. */
  16. @Data
  17. @EqualsAndHashCode(callSuper = false)
  18. @Accessors(chain = true)
  19. @TableName("articles")
  20. public class Articles implements Serializable {
  21. /**
  22. * 主键
  23. */
  24. @TableId(type = IdType.ASSIGN_UUID)
  25. private String slug;
  26. /**
  27. * 标题
  28. */
  29. private String title;
  30. /**
  31. * 描述
  32. */
  33. private String description;
  34. /**
  35. * 文章内容
  36. */
  37. private String body;
  38. /**
  39. * 创建时间
  40. */
  41. @TableField(value = "created_at",fill = FieldFill.INSERT)
  42. private LocalDateTime createdAt;
  43. /**
  44. * 更新时间
  45. */
  46. @TableField(value = "updated_at",fill = FieldFill.INSERT_UPDATE)
  47. private LocalDateTime updatedAt;
  48. public static final String SLUG = "slug";
  49. public static final String TITLE = "title";
  50. public static final String DESCRIPTION = "description";
  51. public static final String BODY = "body";
  52. public static final String CREATED_AT = "created_at";
  53. public static final String UPDATED_AT = "updated_at";
  54. }