完成学生表的增删改查,学生表信息如下
CREATE TABLE tb_student(
sno INT AUTO_INCREMENT PRIMARY KEY,
student_name VARCHAR(20) NULL,
student_age INT NULL
);
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.16version>
dependency>
在resources目录下添加配置文件log4j.properties,其代码如下
# log4j的全局配置
log4j.rootLogger=ERROR, stdout
# MyBatis日志配置
log4j.logger.com.bjwl=DEBUG
# 控制台输出格式
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
在mybaits的主配文件中,添加日志的配置,代码如下
<settings>
...
<setting name="logImpl" value="LOG4J" />
settings>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.24version>
<scope>compilescope>
dependency>
编写实体类时,只需要在类上加@Data注解符,即可,无需程序员编写getter和setter代码,示例代码如下。
@Data
public class Student {
private Integer sno;
private String studentName;
private Integer studentAge;
}
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
<scope>testscope>
dependency>
<dependency>
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;
}
}
public interface StudentDao {
List<StudentPOJO> 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 List<Student> getAllStudent() {
SqlSession sqlSession = factory.openSession();
List<Student> 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() {
List<Student> 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());
List<Student> 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框架常用方式,要求必须掌握