目录
Java后台集成mybatis-plus
1、添加依赖
2、修改配置文件application.yml或application.properties
3、resources下添加mybatis-config文件,路径需要和mybatis plus配置文件中的configLocation的路径一致
4、添加MybatisPlusConfig类
java后台实现文件上传代码
添加一个文件上传实体类Wdzl
添加mapper
添加service
添加controller
前端页面代码
HTML部分
js部分
成果展示
com.baomidou
mybatis-plus-boot-starter
3.4.2
mybatis-plus: # 搜索指定包别名 typeAliasesPackage: com.xxx.**.domain # 配置mapper的扫描,找到所有的mapper.xml映射文件 mapperLocations: classpath*:mybatis/mapper/**/*Mapper.xml # 指定Mapper XML文件的位置,使用classpath通配符指定路径。 #mapper-locations: classpath*:/mapper/**/*.xml # 指定实体类的包路径,用于自动扫描并注册类型别名。 #type-aliases-package: com.xxx.*.domian configLocation: classpath:mybatis/mybatis-conf.xml # 全局配置 global-config: db-config: #id-type: ID_WORKER # 主键ID的生成策略,可选值包括:AUTO、NONE、INPUT、ID_WORKER、UUID id-type: AUTO #机器 ID 部分(影响雪花ID) workerId: 1 #数据标识 ID 部分(影响雪花ID)(workerId 和 datacenterId 一起配置才能重新初始化 Sequence) datacenterId: 18 # 字段验证策略,可选值包括:not_null、not_empty、default field-strategy: not_empty #驼峰下划线转换(将数据库字段的下划线命名规则转换为 Java 实体类属性的驼峰命名规则) db-column-underline: true #刷新mapper 调试神器 refresh-mapper: true #refresh-mapper在生产环境中应该设置为false,以避免不必要的性能开销 #数据库大写下划线转换 #capital-mode: true #序列接口实现类配置 #key-generator: com.baomidou.springboot.xxx #逻辑删除配置(下面3个配置) logic-delete-field: deleted # 逻辑删除字段名 logic-delete-value: 1 # 逻辑删除字段的值表示已删除 logic-not-delete-value: 0 # 逻辑删除字段的值表示未删除 #自定义SQL注入器 #sql-injector: com.baomidou.mybatisplus.mapper.LogicSqlInjector #自定义填充策略接口实现 #meta-object-handler: com.baomidou.springboot.xxx configuration: # 将 Java 实体类属性的驼峰命名规则转换为数据库字段的下划线命名规则 map-underscore-to-camel-case: true # 是否开启二级缓存。 cache-enabled: false # 这个配置会将执行的sql打印出来,在开发或测试的时候可以用 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
@EnableTransactionManagement(proxyTargetClass = true) @Configuration public class MybatisPlusConfig { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); // 分页插件 interceptor.addInnerInterceptor(paginationInnerInterceptor()); // 乐观锁插件 interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor()); // 阻断插件 interceptor.addInnerInterceptor(blockAttackInnerInterceptor()); return interceptor; } /** * 分页插件,自动识别数据库类型 https://baomidou.com/guide/interceptor-pagination.html */ public PaginationInnerInterceptor paginationInnerInterceptor() { PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(); // 设置数据库类型为mysql paginationInnerInterceptor.setDbType(DbType.MYSQL); // 设置最大单页限制数量,默认 500 条,-1 不受限制 paginationInnerInterceptor.setMaxLimit(-1L); return paginationInnerInterceptor; } /** * 乐观锁插件 https://baomidou.com/guide/interceptor-optimistic-locker.html */ public OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor() { return new OptimisticLockerInnerInterceptor(); } /** * 如果是对全表的删除或更新操作,就会终止该操作 https://baomidou.com/guide/interceptor-block-attack.html */ public BlockAttackInnerInterceptor blockAttackInnerInterceptor() { return new BlockAttackInnerInterceptor(); } }
@Data public class WdzL { private String uploadIndex;//上传文件索引 private String id; private String dxId; private String mc; private String lj; private String scrId; private String scr_name; @JsonFormat(pattern="yyyy-MM-dd HH:mm",timezone="GMT+8") //返回时间格式 private Date scsj; private String wdlx; private String wdlx_name; private String bz; public WdzL(String uploadIndex,String id, String dxId, String mc, String lj, String scrId,Date scsj,String bz,String wdlx) { super(); this.uploadIndex = uploadIndex; this.id = id; this.dxId = dxId; this.mc = mc; this.lj = lj; this.scrId = scrId; this.scsj = scsj; this.wdlx = wdlx; this.bz = bz; } }
@Repository public interface WdzlMapper extends BaseMapper{ List getWdzlBydxId(@Param("dxId") String dxId); void insertWdzl(@Param("wdzl") WdzL wdzL); }
@Service public class WdzlService{ @Resource WdzlMapper wdzlMapper; public JSONObject insertWdzl(String fileList, String dxId, String scrId, String fileDir, String dirName, MultipartFile[] uploadFiles) throws Exception { JSONObject result = new JSONObject(); result.put("code", "error"); System.out.println("--------------dxId--------------:" + dxId); if (fileList == null || "".equals(fileList)) { result.put("msg", "内容不可为空"); return result; } if (uploadFiles == null || uploadFiles.length == 0) { result.put("msg", "请先上传文件"); return result; } JSONObject object = JSON.parseObject(fileList); if (object == null || object.size() == 0) { result.put("msg", "内容不可为空"); return result; } if (uploadFiles.length != object.size()) { result.put("msg", "表格条数与文件不匹配"); return result; } String name = ""; ListwdzLs = wdzlMapper.getWdzlBydxId(dxId); int size = object.size(); Date scsj = new Date(); ArrayList list = new ArrayList (); for (int i = 0; i < size; i++) { JSONObject jsonOb = object.getJSONObject(i + ""); String uploadIndex = jsonOb.getString("uploadIndex"); String wjmc = jsonOb.getString("wdmc"); String dalx = jsonOb.getString("wdlx"); String bz = jsonOb.getString("bz"); list.add(new WdzL(uploadIndex, UUID.randomUUID().toString(), dxId, wjmc, "", scrId, scsj, bz, dalx)); } if(!wdzLs.isEmpty() && !list.isEmpty()){ for(WdzL a:wdzLs){ for(WdzL b:list){ if(a.getMc().equals(b.getMc())){ name += "【"+a.getMc()+"】"; } } } if(!"".equals(name)) { result.put("msg", "库中存在相同文件,重复文件名:"+name); return result; } } // 文件上传 String classes_path = ClassUtils.getDefaultClassLoader().getResource("").getPath(); classes_path = URLDecoder.decode(classes_path, "utf-8"); File eclipsePath = new File(classes_path + File.separator + "static"); if(!eclipsePath.exists()) { eclipsePath.mkdirs(); } String rootPath = eclipsePath.getPath(); /* String filePath = "业务"+ File.separator + dxId + File.separator + "文档资料"; */ String filePath = fileDir + File.separator + dxId + File.separator + dirName; File directory = new File(rootPath + File.separator + filePath); if (!directory.exists()) { directory.mkdirs(); } for (int j = 0; j < uploadFiles.length; j++) { MultipartFile file = uploadFiles[j]; if (file != null && !file.isEmpty()) { WdzL wdzl = list.get(j); String wjmc = wdzl.getMc();// 用户填写的文件名称 String originalName = file.getOriginalFilename();// 文件原始名称 String filename = originalName; if (wjmc != null && !"".equals(wjmc)) { // 如果两者名称不相同 if (!originalName.equals(wjmc)) { String originalNameSuffix = originalName.substring(originalName.lastIndexOf(".")).toLowerCase();// 获取后缀 String wjmcPrefix = wjmc.substring(0, wjmc.lastIndexOf("."));// 获取前缀 filename = wjmcPrefix + originalNameSuffix; wdzl.setMc(filename); } } File newfile = new File(rootPath + File.separator + filePath + File.separator + filename); file.transferTo(newfile); // 上传源 wdzl.setLj("/" + fileDir + "/" + dxId + "/" + dirName + "/" + filename); wdzlMapper.insertWdzl(wdzl); result.put("code", "ok"); } else { result.put("msg", "存在大小是0kb的文件,不支持上传0kb的文件"); return result; } } return result; } }
@RestController @RequestMapping("wdzl") public class WdzlController { @Autowired WdzlService wdzlService; @RequestMapping("insertWdzl") public JSONObject insertWdzl(String fileList, String dxId, String scrId, String fileDir, String dirName, @RequestParam(value="uploadFiles",required=false) MultipartFile[] uploadFiles) throws Exception{ return wdzlService.insertWdzl(fileList, dxId, scrId,fileDir,dirName, uploadFiles); } }
洪福湿地 XXXXX问题反馈表
个人基本信息问题描述0/200图片上传及预览
/8
视频上传
项目下
数据库中