发表于: 2018-01-07 22:51:44
2 568
今天完成的事情
1.新电脑到了,配置了jdk,maven,eclipse,idea,mysql,xshell
2.重新写了好多代码
(1)重写jdbcTemplate,接下来说说重写过程中踩到的坑:
《a》beans.xml文件的配置,第一个问题就是系统提示找不到beans.xml,童师兄教我把beans放到一个新建的resource文件夹下,然后指定它为源目录解决了这个问题。
《b》重写toString方法,我才知道这么简便的方法:选择想要生成toString方法的位置,右键选择生成,选toString;
《c》我以前写的测试类和junit单元测试只差了一个@Test
《d》还给这个重写的加上了log4j
《e》写了junit单元测试
下面是项目截图
接下来的老生常谈的不传了,传一下实现的log4j
package com.spring;
import javafx.application.Application;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class mainTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
StudentJdbcTemplate studentJdbcTemplate = (StudentJdbcTemplate) context.getBean("studentJdbcTemplate");
//插入数据
//studentJdbcTemplate.create(null,"张三",110,"css","全球数据库","haha",2020,2021);
//遍历表中所有数据
Logger logger = Logger.getLogger(mainTest.class);
BasicConfigurator.configure();
logger.setLevel(Level.DEBUG);
List<Student> students = studentJdbcTemplate.listStudents();
for (Student record : students) {
// System.out.print("ID : " + record.getId() );
// System.out.print(", Name : " + record.getName() );
// System.out.print(", qq : " + record.getQq());
// System.out.print(", school : " + record.getSchool());
// System.out.print(", type : " + record.getType());
// System.out.print(", say : " + record.getSay());
// System.out.print(", create_at : " + record.getCreate_at());
// System.out.println(", update_at : " + record.getUpdate_at());
logger.debug(record);
}
// //更新数据
// studentJdbcTemplate.update(6,115);
// //查询一条数据
// System.out.println(studentJdbcTemplate.getStudent(1));
}
}
没有注释掉的就是log4j,下面是运行结果:
(2)重写mybatis
《a》提交事物必不可少,否则不会有任何执行动作
《b》调用insert的时候,注意把所有字段的属性都设置好:我这里因为有后面的测试,所以注释掉了
Student s = new Student();
// s.setName("xiao");
// s.setQq(111);
// s.setSchool("s77星云");
// s.setType("jj");
// s.setSay("xixixixi");
// s.setCreate_at(1988);
// s.setUpdate_at(1996);
// session.insert("addStudent",s);
《c》别名的设置,别名指定到包名,我是因为指定到了类上碰到了一系列的错误
《d》还要注意一些特殊的地方,比如查询单个数据:
<mapper namespace="com.student">
<insert id="addStudent" parameterType="Student">
insert into db(id,name,qq,school,type,say,create_at,update_at) values(#{id},#{name},#{qq},#{school},#{type},#{say},#{create_at},#{update_at})
</insert>
<delete id="deleteStudent" parameterType="Student">
delete from db where id = #{id}
</delete>
<select id="getStudent" parameterType="_int" resultType="Student">
select * from db where id = #{id}
</select>
<update id="updateStudent" parameterType="Student">
update db set name = #{name} where id = #{id}
</update>
<select id="listStudent" resultType="Student">
select * from db
</select>
</mapper>
要注意到多出的“_int”
《e》log4j的问题,因为导入的包太新,后来重新导入出现的错误代码:
Exception in thread "main" java.lang.NoSuchMethodError: org.apache.log4j.Logger.isTraceEnabled()Z
原因是之前导入了高版本的jar包没有删除,删除了就好了
《f》指定日志properties文件,是执行操作,需要放在main函数里面,之前没有注意到
《g》一开始用绝对位置指明properties,后来师兄点拨几句,放在了resource里面,不用担心工作环境变化,而且这样做不用指明,会自动加载。
接下来传一下代码,下面是运行结果,添加了log4j
产生结果的代码:同样是为了方便测试,一些已经注释掉
package com.student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Locale;
public class mainTest {
static Logger logger = Logger.getLogger(mainTest.class);
public static void main(String[] args) throws IOException {
//放在resource文件夹下,会自动加载properties,不用再进行下面的指定
// PropertyConfigurator.configure("log4j.properties");
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sqlSessionFactory.openSession();
//下面是增加数据的代码
Student s = new Student();
// s.setName("xiao");
// s.setQq(111);
// s.setSchool("s77星云");
// s.setType("jj");
// s.setSay("xixixixi");
// s.setCreate_at(1988);
// s.setUpdate_at(1996);
// session.insert("addStudent",s);
//下面是删除的代码
// s.setId(9);
// session.delete("deleteStudent",s);
//下面是查询单条数据
// Student s1 = session.selectOne("getStudent",3);
// System.out.println(s1);
//修改指定数据
// Student s2 = session.selectOne("getStudent",3);
// s2.setName("萧忆情");
// session.update("updateStudent",s2);
//试试全部列出来
listAll(session);
//提交事务,关闭会议
session.commit();
session.close();
}
private static void listAll(SqlSession session) {
List<Student> ss = session.selectList("listStudent");
for (Student s:ss){
logger.error(s);
}
}
}
另外需要注意的点是:
<insert id="addStudent" parameterType="Student">
insert into db(id,name,qq,school,type,say,create_at,update_at) values(#{id},#{name},#{qq},#{school},#{type},#{say},#{create_at},#{update_at})
</insert>
insert语句的操作,需要牢记
(3)重新插入百万级数据
童师兄建议我重新做一下这个,因为我之前的插入比较慢
之前100万数据用时37min左右,这次的方法100万数据仅仅1分钟
package Cha;
/**
*
* 太神奇了 100万一分钟左右;
* 提前修改数据库的字符集类型:
* 加上characterEncoding=UTF-8
*
*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Random;
public class cha {
public static void main(String[] args) {
String url = "jdbc:mysql://47.**.***.36:3306/student?characterEncoding=UTF-8&rewriteBatchedStatements=true";
String user = "root";
String password = "********";
Connection conn = null;
PreparedStatement pstm =null;
ResultSet rt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, user, password);
String sql = "INSERT INTO bbe VALUES(?,?,?,?,?,?,?)";
pstm = conn.prepareStatement(sql);
conn.setAutoCommit(false);
Long startTime = System.currentTimeMillis();
for (int i = 1; i <= 1000000; i++) {
pstm.setInt(1, 10);
pstm.setString(2, "就是我");
pstm.setInt(3, 555666);
pstm.setString(4, "css");
pstm.setString(5, "来打我呀");
pstm.setInt(6, 2068);
pstm.setInt(7, 1943);
pstm.addBatch();
}
pstm.executeBatch();
conn.commit();
Long endTime = System.currentTimeMillis();
System.out.println("OK,用时:" + (endTime - startTime));
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}finally{
if(pstm!=null){
try {
pstm.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
}
}
上面就是代码了,一开始插入的时候呢,插进的字符串都是?,百度了一下,这是解决方案截图:
如上图所示,要把character_set_server通过倒数第二行的操作设置成utf8,然后还有注意我代码标红的部分一定要写出来,否则还是不行;这两步做完之后,再次插入就是字符串了。
2.今天遇到的问题
以上,所有的总结都来源于问题
3.今天的收货
今天学习特别开心,周围都是大佬,贼爽贼刺激,解决问题效率特别高,而且氛围真的好。
4.明天计划的事情
将项目代码上传,然后进行任务一验收。
最后,麻烦师兄审核
评论