发表于: 2020-11-06 23:48:28
1 1376
一,今天完成的事情:
任务1 的 17 项
17. 使用Mybatis的时候注意理解为什么不需要Impl,注意遵守命名规范。
1,已经引入junit进入项目。程序结果,是用@Test做的。
2,完成项目验收要求的,根据学员名字,学号去查找报名贴的测试。
3,已经引入
log4j
让结果看的更清楚。
目前项目结构是:
代码如下:
package com.nicole.mybatis.entity;
public class Student {
private Long id;
private Long createAt;
private Long updateAt;
private String name;
private String qqNumber;
private String major;
private String estimatedTimeEnrollment;
private String school;
private String studentId;
private String logLink;
private String mentor;
private String wishes;
private String recommendedFrom;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getCreateAt() {
return createAt;
}
public void setCreateAt(Long createAt) {
this.createAt = createAt;
}
public Long getUpdateAt() {
return updateAt;
}
public void setUpdateAt(Long updateAt) {
this.updateAt = updateAt;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getQqNumber() {
return qqNumber;
}
public void setQqNumber(String qqNumber) {
this.qqNumber = qqNumber;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public String getEstimatedTimeEnrollment() {
return estimatedTimeEnrollment;
}
public void setEstimatedTimeEnrollment(String estimatedTimeEnrollment) {
this.estimatedTimeEnrollment = estimatedTimeEnrollment;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
public String getStudentId() {
return studentId;
}
public void setStudentId(String studentId) {
this.studentId = studentId;
}
public String getLogLink() {
return logLink;
}
public void setLogLink(String logLink) {
this.logLink = logLink;
}
public String getMentor() {
return mentor;
}
public void setMentor(String mentor) {
this.mentor = mentor;
}
public String getWishes() {
return wishes;
}
public void setWishes(String wishes) {
this.wishes = wishes;
}
public String getRecommendedFrom() {
return recommendedFrom;
}
public void setRecommendedFrom(String recommendedFrom) {
this.recommendedFrom = recommendedFrom;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", studentId=" + studentId +
'}';
}
}
package com.nicole.mybatis.mapper;
import com.nicole.mybatis.entity.Student;
import org.apache.ibatis.annotations.Param;
import java.util.*;
public interface StudentMapper {
/**
*
* @return
*/
List<Student> selectAll();
//primary key, only 0 or one result
Student selectById(Long id);
// The result could be null or 1 - max
List<Student> selectByName(String name);
int updateById(Student student);
}
<?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">
<mapper namespace="com.nicole.mybatis.mapper.StudentMapper">
<resultMap id="BaseResultMap" type="com.nicole.mybatis.entity.Student">
<id column="id" jdbcType="BIGINT" property="id" />
<result column="create_at" jdbcType="BIGINT" property="createAt" />
<result column="update_at" jdbcType="BIGINT" property="updateAt" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="qq_number" jdbcType="VARCHAR" property="qqNumber" />
<result column="major" jdbcType="VARCHAR" property="major" />
<result column="estimated_time_enrollment" jdbcType="VARCHAR" property="estimatedTimeEnrollment" />
<result column="school" jdbcType="VARCHAR" property="school" />
<result column="student_id" jdbcType="VARCHAR" property="studentId" />
<result column="log_link" jdbcType="VARCHAR" property="logLink" />
<result column="mentor" jdbcType="VARCHAR" property="mentor" />
<result column="wishes" jdbcType="VARCHAR" property="wishes" />
<result column="recommended_from" jdbcType="VARCHAR" property="recommendedFrom" />
</resultMap>
<sql id="Base_Column_List">
id, create_at, update_at, name, qq_number, major, estimated_time_enrollment, school, student_id, log_link, mentor, wishes, recommended_from
</sql>
<select id="selectAll" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from students
</select>
<select id="selectById" flushCache="true" parameterType="java.lang.Long" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from students
where id=#{id, jdbcType=BIGINT}
</select>
<select id="selectByName" flushCache="true" parameterType="java.lang.String" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from students
where name=#{name, jdbcType=VARCHAR}
</select>
<!--<update id="updateById" parameterType="com.nicole.mybatis.entity.Student">
update students
set
=#{, jdbcType=}
where id=#{id, jdbcType=}
</update>-->
</mapper>
log4j.rootLogger=INFO,Console
log4j.additivity.org.apache=true
log4j.logger.com.nicole.mybatis.mapper=TRACE
#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
log4j.logger.java.sql.ResultSet=DEBUG
log4j.logger.org.apache=DEBUG
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 配置全局属性 -->
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
<typeAliases>
<package name="com.nicole.mybatis.entity"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC">
<property name="" value=""/>
</transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/xz_task1"/>
<!--填写你的数据库用户名-->
<property name="username" value="root"/>
<!--填写你的数据库密码-->
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<package name="com.nicole.mybatis.mapper"/>
</mappers>
</configuration>
package com.nicole.mybatis.mapper;
import com.nicole.mybatis.entity.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.junit.BeforeClass;
import org.junit.Test;
import java.io.IOException;
import java.io.Reader;
import java.util.*;
public class StudentMapperTest {
private static SqlSessionFactory sqlSessionFactory;
@BeforeClass
public static void init() {
try {
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void testSelectAll() {
SqlSession sqlSession = null;
try {
sqlSession = sqlSessionFactory.openSession();
List<Student> students = sqlSession.selectList("selectAll");
for (int i = 0; i < students.size(); i++) {
System.out.println(students.get(i));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (sqlSession != null) {
sqlSession.close();
}
}
}
@Test
public void testSelectByName() {
SqlSession sqlSession = null;
String testName = "燕小鱼";
String noName = "unknow";
try {
sqlSession = sqlSessionFactory.openSession();
StudentMapper studentMapper = (StudentMapper) sqlSession.getMapper(StudentMapper.class);
List<Student> students = studentMapper.selectByName(testName);
System.out.println(" Student name " + testName + " is as listed");
for (int i = 0; i < students.size(); i++) {
System.out.println(students.get(i));
}
List<Student> students2 = studentMapper.selectByName(noName);
System.out.println(" Student name " + noName + " is as listed");
for (int i = 0; i < students2.size(); i++) {
System.out.println(students2.get(i));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (sqlSession != null) {
sqlSession.close();
}
}
}
@Test
public void testSelectById() {
SqlSession sqlSession = null;
Long testId = 1L;
Long notExist = 0L;
try {
sqlSession = sqlSessionFactory.openSession();
StudentMapper studentMapper = (StudentMapper) sqlSession.getMapper(StudentMapper.class);
Student student = studentMapper.selectById(testId);
System.out.println(" Student Id " + testId + " is as listed");
System.out.println(student);
Student student2 = studentMapper.selectById(notExist);
System.out.println(" Student Id " + notExist + " is as listed");
System.out.println(student2);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (sqlSession != null) {
sqlSession.close();
}
}
}
}
目前已经写好的3个测试全部通过结果如下:
二,今天问题:
我的代码粘贴前排版都是缩进好的。粘贴在日志有问题。
没想到这个项目我进度这么慢,还没能提交验收。
三,今天的收获:
复习mybatis, juit, log4j。
四,明天的计划:
1,继续完成mybatis的增删改,继续使用junit完成核验。
2,引入Spring到目前这个项目。虽然之前jdbctemplated的项目肯定已经要用Spring了。
3,完善junit做的单元测试。
4,早日上云,早日提交。
评论