创建测试工程
代码引入了之后,需要创建测试工程,从读代码的角度看,测试工程不需要很负责,只需要配置运行mybatis工程就可以了,尽量保持工程的纯粹,方便读代码。
- 创建表
CREATE TABLE oa_leave (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(50) DEFAULT NULL,
startDate datetime DEFAULT NULL,
endDate datetime DEFAULT NULL,
memo varchar(255) DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
- 创建mybatis配置类
<?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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://192.168.197.205:3306/system_data_permission?characterEncoding=utf8"/>
<property name="username" value="root"/>
<property name="password" value="toor@gzstrong"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/LeaveMapper.xml"></mapper>
</mappers>
</configuration>
- 创建model类
package com.gzwx.mybatis.model;
import java.util.Date;
public class Leave {
/**
* id
*/
private Integer id;
/**
* 请假人姓名
*/
private String name;
/**
* 请假开始时间
*/
private Date startDate;
/**
* 请假结束时间
*/
private Date endDate;
/**
* 请假原因
*/
private String memo;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getStartDate() {
return startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
public Date getEndDate() {
return endDate;
}
public void setEndDate(Date endDate) {
this.endDate = endDate;
}
public String getMemo() {
return memo;
}
public void setMemo(String memo) {
this.memo = memo;
}
@Override
public String toString() {
return "Leave{" +
"id=" + id +
", name='" + name + '\'' +
", startDate=" + startDate +
", endDate=" + endDate +
", memo='" + memo + '\'' +
'}';
}
}
- 创建dao类
package com.gzwx.mybatis.dao;
import com.gzwx.mybatis.model.Leave;
import org.apache.ibatis.annotations.Param;
public interface LeaveMapper {
public void insertLeave(Leave leave);
public void updateLeave(Leave leave);
public Leave findLeaveById(Integer id);
public void deleteLeave(@Param("id") Integer id);
}
- 创建测试工程
package com.gzwx.test;
import com.gzwx.mybatis.dao.LeaveMapper;
import com.gzwx.mybatis.model.Leave;
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.Before;
import org.junit.Test;
import java.io.InputStream;
import java.util.Date;
public class TestMybatis {
private SqlSession session;
@Before
public void start(){
try{
InputStream inputStream = Resources.getResourceAsStream("myBatis-config.xml");
SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(inputStream);
session=factory.openSession();
}catch(Exception exception){
exception.printStackTrace();
}
}
@Test
public void test01(){
Leave leave = new Leave();
leave.setName("小明");
leave.setStartDate(new Date());
leave.setEndDate(new Date());
leave.setMemo("事假");
LeaveMapper leaveMapper = session.getMapper(LeaveMapper.class);
leaveMapper.insertLeave(leave);
session.commit();
}
@Test
public void test02(){
Leave leave = new Leave();
leave.setId(4);
leave.setName("小强");
leave.setStartDate(new Date());
leave.setEndDate(new Date());
leave.setMemo("休假");
LeaveMapper leaveMapper = session.getMapper(LeaveMapper.class);
leaveMapper.updateLeave(leave);
session.commit();
}
@Test
public void test03(){
LeaveMapper leaveMapper = session.getMapper(LeaveMapper.class);
Leave leave = leaveMapper.findLeaveById(4);
System.out.println(leave);
session.commit();
}
@Test
public void test04(){
LeaveMapper leaveMapper = session.getMapper(LeaveMapper.class);
leaveMapper.deleteLeave(4);
session.commit();
}
}
- 工程总览
至此,集成mybatis的工程已经创建好,后面对源码的debug都可以基于这个工程来进行