SqlSessionFactoryUtil.java
1 package com.javaxk.util; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 6 import org.apache.ibatis.io.Resources; 7 import org.apache.ibatis.session.SqlSession; 8 import org.apache.ibatis.session.SqlSessionFactory; 9 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 10 11 public class SqlSessionFactoryUtil { 12 13 private static SqlSessionFactory sqlSessionFactory; 14 15 public static SqlSessionFactory getSqlSessinFactory() { 16 17 if (sqlSessionFactory == null) { 18 19 String resource = "mybatis-config.xml"; 20 InputStream inputStream = null; 21 try { 22 inputStream = Resources.getResourceAsStream(resource); 23 } catch (IOException e) { 24 // TODO Auto-generated catch block 25 e.printStackTrace(); 26 } 27 sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream); 28 } 29 30 return sqlSessionFactory; 31 } 32 33 public static SqlSession openSession() { 34 return getSqlSessinFactory().openSession(); 35 } 36 37 }
Student.java
1 package com.javaxk.model; 2 3 public class Student { 4 5 private int id; 6 private String name; 7 private int age; 8 9 public Student() { 10 super(); 11 } 12 13 public Student(int id, String name, int age) { 14 super(); 15 this.id = id; 16 this.name = name; 17 this.age = age; 18 } 19 20 public Student(String name, int age) { 21 super(); 22 this.name = name; 23 this.age = age; 24 } 25 public int getId() { 26 return id; 27 } 28 public void setId(int id) { 29 this.id = id; 30 } 31 public String getName() { 32 return name; 33 } 34 public void setName(String name) { 35 this.name = name; 36 } 37 public int getAge() { 38 return age; 39 } 40 public void setAge(int age) { 41 this.age = age; 42 } 43 44 @Override 45 public String toString() { 46 return "Student [id=" + id + ", name=" + name + ", age=" + age + "]"; 47 } 48 49 }
mybatis-config.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 <!-- <properties resource="jdbc.properties"/> --> 7 8 <properties> 9 <property name="jdbc.driverClassName" value="com.mysql.jdbc.Driver"/> 10 <property name="jdbc.url" value="jdbc:mysql://localhost:3306/db_mybatis?characterEncoding=utf-8"/> 11 <property name="jdbc.username" value="root"/> 12 <property name="jdbc.password" value="root"/> 13 </properties> 14 15 <!-- 16 <typeAliases> 17 <typeAlias alias="Student" type="com.javaxk.model.Student"/> 18 </typeAliases> 19 --> 20 21 <typeAliases> 22 <package name="com.javaxk.model"/> 23 </typeAliases> 24 25 <environments default="development"> 26 27 <environment id="development"> 28 <transactionManager type="JDBC" /> 29 <dataSource type="POOLED"> 30 <property name="driver" value="${jdbc.driverClassName}" /> 31 <property name="url" value="${jdbc.url}" /> 32 <property name="username" value="${jdbc.username}" /> 33 <property name="password" value="${jdbc.password}" /> 34 </dataSource> 35 </environment> 36 37 <environment id="test"> 38 <transactionManager type="JDBC" /> 39 <dataSource type="POOLED"> 40 <property name="driver" value="${jdbc.driverClassName}" /> 41 <property name="url" value="${jdbc.url}" /> 42 <property name="username" value="${jdbc.username}" /> 43 <property name="password" value="${jdbc.password}" /> 44 </dataSource> 45 </environment> 46 47 </environments> 48 49 <mappers> 50 <!-- 51 <mapper resource="com/javaxk/mappers/StudentMapper.xml" /> 52 --> 53 <package name="com.javaxk.mappers"/> 54 </mappers> 55 </configuration>
StudentMapper.java
1 package com.javaxk.mappers; 2 3 import java.util.List; 4 5 import com.javaxk.model.Student; 6 7 public interface StudentMapper { 8 9 public int add(Student student); 10 11 public int update(Student student); 12 13 public int delete(Integer id); 14 15 public Student findById(Integer id); 16 17 public List<Student> find(); 18 19 }
StudentMapper.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <mapper namespace="com.javaxk.mappers.StudentMapper"> 6 7 <resultMap type="Student" id="StudentResult"> 8 <id property="id" column="id"/> 9 <result property="name" column="name"/> 10 <result property="age" column="age"/> 11 </resultMap> 12 13 <insert id="add" parameterType="Student" > 14 insert into t_student values(null,#{name},#{age}) 15 </insert> 16 17 <update id="update" parameterType="Student"> 18 update t_student set name=#{name},age=#{age} where id=#{id} 19 </update> 20 21 <delete id="delete" parameterType="Integer"> 22 delete from t_student where id=#{id} 23 </delete> 24 25 <select id="findById" parameterType="Integer" resultType="Student"> 26 select * from t_student where id=#{id} 27 </select> 28 29 <select id="find" resultMap="StudentResult"> 30 select * from t_student 31 </select> 32 33 </mapper>
所有的主测试类都在JUtil的测试方法前后中调用
1 private static Logger logger = Logger.getLogger(StudentTest2.class); 2 private SqlSession sqlSession = null; 3 private StudentMapper studentMapper = null; 4 5 /** 6 * 测试方法前调用 7 * @throws Exception 8 */ 9 @Before 10 public void setUp() throws Exception { 11 sqlSession = SqlSessionFactoryUtil.openSession(); 12 studentMapper = sqlSession.getMapper(StudentMapper.class); 13 } 14 15 /** 16 * 测试方法后调用 17 * @throws Exception 18 */ 19 @After 20 public void tearDown() throws Exception { 21 sqlSession.close(); 22 }
第一节:insert映射语句
添加映射配置文件
1 <insert id="add" parameterType="Student" > 2 insert into t_student values(null,#{name},#{age}) 3 </insert>
1 @Test 2 public void testAdd() { 3 logger.info("添加学生"); 4 Student student = new Student("小宁" ,88); 5 studentMapper.add(student); 6 sqlSession.commit(); 7 8 }
第二节:update映射语句
1 <update id="update" parameterType="Student"> 2 update t_student set name=#{name},age=#{age} where id=#{id} 3 </update>
1 @Test 2 public void testUpdate() { 3 logger.info("更新学生"); 4 Student student = new Student(20,"小宁22",99); 5 studentMapper.update(student); 6 sqlSession.commit(); 7 }
第三节:delete映射语句
1 <delete id="delete" parameterType="Integer"> 2 delete from t_student where id=#{id} 3 </delete>
1 @Test 2 public void testDelete() { 3 logger.info("删除学生"); 4 studentMapper.delete(20); 5 sqlSession.commit(); 6 }
第四节:select映射语句
(1)通过ID查找学生
1 <select id="findById" parameterType="Integer" resultType="Student"> 2 select * from t_student where id=#{id} 3 </select>
1 @Test 2 public void testFindById() { 3 logger.info("通过ID查找学生"); 4 Student student=studentMapper.findById(1); 5 System.out.println(student); 6 }
(2)查找所有学生
1 <resultMap type="Student" id="StudentResult"> 2 <id property="id" column="id"/> 3 <result property="name" column="name"/> 4 <result property="age" column="age"/> 5 </resultMap> 6 7 <select id="find" resultMap="StudentResult"> 8 select * from t_student 9 </select>
1 @Test 2 public void testFind() { 3 logger.info("查找所有学生"); 4 List<Student> studentlist = studentMapper.find(); 5 for (Student s : studentlist) { 6 System.out.println(s); 7 } 8 }