整体项目
使用pom.xml导入jar包
4.0.0
com.mybatis
MyBatisPro01
0.0.1-SNAPSHOT
jar
UTF-8
org.mybatis
mybatis
3.2.8
mysql
mysql-connector-java
3.1.12
log4j
log4j
1.2.17
junit
junit
4.12
test
log4j.properties
log4j.rootLogger=info,appender1,appender2
log4j.appender.appender1=org.apache.log4j.ConsoleAppender
log4j.appender.appender2=org.apache.log4j.FileAppender
log4j.appender.appender2.File=D:/logFile.txt
log4j.appender.appender1.layout=org.apache.log4j.TTCCLayout
log4j.appender.appender2.layout=org.apache.log4j.TTCCLayout
jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.220.129:3306/test
jdbc.username=root
jdbc.password=1234
mybatis-config.xml
Student实体类
package com.java1234.model;
public class Student {
private Integer id;
private String name;
private Integer age;
public Student() {
super();
}
public Student(String name, Integer age) {
super();
this.name = name;
this.age = age;
}
public Student(Integer id, String name, Integer age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
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 Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
Mapper接口
package com.java1234.mappers;
import java.util.List;
import com.java1234.model.Address;
import com.java1234.model.Student;
public interface StudentMapper {
public int add(Student student);
public int update(Student student);
public int delete(Integer id);
public Student findById(Integer id);
public List find();
}
StudentMapper.XML映射文件
insert into Student values(null,#{name},#{age})
update Student set name=#{name},age=#{age} where id=#{id}
delete from Student where id=#{id}
创建SqlSession的工具类
package com.java1234.util;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class SqlSessionFactoryUtil {
private static SqlSessionFactory sqlSessionFactory;
public static SqlSessionFactory getSqlSessionFactory(){
if(sqlSessionFactory==null){
InputStream inputStream=null;
try{
inputStream=Resources.getResourceAsStream("mybatis-config.xml");
sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
}catch(Exception e){
e.printStackTrace();
}
}
return sqlSessionFactory;
}
public static SqlSession openSession(){
return getSqlSessionFactory().openSession();
}
}
测试类
package com.java1234.model;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.java1234.mappers.StudentMapper;
import com.java1234.model.Student;
import com.java1234.util.SqlSessionFactoryUtil;
public class StudentTest {
private static Logger logger=Logger.getLogger(StudentTest.class);
private SqlSession sqlSession=null;
private StudentMapper studentMapper=null;
/**
* 测试方法前调用
* @throws Exception
*/
@Before
public void setUp() throws Exception {
sqlSession=SqlSessionFactoryUtil.openSession();
studentMapper=sqlSession.getMapper(StudentMapper.class);
}
/**
* 测试方法后调用
* @throws Exception
*/
@After
public void tearDown() throws Exception {
sqlSession.close();
}
public void testAdd() {
logger.info("添加学生");
Student student=new Student("王五",12);
studentMapper.add(student);
sqlSession.commit();
}
public void testUpdate(){
logger.info("修改学生");
Student student=new Student(8,"王五2",13);
studentMapper.update(student);
sqlSession.commit();
}
public void testDelete(){
logger.info("删除学生");
studentMapper.delete(8);
sqlSession.commit();
}
public void testFindById(){
logger.info("通过ID查找学生");
Student student=studentMapper.findById(12);
System.out.println(student);
}
public void testFind(){
logger.info("查找所有学生");
List studentList=studentMapper.find();
for(Student s:studentList){
logger.info(s.getId()+" "+s.getName()+" "+s.getAge());
}
}
}