发表于: 2020-06-14 23:33:02
1 1537
今天完成的事:
1.整理完jdbcTemplate的代码和mybatis使用配置文件和注解进行增删改查的代码。
明天计划的事:
1.spring和mybaties整合进行增删改查。
2.给数据库里插入100万条数据,对比建索引和不建索引的效率查别。再插入3000万条数据,然后是2亿条。
遇到的问题:在大的maven项目里面建立几个小module,如果大项目的pom没有配置这几个module进去,小module里面的所有相关配置文件都会找不到。
如果不小心经过把module改名之后又删除会导致这里面配置的一个module会不见,如果后面不关注这个大的maven项目的pom,运行测试里面被改的module会报找不到配置文件的错。 日常被maven秀啊。
收获:
学会了mybatis的动态sql一些简单用法。
CONCAT(#{studentNumber},'%'")
使用mybaits进行模糊查询的关键字
mybaits的整理好的代码
项目目录
增删改查具体实现代码
配置文件的
<mapper namespace="Student">
<!-- column:数据库的列名 property:要映射到实体类的字段名-->
<resultMap id="studentMap" type="Student">
<!--要映射到实例类的主键ID-->
<id column="id" property="id"/>
<!--普通属性-->
<result column="student_name" property="name"/>
<result column="enter_time" property="enterTime"/>
<result column="student_number" property="studentNumber"/>
<result column="qq" property="qqNumber"/>
<result column="school" property="school"/>
<result column="study_type" property="type"/>
<result column="log_link" property="logLink"/>
<result column="slogan" property="slogan"/>
<result column="brother" property="brother"/>
</resultMap>
<!--根据id查询-->
<select id="selectById" parameterType="java.lang.Integer" resultMap="studentMap">
select *from student_task1 where id=#{id}
</select>
<!--插入学生数据-->
<insert id="insertStudent" parameterType="Student">
insert into student_task1(
student_name,enter_time,qq,school,student_number,study_type,log_link,slogan,brother,create_at,update_at)
values (
#{name},#{enterTime},#{qqNumber},#{school},#{studentNumber},#{type},#{logLink},#{slogan},#{brother},#{createTime},#{updateTime})
</insert>
<!--根据姓名模糊查询-->
<select id="selectByName" parameterType="java.lang.String" resultMap="studentMap">
select *from student_task1 where student_name like CONCAT(#{name},'%')
</select>
<!--根据学号模糊查询-->
<select id="selectByNumber" parameterType="java.lang.String" resultMap="studentMap">
select *from student_task1 where student_number like CONCAT(#{studentNumber},'%');
</select>
<!--根据id更新多种数据-->
<update id="updateById" parameterType="Student">
update student_task1
<trim prefix="set" suffixOverrides=",">
<if test="studentNumber!=null">student_number=#{studentNumber}</if>
<if test="school!=null">school=#{school}</if>
<if test="qqNumber!=null">qq=#{qqNumber}</if>
<if test="type!=null">study_type=#{type}</if>
<if test="logLink!=null">log_link=#{logLink}</if>
<if test="slogan!=null">slogan=#{slogan}</if>
<if test="brother!=null">brother=#{brother}</if>
<if test="enterTime!=null">enter_time=#{enterTime}</if>
<if test="createTime!=null">create_at=#{createTime}</if>
<if test="name!=null">student_name=#{name}</if>
</trim>
where id=#{id}
<!--根据id更新数据-->
</update>
<delete id="deleteById" parameterType="java.lang.Integer">
delete from student_task1 where id=#{id}
</delete>
</mapper>
注解的
package com.jnshu.dao;
import com.jnshu.pojo.Student;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.util.List;
/**
* @version 1.0
* @Author 韦延伦
* @Description StudentDao接口,直接可以添加mybatis注解实现功能
* @Date 2020/6/14 21:01
**/
public interface StudentDao {
/**
* @return void
* @Author 韦延伦
* @Description 插入数据接口
* @Date 2020/6/14 21:02
* @Param [student]
**/
@Insert("insert into student_task1(\n" +
" student_name,enter_time,qq,school,student_number,study_type,log_link,slogan,brother,create_at,update_at)\n" +
" values (\n" +
" #{name},#{enterTime},#{qqNumber},#{school},#{studentNumber},#{type},#{logLink},#{slogan},#{brother},#{createTime},#{updateTime})")
void add(Student student);
/**
* @return void
* @Author 韦延伦
* @Description 根据id删除学生接口
* @Date 2020/6/14 21:03
* @Param [id]
**/
@Delete(" delete from student_task1 where id=#{id}")
void deleteById(int id);
/**
* @return void
* @Author 韦延伦
* @Description 根据id更新学生接口
* @Date 2020/6/14 21:03
* @Param [student]
**/
@Update("update student_task1 set student_name=#{name},update_at=#{updateTime} where id=#{id};")
void updateById(Student student);
/**
* @return com.jnshu.pojo.Student
* @Author 韦延伦
* @Description 根据id查询学生接口
* @Date 2020/6/14 21:03
* @Param [id]
**/
@Select("select *from student_task1 where id=#{id}")
Student findById(int id);
/**
* @return java.util.List<com.jnshu.pojo.Student>
* @Author 韦延伦
* @Description 查询全部学生接口
* @Date 2020/6/14 21:04
* @Param []
**/
@Select(" select *FROM student_task1")
List<Student> findAll();
/**
* @return java.util.List<com.jnshu.pojo.Student>
* @Author 韦延伦
* @Description 根据姓名查询学生接口
* @Date 2020/6/14 21:05
* @Param [name]
**/
@Select("select *from student_task1 where student_name like CONCAT(#{name},'%')")
List<Student> findByName(String name);
/**
* @return com.jnshu.pojo.Student
* @Author 韦延伦
* @Description 根据学号查询学生接口
* @Date 2020/6/14 21:05
* @Param [number]
**/
@Select("select *from student_task1 where student_number like CONCAT(#{studentNumber},'%'")
Student findByNumber(String number);
}
注解测试类
/**
* @Author 韦延伦
* @Description Mybatis注解增删改查测试类
* @Date 2020/6/14 21:19
**/
public class TestAnnotation {
private SqlSession sqlSession;
private InputStream in;
private StudentDao studentDao;
/**
* @return void
* @Author 韦延伦
* @Description 用junit的@Before注解获取sqlSession
* @Date 2020/6/14 21:20
* @Param []
**/
@Before
public void init() throws IOException {
in = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(in);
sqlSession = factory.openSession();
studentDao = sqlSession.getMapper(StudentDao.class);
}
/**
* @return void
* @Author 韦延伦
* @Description 用@After提交数据和关闭SqlSession
* @Date 2020/6/14 21:22
* @Param []
**/
@After
public void close() throws IOException {
sqlSession.commit();
sqlSession.close();
in.close();
}
/**
* @return void
* @Author 韦延伦
* @Description 添加学生测试方法
* @Date 2020/6/14 21:22
* @Param []
**/
@Test
public void testAdd() {
Student student = new Student();
student.setName("韦延伦");
student.setSchool("贵航");
student.setEnterTime("20200605");
student.setStudentNumber("6601");
student.setType("JAVA工程师");
student.setQqNumber("939070310");
student.setSlogan("javaer");
student.setLogLink("http://www.jnshu.com/school/32609/daily");
student.setBrother("师兄");
for (int x = 0; x < 10; x++) {
long startTime = System.currentTimeMillis();
student.setCreateTime(startTime);
studentDao.add(student);
}
}
/**
* @return void
* @Author 韦延伦
* @Description 根据id删除学生测试方法
* @Date 2020/6/14 21:23
* @Param []
**/
@Test
public void testDelete() {
studentDao.deleteById(2);
}
/**
* @return void
* @Author 韦延伦
* @Description 根据id更新学生测试方法
* @Date 2020/6/14 21:23
* @Param []
**/
@Test
public void testUpdateById() {
Student s = new Student();
s.setName("延呈..");
s.setId(1);
long updateTime = System.currentTimeMillis();
s.setUpdateTime(updateTime);
studentDao.updateById(s);
}
/**
* @return void
* @Author 韦延伦
* @Description 根据id查找学生测试方法
* @Date 2020/6/14 21:24
* @Param []
**/
@Test
public void testFindById() {
Student student = studentDao.findById(1);
System.out.println("姓名:\t" + student.getName());
System.out.println("入学时间:\t" + student.getEnterTime());
System.out.println("学号:\t" + student.getStudentNumber());
System.out.println("QQ:\t " + student.getQqNumber());
System.out.println("学校:\t" + student.getSchool());
System.out.println("修真类型:\t" + student.getType());
System.out.println("日报链接:\t" + student.getLogLink());
System.out.println("立愿:\t" + student.getSlogan());
System.out.println("辅导师兄:\t" + student.getBrother());
}
/**
* @return void
* @Author 韦延伦
* @Description 查询所有学生测试
* @Date 2020/6/14 21:25
* @Param []
**/
@Test
public void testFindAll() {
List<Student> list = studentDao.findAll();
for (Student student : list) {
System.out.println("姓名:\t" + student.getName());
System.out.println("入学时间:\t" + student.getEnterTime());
System.out.println("学号:\t" + student.getStudentNumber());
System.out.println("QQ:\t " + student.getQqNumber());
System.out.println("学校:\t" + student.getSchool());
System.out.println("修真类型:\t" + student.getType());
System.out.println("日报链接:\t" + student.getLogLink());
System.out.println("立愿:\t" + student.getSlogan());
System.out.println("辅导师兄:\t" + student.getBrother());
}
}
/**
* @return void
* @Author 韦延伦
* @Description 根据姓名查找学生
* @Date 2020/6/14 21:25
* @Param []
**/
@Test
public void testFindByName() {
List<Student> studentList = studentDao.findByName("魏延磊");
for (Student student : studentList) {
System.out.println("姓名:\t" + student.getName());
System.out.println("入学时间:\t" + student.getEnterTime());
System.out.println("学号:\t" + student.getStudentNumber());
System.out.println("QQ:\t " + student.getQqNumber());
System.out.println("学校:\t" + student.getSchool());
System.out.println("修真类型:\t" + student.getType());
System.out.println("日报链接:\t" + student.getLogLink());
System.out.println("立愿:\t" + student.getSlogan());
System.out.println("辅导师兄:\t" + student.getBrother());
}
}
/**
* @Author 韦延伦
* @Description 根据学号查询学生的方法
* @Date 2020/6/14 23:06
* @Param []
* @return void
**/
@Test
public void testFindByNumber() {
Student student = studentDao.findByNumber("6601");
System.out.println("姓名:\t" + student.getName());
System.out.println("入学时间:\t" + student.getEnterTime());
System.out.println("学号:\t" + student.getStudentNumber());
System.out.println("QQ:\t " + student.getQqNumber());
System.out.println("学校:\t" + student.getSchool());
System.out.println("修真类型:\t" + student.getType());
System.out.println("日报链接:\t" + student.getLogLink());
System.out.println("立愿:\t" + student.getSlogan());
System.out.println("辅导师兄:\t" + student.getBrother());
}
}
配置文件测试类
/**
* @Author 韦延伦
* @Description mybatis用配置文件进行增删改查的测试类
* @Date 2020/6/14 23:04
* @Param
* @return
**/
public class TestXml {
private SqlSession sqlSession;
private InputStream in;
@Before
public void init() throws IOException {
in = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(in);
sqlSession = factory.openSession();
}
@After
public void close() throws IOException {
sqlSession.commit();
sqlSession.close();
in.close();
}
/**
* @return void
* @Author 韦延伦
* @Description 测试按id查询学生方法
* @Date 2020/6/14 23:01
* @Param []
**/
@Test
public void testSelectById() {
Student student = sqlSession.selectOne("selectById", 2);
System.out.println("姓名:\t" + student.getName());
System.out.println("入学时间:\t" + student.getEnterTime());
System.out.println("学号:\t" + student.getStudentNumber());
System.out.println("QQ:\t " + student.getQqNumber());
System.out.println("学校:\t" + student.getSchool());
System.out.println("修真类型:\t" + student.getType());
System.out.println("日报链接:\t" + student.getLogLink());
System.out.println("立愿:\t" + student.getSlogan());
System.out.println("辅导师兄:\t" + student.getBrother());
}
/**
* @return void
* @Author 韦延伦
* @Description 测试插入学生方法
* @Date 2020/6/14 23:01
* @Param []
**/
@Test
public void testInsert() {
Student student = new Student();
student.setName("韦延伦");
student.setEnterTime("20200605");
student.setQqNumber("9392");
student.setType("java");
student.setStudentNumber("6603");
student.setSlogan("加油思密达 ");
student.setLogLink("www.jnshu.com");
student.setSchool("修真院");
student.setUpdateTime(System.currentTimeMillis());
student.setCreateTime(System.currentTimeMillis());
student.setBrother("invild s");
sqlSession.insert("insertStudent", student);
}
/**
* @return void
* @Author 韦延伦
* @Description 测试根据名字查找学生方法
* @Date 2020/6/14 23:02
* @Param []
**/
@Test
public void testSelectByName() {
Student student = sqlSession.selectOne("selectByName", "韦延伦");
System.out.println(student.getBrother());
}
/**
* @return void
* @Author 韦延伦
* @Description 测试按学号查询学生方法
* @Date 2020/6/14 23:02
* @Param []
**/
@Test
public void testSelectByNumber() {
Student student = sqlSession.selectOne("selectByNumber", "6601");
System.out.println("姓名:\t" + student.getName());
System.out.println("入学时间:\t" + student.getEnterTime());
System.out.println("学号:\t" + student.getStudentNumber());
System.out.println("QQ:\t " + student.getQqNumber());
System.out.println("学校:\t" + student.getSchool());
System.out.println("修真类型:\t" + student.getType());
System.out.println("日报链接:\t" + student.getLogLink());
System.out.println("立愿:\t" + student.getSlogan());
System.out.println("辅导师兄:\t" + student.getBrother());
}
@Test
public void testUpdateById() {
Student s = new Student();
s.setId(3);
s.setStudentNumber("6601");
s.setName("魏延磊");
sqlSession.update("updateById", s);
}
@Test
public void testDeleteById() {
sqlSession.delete("deleteById", 3);
}
}
评论