前言:
最近也是遇到了这个问题,最后成功解决。前期在网上搜索了很多内容,发现都很复杂而且都不尽相同,况且不同的开发软件,不同的配置都会增加我们参考时的麻烦,这里为大家放上了更加简便的方法!
开发软件: SpringToolSuite4(个人认为比eclipse更好开发)
正文:
1.1: 如图,前端上传文件。
![](https://img-blog.csdnimg.cn/13710054f9424b10a17b2b2d0e20c746.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAU2lfPz8gPw==,size_20,color_FFFFFF,t_70,g_se,x_16)
1.2:@Controller
获取前端输入的文件的url,存储在path中,通过实现UpdateMapper接口将文件路径传入。
@Resource注解:获取mapper中的 对象
/*
*通过注解方式进行页面的转换
*这里通过HttpServletRequest中getParameter方法获取提交表单中name的值
*/
@Resource
UpdateMapper updateMapper;
@RequestMapping(value = "/toMyself")
public String toMyself(HttpServletRequest req) {
String path1 = req.getParameter("filename1");
String path2 = req.getParameter("filename2");
String path3 = req.getParameter("filename3");
HttpSession file = req.getSession(true);//若存在会话则返回该会话,否则新建一个会话。
file.setAttribute("file1", path1);
int row = updateMapper.update(path1);
System.out.println(row);//输出更新的行数
return "A1A01WH01A01_ETCカード発行依頼申請";
}
1.3:接口
在@Mapper注解中,写入要进行的操作。
@Mapper
/*
* 添加了@Mapper注解用来表示该接口类的实现类对象交给mybatis底层创建
* 然后交由spring框架处理,该接口中的方法要在mybatis中实现
*
* @Param注解的作用是给参数命名,参数命名后就能根据名字得到 参数值,
* 正确的将参数传入sql语句中{一般通过#{}的方式,${}会有sql语句注入的问题}
*
*/
public interface UpdateMapper {
//两个接口
public int update(@Param("file1") String file1);
public int update1(@Param("name1") String name1,@Param("file1") String file1);
}
1.4 :Mybatis语句
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- 在mybatis中,映射文件中的namespace是用于绑定Dao接口的,即面向接口编程。
当你的namespace绑定接口后,你可以不用写接口实现类,mybatis会通过该绑定自动帮你找到对应要执行的SQL语句 -->
<!-- 在此xml文件中插入要写的SQL语句,此处为更新文件url到数据库 -->
<mapper namespace="cn.com.dhee.mapper.UpdateMapper">
<update id="update" parameterType="String">
update APP set HKS1='${file1}'
</update>
<update id="update1" parameterType="String">
update APP set SYOKBN='${name1}' , HKS1='${file1}'
</update>
</mapper>
最后,成功将文件url保存到数据库!
![](https://img-blog.csdnimg.cn/a491ab727f594a5eafe49ff607966427.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAU2lfPz8gPw==,size_20,color_FFFFFF,t_70,g_se,x_16)
生活本就不易,残酷的代码让我更加雪上加霜。 —— 角落里的宪笙