郑州市建设工程造价信息网站,小企业怎么做网站,珠海响应式网站制作,网站怎么做域名跳转实验目的
掌握MyBaits动态代理的使用log4j日志的使用Lombk的使用单元测试的使用SqlSessionFactory单例模式预处理语句的使用
实验内容
完成学生表的增删改查#xff0c;学生表信息如下
CREATE TABLE tb_student( sno INT AUTO_INCREMENT PRIMARY KEY, student_name VAR…实验目的
掌握MyBaits动态代理的使用log4j日志的使用Lombk的使用单元测试的使用SqlSessionFactory单例模式预处理语句的使用
实验内容
完成学生表的增删改查学生表信息如下
CREATE TABLE tb_student( sno INT AUTO_INCREMENT PRIMARY KEY, student_name VARCHAR(20) NULL, student_age INT NULL
);实验步骤
log4j日志文件的配置与使用 在pom文件中添加下述坐标获取log4j依赖包
dependency groupIdlog4j/groupId artifactIdlog4j/artifactId version1.2.16/version
/dependency 在resources目录下添加配置文件log4j.properties其代码如下
# log4j的全局配置
log4j.rootLoggerERROR, stdout
# MyBatis日志配置
log4j.logger.com.bjwlDEBUG
# 控制台输出格式
log4j.appender.stdoutorg.apache.log4j.ConsoleAppender
log4j.appender.stdout.layoutorg.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern%5p [%t] - %m%n 在mybaits的主配文件中添加日志的配置代码如下
settings ... !--配置SQL输出文件-- setting namelogImpl valueLOG4J /
/settings Lombok的配置与使用 Lombok项目是一个java库它可以自动插入到编辑器和构建工具中增强java的性能。不需要再写getter、setter或equals方法只要有一个注解就有一个功能齐全的构建器、自动记录变量等。使用Maven坐标导入使用Lombok的依赖包坐标如下。
dependency groupIdorg.projectlombok/groupId artifactIdlombok/artifactId version1.18.24/version scopecompile/scope
/dependency编写实体类时只需要在类上加Data注解符即可无需程序员编写getter和setter代码示例代码如下。
Data
public class Student { private Integer sno; private String studentName; private Integer studentAge;
} 单元测试junit的使用 使用单元测试工具junit在pom文件中增加以下maven坐标完成依赖包的导入后在类的方法上加注解夫test就可以进行运行测试。maven坐标如下所示 dependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion4.12/versionscopetest/scope/dependencydependencySqlSessionFactory单例模式 在myBatis框架的使用中每次执行持久化化访问操作时需要读入主配文件创建sqlSessionFactory实例可以将其中共用部分提取到一个公用类实现复用。由于SqlSessionFactory可以被多个线程共享避免资源浪费应该保证SqlSessionFactory的在一个系统中仅有一个实例其代码如下
public class BatisUtils { private static SqlSessionFactory sqlSessionFactory; private BatisUtils() {}; public static synchronized SqlSessionFactory getSqlSessionFactory() throws IOException { if (sqlSessionFactory null) { InputStream inputStream Resources.getResourceAsStream(mybatis-config.xml); sqlSessionFactory new SqlSessionFactoryBuilder().build(inputStream); } return sqlSessionFactory; }
}定义持久化接口StudentDao 代码如下 public interface StudentDao { ListStudentPOJO getAllStudent(); int addStudent(StudentPOJO student); int updateStudent(StudentPOJO student); int deleteStudent(Integer sno); } 不使用动态代理时需要开发人员编写持久化接口的实现类代码如下。 public class StudentDaoImpl implements StudentDao { private SqlSessionFactory factory; public StudentDaoImpl(SqlSessionFactory factory) { this.factory factory; } Override public ListStudent getAllStudent() { SqlSession sqlSession factory.openSession(); ListStudent students sqlSession.selectList(com.bjwl.dao.StudentDao.getAllStudent); return students; } Override public int addStudent(Student student) { SqlSession sqlSession factory.openSession(); Integer flag sqlSession.insert(com.bjwl.dao.StudentDao.addStudent,student); sqlSession.commit(); return flag; } Override public int updateStudent(Student student) { SqlSession sqlSession factory.openSession(); Integer flag sqlSession.update(com.bjwl.dao.StudentDao.updateStudent,student); sqlSession.commit(); return flag; } Override public int deleteStudent(Integer sno) { SqlSession sqlSession factory.openSession(); Integer flag sqlSession.delete(com.bjwl.dao.StudentDao.deleteStudent,sno); sqlSession.commit(); return flag; }
} 需要注意的是增加、删除、修改必须使用 sqlSession.commit()提交修改信息才能将维护信息写入数据库中编写测试类代码如下。
//不使用动态代理模式
public class MyBatisTest0 { private StudentDao dao; public MyBatisTest0() throws IOException { SqlSessionFactory sqlSessionFactory BatisUtils.getSqlSessionFactory(); this.dao new StudentDaoImpl(sqlSessionFactory); } Test public void getAllStudentTest() { ListStudent students dao.getAllStudent(); for (int i 0; i students.size(); i) { System.out.println(students.get(i).toString()); } } Test public void addStudentTest() { Student student new Student(); student.setStudentName(陆雪琪); student.setStudentAge(19); dao.addStudent(student); } Test public void updateStudentTest() { Student student new Student(); student.setSno(3); student.setStudentName(黑山老妖); student.setStudentAge(299); dao.updateStudent(student); } Test public void deleteStudentTest() { dao.deleteStudent(8); }
} 代码中方法上的Test 注解符就是使用junit提供的测试功能运行结果如下图所示。 图中显示的sql语句就是通过log4j在控制台显示的内容 9. 使用动态代理 不使用动态代理时开发人员必须编写每个持久化接口的实现类分析这些实现类中代码其执行过程非常相似如果这部分代码由计算机自动生成则可大大减轻开发人员的工作量动态代理方式就是由计算机生成持久化实现类的方式。添加一个测试类MyBatisTest在此类中直接使用动态代理方式示例代码如下。
public class MyBaitsTest { public void getAllStudentTest() throws Exception { SqlSession session getSqlSession(); System.out.println(StudentDao.class); StudentDao dao session.getMapper(StudentDao.class); System.out.println(dao.getClass()); ListStudent students dao.getAllStudent(); for (int i 0; i students.size(); i) { System.out.println(students.get(i).toString()); } session.close(); } private SqlSession getSqlSession() throws IOException { SqlSessionFactory sqlSessionFactory BatisUtils.getSqlSessionFactory(); SqlSession session sqlSessionFactory.openSession(); return session; }
} 运行过如下 由运行图可以看到通过传入接口com.bjwl.dao.StudentDao生成的StudentDao 为代理对象无需开发人员编写接口的实现类。
使用动态代理是使用mybaits框架常用方式要求必须掌握