配置
spring.datasource.url=jdbc:mariadb://localhost:3306/testspring.datasource.driver-class-name=com.mariadb.jdbc.Driverspring.datasource.username=rootspring.datasource.password=
添加 mybatis-plus
在 bulid.gradle 的 dependencies 中添加以下内容:
compile group:'com.baomidou', name: 'mybatis-plus-boot-starter', version: '3.4.2'
在项目的入口处添加以下注解:
@springBootApplication@MapperScan("com.example.test.mapper")public class TestApplication {public static void main(String[] args) {SpringApplication.run(TestApplication.class, args);}}
在 test.mapper 下创建 ImagesMapper 文件
public interface ImageMapper extends BaseMapper<Images> {}
使用:
@Servicepublic class imageService {@ResourceImagesMapper imagesMapperpublic Images page() {List<Images> imagesList = imagesMapper.selectList(null);}}
获取配置中的值
在Java使用SpringBoot开发的时候,我们有时候需要获取application.properties文件中的值。这个时候我们就需要使用一些方法了。话不多说,上代码:
import org.springframework.core.io.ClassPathResource;import org.springframework.core.io.Resource;import org.springframework.core.io.support.PropertiesLoaderUtils;import java.io.IOException;import java.util.HashMap;import java.util.Map;import java.util.Properties;public class Example {private static Map<String,String> getDatabaseSourceConfig() throws IOException {Map<String,String> map = new HashMap<>();Resource resource = new ClassPathResource("application.properties");Properties props = PropertiesLoaderUtils.loadProperties(resource);String url = props.getProperty("spring.datasource.url");String username = props.getProperty("spring.datasource.username");String password = props.getProperty("spring.datasource.password");map.put("url",url);map.put("username",username);map.put("password",password);return map;}}
拦截器
在SpringBoot的开发过程中,我们难免会用到拦截器。我们要在SpringBoot中使用基本的拦截器一般只要实现两个类就行。废话少说,先上代码:
实现HandlerInterceptor:
@Componentpublic class WebInterceptor implements HandlerInterceptor {@Overridepublic boolean preHandle(HttpSerletRequest request, HttpSerletResponse response, Object handler) throws Exception {return true;}@Overridepublic void postHandle(HttpSerletRequest request, HttpSerletRespone response, Object handler,ModelAndView mv) throws Exception {}@Overridepublic void afterCompeletion(HttpSerletRequest request, HttpSerletResponse response, Object handler, Exception exception) throws Exception {}}
在`HandlerInterceptor`类中,`preHandle`在请求到达Controller之前拦截,`postHanlde`在Controller处理完成后,返回ModelView之前拦截,`afterCompeletion`在返回ModelView之后拦截。<br />preHandle的返回值,决定了是否要继续把请求向下传播,如果返回`false`,则请求在拦截器中中止,如果返回`true`,请求继续向下传播。<br />实现`WebMvcConfigurer`:
@Componentpublic class WebConfig implements WebMvcConfigurer {@AutowiredWebInterceptor webInterceptor;@Overridepublic void addInterceptor(InterceptorRegistry registry) {registry.add(webInterceptor);}}
Mybatis-Plus
自动填充
在使用Mybatis-Plus的时候,有时我们需要使用自动填充的功能,比如自动更新创建时间和更新时间等。在使用Mybatis_Plug的自动填充的功能的时候,只要实现两个功能就好。1. 实现元对象处理器接口:com.baodidou.mybatisplus.core.handlers.MetaObjectHandler.2. 给指定的字段添加@TableField(.. fill = FieldFill.INSERT)注解。
示例代码:
实现接口
package com.silence.realworldspring.config;import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;import org.apache.ibatis.reflection.MetaObject;import org.springframework.stereotype.Component;import java.time.LocalDateTime;@Componentpublic class MyMetaObjectHandler implements MetaObjectHandler {@Overridepublic void insertFill(MetaObject metaObject) {final LocalDateTime now = LocalDateTime.now();this.fillStrategy(metaObject, "createdAt", now);this.fillStrategy(metaObject,"updatedAt", now);}@Overridepublic void updateFill(MetaObject metaObject) {final LocalDateTime now = LocalDateTime.now();this.fillStrategy(metaObject, "updatedAt", now);}}
添加注解
package com.silence.realworldspring.persistent.entity;import com.baomidou.mybatisplus.annotation.*;import java.io.Serializable;import java.time.LocalDateTime;import lombok.Data;import lombok.EqualsAndHashCode;import lombok.experimental.Accessors;/*** <p>* 文章* </p>** @author silence_zhpf* @since 2021-08-12*/@Data@EqualsAndHashCode(callSuper = false)@Accessors(chain = true)@TableName("articles")public class Articles implements Serializable {/*** 主键*/@TableId(type = IdType.ASSIGN_UUID)private String slug;/*** 标题*/private String title;/*** 描述*/private String description;/*** 文章内容*/private String body;/*** 创建时间*/@TableField(value = "created_at",fill = FieldFill.INSERT)private LocalDateTime createdAt;/*** 更新时间*/@TableField(value = "updated_at",fill = FieldFill.INSERT_UPDATE)private LocalDateTime updatedAt;public static final String SLUG = "slug";public static final String TITLE = "title";public static final String DESCRIPTION = "description";public static final String BODY = "body";public static final String CREATED_AT = "created_at";public static final String UPDATED_AT = "updated_at";}
