SqlSessionFactoryUtil.java
package com.javaxk.util; import java.io.IOException;
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 getSqlSessinFactory() { if (sqlSessionFactory == null) { String resource = "mybatis-config.xml";
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(resource);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
} return sqlSessionFactory;
} public static SqlSession openSession() {
return getSqlSessinFactory().openSession();
} }
Student.java
package com.javaxk.model; public class Student { private int id;
private String name;
private int age; public Student() {
super();
} public Student(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
} public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
} @Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
} }
mybatis-config.xml
<?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>
<!-- <properties resource="jdbc.properties"/> --> <properties>
<property name="jdbc.driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="jdbc.url" value="jdbc:mysql://localhost:3306/db_mybatis?characterEncoding=utf-8"/>
<property name="jdbc.username" value="root"/>
<property name="jdbc.password" value="root"/>
</properties> <!--
<typeAliases>
<typeAlias alias="Student" type="com.javaxk.model.Student"/>
</typeAliases>
--> <typeAliases>
<package name="com.javaxk.model"/>
</typeAliases> <environments default="development"> <environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</dataSource>
</environment> <environment id="test">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</dataSource>
</environment> </environments> <mappers>
<!--
<mapper resource="com/javaxk/mappers/StudentMapper.xml" />
-->
<package name="com.javaxk.mappers"/>
</mappers>
</configuration>
StudentMapper.java
package com.javaxk.mappers; import java.util.List; import com.javaxk.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<Student> find(); }
StudentMapper.xml
<?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.javaxk.mappers.StudentMapper"> <resultMap type="Student" id="StudentResult">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
</resultMap> <insert id="add" parameterType="Student" >
insert into t_student values(null,#{name},#{age})
</insert> <update id="update" parameterType="Student">
update t_student set name=#{name},age=#{age} where id=#{id}
</update> <delete id="delete" parameterType="Integer">
delete from t_student where id=#{id}
</delete> <select id="findById" parameterType="Integer" resultType="Student">
select * from t_student where id=#{id}
</select> <select id="find" resultMap="StudentResult">
select * from t_student
</select> </mapper>
所有的主测试类都在JUtil的测试方法前后中调用
private static Logger logger = Logger.getLogger(StudentTest2.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();
}
第一节:insert映射语句
添加映射配置文件
<insert id="add" parameterType="Student" >
insert into t_student values(null,#{name},#{age})
</insert>
@Test
public void testAdd() {
logger.info("添加学生");
Student student = new Student("小宁" ,88);
studentMapper.add(student);
sqlSession.commit(); }
第二节:update映射语句
<update id="update" parameterType="Student">
update t_student set name=#{name},age=#{age} where id=#{id}
</update>
@Test
public void testUpdate() {
logger.info("更新学生");
Student student = new Student(20,"小宁22",99);
studentMapper.update(student);
sqlSession.commit();
}
第三节:delete映射语句
<delete id="delete" parameterType="Integer">
delete from t_student where id=#{id}
</delete>
@Test
public void testDelete() {
logger.info("删除学生");
studentMapper.delete(20);
sqlSession.commit();
}
第四节:select映射语句
(1)通过ID查找学生
<select id="findById" parameterType="Integer" resultType="Student">
select * from t_student where id=#{id}
</select>
@Test
public void testFindById() {
logger.info("通过ID查找学生");
Student student=studentMapper.findById(1);
System.out.println(student);
}
(2)查找所有学生
<resultMap type="Student" id="StudentResult">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
</resultMap> <select id="find" resultMap="StudentResult">
select * from t_student
</select>
@Test
public void testFind() {
logger.info("查找所有学生");
List<Student> studentlist = studentMapper.find();
for (Student s : studentlist) {
System.out.println(s);
}
}