Data Source: http://blog.sina.com.cn/s/blog_6145ed810100e570.html
Ibatis Official homepage: http://ibatis.apache.org/
MySQL Database (library name: test):
--------------------------------Table structure for student------------------------------CREATE table ' student ' (' I d ' Int (one) not null auto_increment, ' name ' varchar (255) default NULL, PRIMARY KEY (' id ') engine=innodb default charset=l Atin1;
First look at the engineering structure:
The code is as follows:
Student.java
Package Com.ibatis.bean; public class Student {private int id; private string name; private string gender, private int age; private int Gradeid P Ublic 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;} p ublic void SetName (string name) {this.name = name;} public String Getgender () {return gender;} public void Setgender (S Tring 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 () {Sq Lsession session = Ibatissessionfactory.getsqlsession (); list<student> list = session.selectlist ("Student.selectstudentlist"); Session.close (); return list; 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} </se lect> </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; stat IC {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 C Losesqlsession () {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.getsqlses Sion (); 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 ()); } }