資料來源:http://blog.sina.com.cn/s/blog_6145ed810100e570.html
ibatis官方首頁:http://ibatis.apache.org/
MySQL資料庫(庫名:test):
-- ---------------------------- -- Table structure for student -- ---------------------------- CREATE TABLE `student` ( `id` int(11) NOT NULL auto_increment, `name` varchar(255) default NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
先看一下工程結構:
代碼如下:
Student.java
package com.ibatis.bean; public class Student { private int id; private String name; private String gender; private int age; private int gradeId; public Student() { super(); } public Student(int id, String name, String gender, int age, int gradeId) { super(); this.id = id; this.name = name; this.gender = gender; this.age = age; this.gradeId = gradeId; } 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 String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getGradeId() { return gradeId; } public void setGradeId(int gradeId) { this.gradeId = gradeId; } }
StudentDaoImpl.java
package com.ibatis.dao.impl; import java.util.List; import org.apache.ibatis.session.SqlSession; import com.ibatis.bean.Student; import com.ibatis.dao.StudentDao; import com.ibatis.factory.IbatisSessionFactory; public class StudentDaoImpl implements StudentDao { @SuppressWarnings("unchecked") public List<Student> list() { SqlSession session = IbatisSessionFactory.getSqlSession(); List<Student> list = session.selectList("student.selectStudentList"); session.close(); return list; } public Student getStudent(Integer id) { SqlSession session = IbatisSessionFactory.getSqlSession(); Student stu = (Student) session.selectOne("student.selectStudent", id); session.close(); return stu; } }
studentSqlMapper.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"> <!-- <mapper namespace="student"> <resultMap type="student" id="studentMapper"> <result column="id" property="id" jdbcType="INTEGER" /> <result column="name" property="name" jdbcType="VARCHAR" /> </resultMap> <select id="selectStudentList" resultType="student"> select * from student </select> <select id="selectStudent" parameterType="int" resultMap="studentMapper"> select * from student where id = #{id} </select> </mapper> --> <mapper namespace="com.ibatis.dao.StudentDao"></mapper>
StudentDao.java
package com.ibatis.dao; import java.util.List; import org.apache.ibatis.annotations.Select; import com.ibatis.bean.Student; public interface StudentDao { @Select("select * from student") List<Student> list(); @Select("select * from student where id = #{id}") Student getStudent(Integer id); }
IbatisSessionFactory.java
package com.ibatis.factory; import java.io.IOException; import java.io.Reader; 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 IbatisSessionFactory { private static SqlSessionFactory sqlMapper; private static SqlSession sqlSession; static { try { String resource = "dao-configuration.xml"; Reader reader = Resources.getResourceAsReader(resource); sqlMapper = new SqlSessionFactoryBuilder().build(reader); } catch (IOException e) { e.printStackTrace(); } } public static SqlSession getSqlSession() { sqlSession = sqlMapper.openSession(); return sqlSession; } public static void closeSqlSession() { if (sqlSession != null) { sqlSession.close(); } } }
dao-configuration.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-config.dtd"> <configuration> <typeAliases> <typeAlias type="com.ibatis.bean.Student" alias="student" /> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="UNPOOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/test" /> <property name="username" value="root" /> <property name="password" value="root" /> </dataSource> </environment> </environments> <mappers> <mapper resource="com/ibatis/dao/sql/studentSqlMapper.xml" /> </mappers> </configuration>
AnnocationTest.java
package com.ibatis; import java.util.List; import org.apache.ibatis.session.SqlSession; import com.ibatis.bean.Student; import com.ibatis.dao.StudentDao; import com.ibatis.factory.IbatisSessionFactory; public class AnnocationTest { public static void main(String[] args) { SqlSession session = IbatisSessionFactory.getSqlSession(); StudentDao dao = session.getMapper(StudentDao.class); List<Student> list = dao.list(); System.out.println(list.size()); Student stu = dao.getStudent(2); System.out.println(stu.getName()); } }
Test.java
package com.ibatis; import com.ibatis.bean.Student; import com.ibatis.dao.StudentDao; import com.ibatis.dao.impl.StudentDaoImpl; public class Test { public static void main(String[] args) { StudentDao dao = new StudentDaoImpl(); Student stu = dao.getStudent(2); System.out.println(stu.getName()); } }