The idea of integrating spring and ibatis frameworks is basically consistent with the integration of spring and hibernate frameworks.
Step one: Create a new project.
Step Two: Add the spring application environment to the project.
Step three: Import the required jar package for Ibatis and the database jar package.
Step four: Create a new entity bean. As follows:
Package cn.test.entity;
Import java.io.Serializable;
/**
* @author Administrator
*
* Student Entity Bean
*
*/
public class Student implements Serializable {
/**
*
*/
Private static final long serialversionuid = 1L;
Private Integer ID;
Private String Studentname;
Private Integer studentage;
Private String studentaddress;
Public Integer getId () {
return ID;
}
public void SetId (Integer id) {
This.id = ID;
}
Public String Getstudentname () {
return studentname;
}
public void Setstudentname (String studentname) {
This.studentname = Studentname;
}
Public Integer Getstudentage () {
return studentage;
}
public void Setstudentage (Integer studentage) {
This.studentage = Studentage;
}
Public String getstudentaddress () {
return studentaddress;
}
public void setstudentaddress (String studentaddress) {
this.studentaddress = studentaddress;
}
}
Step Five: Create a new configuration file for the corresponding bean. As follows: Student.xml
<?xml version= "1.0″encoding=" utf-8″?>
<! DOCTYPE Sqlmap
Public "-//ibatis.apache.org//dtd SQL Map 2.0//en"
"Http://ibatis.apache.org/dtd/sql-map-2.dtd" >
<sqlMap>
<!– Query Operation –>
<select id= "getallstudent" resultclass= "Cn.test.entity.Student" >
SELECT * FROM student where 1=1
</select>
<!– get information by number –>
<select id= "Getstudentbyid" resultclass= "cn.test.entity.Student" parameterclass= "Integer" >
SELECT * FROM student where id= #ids #
</select>
</sqlMap>
Step Six: Create a new application configuration file for Ibatis. Sql-map-config.xml
<?xml version= "1.0″encoding=" utf-8″?>
<! DOCTYPE Sqlmapconfig
Public "-//ibatis.apache.org//dtd SQL Map Config 2.0//en"
"Http://ibatis.apache.org/dtd/sql-map-config-2.dtd" >
<sqlMapConfig>
<!– notify spring where to look for configuration files –>
<sqlmap resource= "Cn/test/configfile/student.xml"/>
</sqlMapConfig>
Step Seven: Modify the spring Applicationcontext.xml file. Add the following code:
<!– Configuring the data source –>
<bean id= "DataSource" >
<property name= "Driverclassname" >
<value>com.mysql.jdbc.Driver</value>
</property>
<property name= "url" >
<value>jdbc:mysql://localhost:3306/test</value>
</property>
<property name= "username" >
<value>root</value>
</property>
<property name= "Password" >
<value>root</value>
</property>
</bean>
<!–spring's Ibatis Preparation –>
<bean id= "Sqlmapclient" >
<property name= "DataSource" >
<ref bean= "DataSource"/>
</property>
<property name= "Configlocation" >
<value>cn/test/configfile/sql-map-config.xml</value>
</property>
</bean>
<!– Configuration Template –>
<bean id= "Sqlmapclienttemplate" >
<property name= "Sqlmapclient" >
<ref bean= "Sqlmapclient"/>
</property>
</bean>
Step Eight: Write Data persistence levels and business layer code. The data persistence layer code is as follows:
public class Studentdaoimple implements istudentdao{
Private Sqlmapclienttemplate sqlmapclienttemplate;
public void Setsqlmapclienttemplate (Sqlmapclienttemplate sqlmapclienttemplate) {
This.sqlmapclienttemplate = sqlmapclienttemplate;
}
Public list<student> getallstudent () {
list<student> list = sqlmapclienttemplate.queryforlist ("getallstudent");
return list;
}
}
and modify the spring Applicationcontext.xml file
<!–DAO–>
<bean id= "Studentdao" >
<property name= "Sqlmapclienttemplate" >
<ref bean= "Sqlmapclienttemplate"/>
</property>
</bean>
Step nine: Establish test classes and test methods.
public class Test {
/**
* @param args
*/
public static void Main (string[] args) {
ApplicationContext factory = new Classpathxmlapplicationcontext ("Applicationcontext.xml");
Istudentdao DAO = (Istudentdao) factory.getbean ("Studentdao");
list<student> list = Dao.getallstudent ();
for (int i = 0; i < list.size (); i++) {
Student stu = List.get (i);
System.out.println (Stu.getid () + ":" +stu.getstudentname () + ":" +stu.getstudentage () + ":" +stu.getstudentaddress ());
}
}