Here I learn ibatis time, an example of implementation sent up, for reference.
The project directory structure is as follows:
1. SQL Map configuration file
Sqlmapconfig.xml
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE Sqlmapconfig
Public "-//ibatis.com//dtd SQL Map Config 2.0//en"
"Http://www.ibatis.com/dtd/sql-map-config-2.dtd" >
<sqlMapConfig>
<properties resource= "Com/study/xiaofeng/maps/sqlmapconfig.properties"/>
<settings cachemodelsenabled= "true"
Enhancementenabled= "true"
Lazyloadingenabled= "true"
Errortracingenabled= "true"
Maxrequests= "32"
Maxsessions= "10"
Maxtransactions= "5"
Usestatementnamespaces= "false"/>
<transactionmanager type= "JDBC" >
<datasource type= "Simple" >
<property name= "JDBC. Driver "value=" ${driver} "/>
<property name= "JDBC. Connectionurl "value=" ${url} "/>
<property name= "JDBC. Username "value=" ${username} "/>
<property name= "JDBC. Password "value=" ${password} "/>
<property name= "Pool.maximumactiveconnections" value= "ten"/>
<property name= "Pool.maximumidleconnections" value= "5"/>
<property name= "Pool.maximumcheckouttime" value= "120000"/>
<property name= "pool.timetowait" value= "/>"
<property name= "Pool.pingquery" value= "SELECT 1 from Sample"/>
<property name= "pool.pingenabled" value= "false"/>
<property name= "Pool.pingconnectionsolderthan" value= "1"/>
<property name= "Pool.pingconnectionsnotusedfor" value= "1"/>
</dataSource>
</transactionManager>
<!--
<transactionmanager type= "JTA" >
<property name= "UserTransaction"
Value= "Java:comp/env/jdbc/framework"/>
<datasource type= "JNDI" >
<property name= "DataSource"
Value= "Java:comp/env/jdbc/ibatistest"/>
</dataSource>
</transactionManager>
<transactionmanager type= "JDBC" >
<datasource type= "JNDI" >
<property name= "DataSource"
Value= "Java:comp/env/jdbc/ibatistest"/>
</dataSource>
</transactionManager>
-
<sqlmap resource= "Com/study/xiaofeng/maps/person.xml"/>
</sqlMapConfig>
2. The SQL Map configuration file has a unique <properties> element, and after doing so, the properties defined in the properties file can be referenced as variables in the SQL map configuration file and all of the SQL map mapping files it contains
Sqlmapconfig.xml
Driver=com.microsoft.jdbc.sqlserver.sqlserverdriver
Url=jdbc:microsoft:sqlserver://localhost:1433;databasename=ibatistest; Selectmethod=cursor;
Username=xiaofeng
Password=xiaofeng
3. SQL Map XML map
Person.xml
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE Sqlmap
Public "-//ibatis.com//dtd SQL Map 2.0//en"
"Http://www.ibatis.com/dtd/sql-map-2.dtd" >
<sqlmap namespace= "Student" >
<typealias alias= "Student" type= "Com.study.xiaofeng.Student"/>
<typealias alias= "Course" type= "Com.study.xiaofeng.Course"/>
<typealias alias= "Intro" type= "Com.study.xiaofeng.Intro"/>
<typealias alias= "SC" type= "Com.study.xiaofeng.SC"/>
<resultmap id= "Get-student-result" class= "Student" >
<result property= "Sno" column= "Sno"/>
<result property= "sname" column= "sname"/>
<result property= "Ssex" column= "Ssex"/>
<result property= "Sage" column= "sage"/>
<result property= "sdept" column= "sdept"/>
<result property= "SC" column= "Sno" select= "GETSC"/>
<result property= "Intro" column= "Sno" select= "Getintro"/>
</resultMap>
<resultmap id= "Get-course-result" class= "Course" >
<result property= "CNO" column= "CNO"/>
<result property= "CNAME" column= "cname"/>
<result property= "Ccredit" column= "Ccredit"/>
</resultMap>
<resultmap class= "Intro" id= "Get-intro-result" >
<result property= "Sno" column= "Sno"/>
<result property= "idescription" column= "Idescription"/>
</resultMap>
<!--
<resultmap id= "Get-sc-result" class= "SC" >
<result property= "Sno" column= "Sno"/>
<result property= "CNO" column= "CNO"/>
<result property= "Grade" column= "Grade"/>
<result property= "Course" column= "Cno" select= "GetCourse"/>
</resultMap>
-
<select id= "getstudent" parameterclass= "String" resultmap= "Get-student-result" >
SELECT * FROM STUDENT
WHERE
Sname= #value #
</select>
<select id= "GetCourse" parameterclass= "Integer" resultmap= "Get-course-result" >
SELECT * from COURSE WHERE cno= #value #
</select>
<select id= "Getintro" parameterclass= "Integer" resultmap= "Get-intro-result" >
Select *from INTRO WHERE sno= #value #
</select>
<select id= "GETSC" parameterclass= "Integer" resultclass= "SC" >
Select Cno,grade
From SC
WHERE
sno= #sno #
</select>
<insert id= "Insertstudent" parameterclass= "Student" >
INSERT into
STUDENT (sno,sname,ssex,sage,sdept)
VALUES (#sno #, #sname #, #ssex #, #sage #, #sdept #)
</insert>
<update id= "Updatestudent" parameterclass= "Student" >
UPDATE STUDENT
SET sname= #sname #,
ssex= #ssex #,
Sage= #sage #,
sdept= #sdept #
WHERE Sno = #sno #
</update>
<delete id= "Deletestudent" parameterclass= "Student" >
DELETE STUDENT
WHERE Sno = #sno #
</delete>
</sqlMap>
4, Appsqlconfig.java
Package Com.study.xiaofeng;
Import com.ibatis.sqlmap.client.SqlMapClient;
Import Java.io.Reader;
Import com.ibatis.common.resources.*;
Import Com.ibatis.sqlmap.client.SqlMapClientBuilder;
public class Appsqlconfig {
private static final sqlmapclient Sqlmap;
static {
try {
String resource = "Com/study/xiaofeng/maps/sqlmapconfig.xml";
Reader reader = Resources.getresourceasreader (Resource);
Sqlmap = sqlmapclientbuilder.buildsqlmapclient (reader);
} catch (Exception e) {
E.printstacktrace ();
throw new RuntimeException ("Error Initializing Myappsqlconfig class. Cause: "+e);
}
}
public static Sqlmapclient Getsqlmapinstance () {
return sqlmap;
}
}
Test.java
Package Com.study.xiaofeng;
Import com.ibatis.sqlmap.client.SqlMapClient;
Import java.sql.*;
Import java.util.List;
Jta
Import Javax.naming.InitialContext;
Import javax.transaction.UserTransaction;
public class Test {
public static void update (int no,string name,string sex,int age,string dept) {
Com.ibatis.sqlmap.client.SqlMapClient client = null;
try{
Client=new appsqlconfig (). Getsqlmapinstance ();
Client.starttransaction ();
Student student=new Student ();
Student.setsno (no);
Student.setsname (name);
Student.setssex (Sex);
Student.setsage (age);
Student.setsdept (dept);
Client.update ("Updatestudent", student);
Client.committransaction ();
}catch (SQLException e) {
System.out.println (E.getmessage ());
}finally {
try {
Client.endtransaction ();
} catch (SQLException e) {
E.printstacktrace ();
}
}
}
public static void insertstudent (int no,string name,string sex,int age,string dept) {
Com.ibatis.sqlmap.client.SqlMapClient client = null;
try{
Client=new appsqlconfig (). Getsqlmapinstance ();
Client.starttransaction ();
Student student=new Student ();
Student.setsno (no);
Student.setsname (name);
Student.setssex (Sex);
Student.setsage (age);
Student.setsdept (dept);
Client.insert ("Insertstudent", student);
Client.committransaction ();
}catch (SQLException e) {
System.out.println (E.getmessage ());
}finally {
try {
Client.endtransaction ();
} catch (SQLException e) {
E.printstacktrace ();
}
}
}
An object is implemented directly as an attribute, implementing an association
public static Student Getstudent () {
Com.ibatis.sqlmap.client.SqlMapClient client = null;
Student Student=null;
try{
Client=new appsqlconfig (). Getsqlmapinstance ();
Client.starttransaction ();
Student = (student) client.queryforobject ("Getstudent", "Xiaofeng");
Client.committransaction ();
}catch (SQLException e) {
System.out.println (E.getmessage ());
}finally{
try {
Client.endtransaction ();
} catch (SQLException e) {
E.printstacktrace ();
}
}
return student;
}
Multiple objects are placed in the list as a property implementation association, but nested queries. Test a one-to-many association query
You can also implement many-to-many
public static void Restudent (String name) {
Com.ibatis.sqlmap.client.SqlMapClient sqlmap = null;
Sqlmap=new appsqlconfig (). Getsqlmapinstance ();
try{
Sqlmap.starttransaction ();
List studentlist=sqlmap.queryforlist ("Getstudent", name);
for (int i=0;i<studentlist.size (); i++) {
Student student= (Student) studentlist.get (i);
System.out.println ("Name (table 1):" +student.getsname ());
for (int k=0;k<student.getsc (). Size (); k++) {
SC sc= (SC) STUDENT.GETSC (). get (k);
Course course= (Course) sqlmap.queryforobject ("GetCourse", Sc.getcno ());
System.out.print ("Course number (table 2):" +sc.getcno ());
System.out.print ("------Course name (table 3):" +course.getcname (). Trim () + "------score (Table 2):" +sc.getgrade () + "/n");
}
}
Sqlmap.committransaction ();
}catch (SQLException e) {
System.out.println (E.getmessage ());
}finally{
try {
Sqlmap.endtransaction ();
} catch (SQLException e) {
E.printstacktrace ();
}
}
}
public static void Main (String args[]) {
Update (2004131301, "Xiaofeng", "male", 23, "information");
Restudent ("name2");
Student student=getstudent ();
System.out.println ("Table 4 Description:" +student.getintro (). Getidescription ());
Insertstudent (2004131305, "xiaofeng5", "male", 23, "information");;
}
}
Course.java
Package Com.study.xiaofeng;
Import java.io.Serializable;
public class Course implements serializable{
private int cno;
private String CNAME;
private int ccredit;
public int getcno () {
return this.cno;
public void setcno (int no) {
this.cno=no;
public String getcname () {
return this.cname;
public void Setcname (String name) {
this.cname =name;
}
public int getccredit () {
return this.ccredit;
public void Setccredit (int credits) {
This.ccredit=credit;
}
}
Intro.java
Package Com.study.xiaofeng;
Import java.io.Serializable;
public class Intro implements serializable{
private int sno;
Private String idescription;
public int Getsno () {
return this.sno;
}
public void Setsno (int sno) {
This.sno=sno;
}
Public String getidescription () {
return this.idescription;
}
public void Setidescription (String description) {
This.idescription=description;
}
}
Sc.java
Package Com.study.xiaofeng;
Import java.io.Serializable;
public class SC implements serializable{
private int sno;
private int cno;
private int grade;
Private Course Course;
public int Getsno () {
return this.sno;
}
public void Setsno (int no) {
This.sno=no;
}
public int Getcno () {
return this.cno;
}
public void Setcno (int no) {
This.cno=no;
}
public int Getgrade () {
return this.grade;
}
public void Setgrade (int grade) {
This.grade=grade;
}
Public Course GetCourse () {
return this.course;
}
public void Setcourse (Course Course) {
This.course=course;
}
}
Student.java
Package Com.study.xiaofeng;
Import java.io.Serializable;
Import java.util.List;
public class Student implements serializable{
private int sno;
Private String sname;
Private String Ssex;
private int sage;
Private String sdept;
Private List SC;
Private Intro Intro;
public int Getsno () {
return this.sno;
}
public void Setsno (int no) {
This.sno=no;
}
Public String Getsname () {
return this.sname;
}
public void Setsname (String name) {
This.sname=name;
}
Public String Getssex () {
return this.ssex;
}
public void Setssex (String sex) {
This.ssex=sex;
}
public int getsage () {
return this.sage;
}
public void Setsage (int.) {
This.sage=age;
}
Public String getsdept () {
return this.sdept;
}
public void Setsdept (String dept) {
this.sdept=dept;
}
Public List GETSC () {
return This.sc;
}
public void Setsc (List SC) {
THIS.SC=SC;
}
Public Intro Getintro () {
return This.intro;
}
public void Setintro (Intro Intro) {
This.intro=intro;
}
}
The results of the operation are as follows: