使用mybatis实现批量插入文件路径和时间等
1.基础数据库插入语句:
insert into table ([列名],[列名]) values ([列值],[列值]));
或:
insert into table values ([列值],[列值]));
mybatis中mapper.xml的写法:
<--(id, date, pathname)分别是列名。-->
<insert id="insert">
insert into table(id, date, pathname)
values (#{id,jdbcType=INTEGER}, #{date,jdbcType=DATE}, # {pathname,jdbcType=VARCHAR})
</insert>
但插入多条语句时需要使用 foreach语句,首先来温习一下foreach标签的基本语法,和参数。
<foreach collection="**" item="*" index="index" separator=",">
(#{*.name},#{*.sex},#{*.address})
</foreach>
foreach元素的属性主要有 collection,item,separator,index,open,close。
- collection:指定要遍历的集合。表示传入过来的参数的数据类型。该属性是必须指定的,要做 foreach 的对象。在使用foreach的时候最关键的也是最容易出错的就是collection属性。在不同情况下,该属性的值是不一样的,主要有以下3种情况:传参为List,传参为Array,传参为Map。分别对应:list,array,map。
- item:表示集合中每一个元素进行迭代时的别名。将当前遍历出的元素赋值给指定的变量,然后用#{变量名},就能取出变量的值,也就是当前遍历出的元素。
- separator:表示在每次进行迭代之间以什么符号作为分隔符
- index:索引。index指定一个名字,用于表示在迭代过程中,每次迭代到的位置。遍历list的时候index就是索引,遍历map的时候index表示的就是map的key,item就是map的值。
- open表示该语句以什么开始。
- close表示以什么结束。
我使用的是List<对象>类型,对象里面包含 id,日期,文件目录三个字段。ignore是为了避免重复插入。
mapper中
<insert id="insertPath">
insert ignore into paths(id,date,pathname)
values
<foreach collection="list" item="paths" index="id" separator=",">
(#{paths.Id,jdbcType=INTEGER}, #{paths.Date,jdbcType=DATE}, #{paths.Pathname,jdbcType=VARCHAR})
</foreach>
</insert>
pathmapper中:
public interface PathMapper {
int insertPath(@Param("list")List<Paths> pathlist);
}
注意@Param注解
我提前将自己想要的数据封装成了Paths类型()这是自己定义的类名。
pathServiceImpl:
@Service
public class pathServiceImpl implements pathService {
@Autowired
private PathMapper pathMapper;
@Transactional
@Override
public int getPathsByConditionLike(List<Paths> pathlist) {
int i = pathMapper.insertPath(pathlist);
return i;
}
}
pathService
public interface pathService {
int getPathsByConditionLike(@Param("list") List<Paths> paths);
}
Controler:
由于我把获取文件路径和将文件路径的日期分割都放到了这里面,然后放入到Paths对象里面,不知道做的是否规范。
@RestController
public class readpath {
//定义一个全局变量,来存储递归函数内的文件路径
static int len = 50000;
static int num = 0;
static String[] paths = new String[len];
static Date[] datel = new Date[len];
static List<Paths> pathlists = new ArrayList<>();
private static Paths pathsl = null;
@Autowired
private pathService pathService;
@RequestMapping("/pathsql")
//将数据进行封装成list
public void format() {
File file = new File("E:/TiffData/市数据");
paths = fileList(file, 0, paths);
//设置日期的格式
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
int ind = 0;
for (int i = 0; i < file.length(); i++) {
if (paths[i] != null) {
if (paths[i].length() - paths[i].lastIndexOf("/") > 8 && paths[i].lastIndexOf("_") + 1 > 0) {
String q = paths[i].substring(paths[i].lastIndexOf("\\") + 1, paths[i].lastIndexOf("_"));
String dates = q.substring(q.indexOf("_") + 1, q.lastIndexOf("_"));
String fmt = "(\\d{4})(\\d{2})(\\d{2})";
dates = dates.replaceAll(fmt, "$1-$2-$3");
Date date = null;
try {
date = formatter.parse(dates);
datel[i] = date;
} catch (ParseException e) {
e.printStackTrace();
}
//System.out.println(formatter.format(date));
} else {
datel[i] = null;
}
pathsl = new Paths(ind+1, datel[i], paths[i]);
pathlists.add(pathsl);
ind++;
//System.out.println(datel[i]);
//System.out.println(paths[i]);
}
}
pathlists.forEach(pl -> {
System.out.println(pl.getId() + "," + pl.getDate() + "," + pl.getPathname());
});
int insertnum = pathService.getPathsByConditionLike(pathlists);
System.out.println(insertnum);
}
//获取文件路径
public static String[] fileList(File file, int node, String paths[]) {
node++;
File[] files = file.listFiles();
if (files != null) {
for (int n = 0; n < files.length; n++) {
for (int i = 0; i < node; i++) {
//判断出为子目录
if (i == node - 2) {
paths[num] = files[n].getAbsolutePath();
num++;
}
}
//+file1.getName()仅获取文件名
//System.out.println( files[n].getAbsolutePath());
fileList(files[n], node, paths);
}
}
return paths;
}
}
代码至此就告一段路。
值得注意的是:1.mapper中插入语句的写法;2.foreach的参数,以及传入类型;3.数据封装时添加值;4.建立数据库的主键设置;5.java类型的值和数据库中的值类型的值是否相符合。-------------java.util.Date和java.sql.Date在mybatis的插入时会自动转换。
插入后数据库中所示: