依赖
<!-- MySQL -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
</dependency>
<!-- mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.2</version>
</dependency>
<!-- freemarker -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
</dependency>
代码
public class codeGenerate {
public static void main(String[] args) {
String url = "jdbc:mysql://43.140.253.7:28414/byr-bbs?useSSL=true";
String username = "root";
String password = "xxx";
String tables = "user"; // 设置需要生成的表名
String prefix_tables = ""; // 设置过滤表前缀
String moduleName = "user";
// 父包名
String parent = "com.bbs";
// 项目路径
String projectPath = "D:\\\\FullStack\\\\bbs\\\\backend\\\\src\\\\main\\\\java";
// 设置mapperXml生成路径
String mapperLocation = "D:\\\\FullStack\\\\bbs\\\\backend\\\\src\\\\main\\\\resources\\\\mapper\\\\" + moduleName;
FastAutoGenerator.create(url, username, password)
.globalConfig(builder -> {
builder.author("thexb") // 设置作者
// .enableSwagger() // 开启 swagger 模式
// .fileOverride() // 覆盖已生成文件
.outputDir(projectPath); // 指定输出目录
})
.packageConfig(builder -> {
builder.parent(parent)
.moduleName(moduleName)
.pathInfo(Collections.singletonMap(OutputFile.xml, mapperLocation));
})
.strategyConfig(builder -> {
builder.addInclude(tables)
.addTablePrefix(prefix_tables);
})
.templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板
.execute();
}
}
在启动类配置mapper扫描
@MapperScan("com.ikun.*.mapper")
@Data
public class R<T> implements Serializable {
private Integer code; //编码:20000成功,其他数字为失败
private String msg; //错误信息
private T data; //数据
private Map map = new HashMap(); //动态数据
public static <T> R<T> success(T object) {
R<T> r = new R<T>();
r.data = object;
r.code = 20000;
return r;
}
public static <T> R<T> error(String msg) {
R r = new R();
r.msg = msg;
r.code = 20001;
return r;
}
public R<T> add(String key, Object value) {
this.map.put(key, value);
return this;
}
}
@Configuration
public class CorsConfig {
@Bean
public CorsFilter corsFilter(){
//1.添加CORS配置信息
CorsConfiguration config = new CorsConfiguration();
//1) 允许的域,不要写*,否则cookie就无法使用了
config.addAllowedOrigin("<http://localhost:8081>"); //这里填写请求的前端服务器
//2) 是否发送Cookie信息
config.setAllowCredentials(true);
//3) 允许的请求方式
config.addAllowedMethod("OPTIONS");
config.addAllowedMethod("HEAD");
config.addAllowedMethod("GET");
config.addAllowedMethod("PUT");
config.addAllowedMethod("POST");
config.addAllowedMethod("DELETE");
config.addAllowedMethod("PATCH");
// 4)允许的头信息
config.addAllowedHeader("*");
//2.添加映射路径,我们拦截一切请求
UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
configSource.registerCorsConfiguration("/**", config);
//3.返回新的CorsFilter.
return new CorsFilter(configSource);
}
}
server:
port: 8088
spring:
application:
name: ikun
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://43.140.253.7:28414/byr-bbs?useSSL=true
username: root
password: xxxx
<!--JWT依赖包 -->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>2.0.7</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
工具类
@Component
public class JwtUtil {
// 有效期
private static final long JWT_EXPIRE = 30*60*1000L; //半小时
// 令牌秘钥
private static final String JWT_KEY = "123456";
public String createToken(Object data){
// 当前时间
long currentTime = System.currentTimeMillis();
// 过期时间
long expTime = currentTime+JWT_EXPIRE;
// 构建jwt
JwtBuilder builder = Jwts.builder()
.setId(UUID.randomUUID()+"")
.setSubject(JSON.toJSONString(data))
.setIssuer("system")
.setIssuedAt(new Date(currentTime))
.signWith(SignatureAlgorithm.HS256, encodeSecret(JWT_KEY))
.setExpiration(new Date(expTime));
return builder.compact();
}
private SecretKey encodeSecret(String key){
byte[] encode = Base64.getEncoder().encode(key.getBytes());
SecretKeySpec aes = new SecretKeySpec(encode, 0, encode.length, "AES");
return aes;
}
public Claims parseToken(String token){
Claims body = Jwts.parser()
.setSigningKey(encodeSecret(JWT_KEY))
.parseClaimsJws(token)
.getBody();
return body;
}
public <T> T parseToken(String token,Class<T> clazz){
Claims body = Jwts.parser()
.setSigningKey(encodeSecret(JWT_KEY))
.parseClaimsJws(token)
.getBody();
return JSON.parseObject(body.getSubject(),clazz);
}
}
pom文件
<!-- 连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.18</version>
</dependency>