发表于: 2018-01-04 20:41:00
2 549
今天做的内容:
(1).远程DB连接服务器的数据库,进行crud
首先,对服务器进行布置,这里就不详细说明了,直接附上连接:
https://www.cnblogs.com/a2211009/p/4265225.html
http://blog.csdn.net/qq_24091555/article/details/76039475
http://www.shuchengxian.com/article/info/id/204.html
http://blog.csdn.net/lorin_ag/article/details/78601191
然后在服务器端mysql数据库里创建表,然后将本地db配置中url里的ip改成服务器公网ip,测试,成功。
(2). 进行了一些性能测试,结果如下:
其中触发器方案是将创建时间和更新时间放在数据库中用动态timestamp函数和触发器完成,对应的建表方案:
经过测试,触发器方案明显地劣于原来的方案,而且给数据库添加压力,而且不方便管理,所以很不友好,就不在这里展开了,以后有需要的话可以再改良...
(3). 编写main函数并添加自定义日志内容
public static void main(String[] args) throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
StudentMapper studentMapper = ctx.getBean(StudentMapper.class);
//加日志
Logger logger = Logger.getLogger(Main.class);
//插入数据
long start_insert = System.currentTimeMillis();
Student student_insert = new Student(
"曲艳行", "3169119846", "JAVA工程师",
"11月18日--11月22日", "燕山大学", "java-",
"http://www.jnshu.com/daily/40038?dailyType=others&total=8&page=1&uid=18143&sort=0&orderBy=3",
"努力努力再努力!", "郑州分院王鹏举", "知乎");
student_insert.setCreateAt(System.currentTimeMillis());
for (int i = 0; i < 10000; i++) {
student_insert.setNum("java-" + i);
logger.debug(studentMapper.insertStudent(student_insert) + " : " + student_insert.getId());
}
long end_insert = System.currentTimeMillis();
logger.info("The total time spent on selecting is: " + (end_insert - start_insert) + "ms.");
//删除
logger.info("Delete is successful or not: " + studentMapper.deleteStudent(1L));
//使用学号模糊查询
Student student_read1 = studentMapper.findByNum("java-5000");
logger.info("Query with num:\n" + student_read1);
//主键查询
Student student_read2 = studentMapper.findById(2L);
logger.info("Query with the primary key:\n" + student_read2);
//逐条更新
long start_update = System.currentTimeMillis();
Student student_update = studentMapper.findById(5000L);
student_update.setUpdateAt(System.currentTimeMillis());
student_update.setDeclaration("老大最帅");
for (long i = 2; i < 10000; i++) {
logger.debug(studentMapper.updateStudent(student_update));
}
long end_update = System.currentTimeMillis();
logger.info("The total time spent on updating is: " + (end_update - start_update) + "ms.");
//清空表格,为下次测试做准备
logger.info("Cleaning is successful or not: " + studentMapper.truncateTable());
}
经过测试,成功执行,并输出到设置的本地日志目录下。这里要说明的是,为了增强功能,我用动态SQL改写了update的实现方法:
其中特别有趣的是,如果update传入的Student对象的id值为null,将会更新整列的内容,而且执行效率相当地快,具体的时间可以看第二点中的图。
此外,为了方便main函数的调试,避免这次测试的内容对下次造成影响,我在DAO里添加了清空整表的方法:
public boolean truncateTable() throws Exception;
对应的xml语句:
<update id="truncateTable">
truncate table students
</update>
我把这个方法加在main方法的最下面,经测试,成功执行。
明天要做的事情:尝试进行异常处理,有空再搞搞服务器。
今天的收获:对任务一的理解又深了一步。
评论