Spring + Ibatis Complete Example

Source: Internet
Author: User
Tags config mysql query xmlns

Recently studied spring + IBATIS. It is one thing to find examples of others, and to write a complete application is another matter. I'm fed up with the smattering code posted online.

Ibatis is a persistence framework that covers SQL procedures, although SQL statements need to be written by themselves. In addition, I think for beginners, the complete example is really important, otherwise I do not know if the file is placed.

All third packages need to be added, spring, Ibatis.-2.3.3.720.jar, Sqlijdbc.jar, Oscache-2.4.jar, Commons-pool-1.3.jar, Commons-dbcp-1.4.jar,mysql-connector-5.0.5.jar. Otherwise, the time to run the error.

The location of the XML file is also critical because the access profile needs to determine his path.

There are three of packages:

1.bean.

The bean package encapsulates the Pojo object Ibatis, as follows:

Package Com.tmall.bean;

public class Ibatis {
	private String ID;
	   private String name;
	   Public String getId () {
	        return ID;
	   }
	   public void SetId (String id) {
	        this.id = ID;
	   }
	   Public String GetName () {
	        return name;
	   }
	   public void SetName (String name) {
	        this.name = name;
	   }
	   Public Ibatis () {
	       
	   } public
	Ibatis (string ID, string name) {
	    super ();
	    This.id = ID;
	    this.name = name;
	}
}

2. DAO Package

DAO has a DAO and daoimp inside. DAO is an interface that DAOIMP implements the DAO interface.

Dao.java as follows:

Package Com.tmall.dao;

Import java.util.List;
import Com.tmall.bean.Ibatis;;
Public interface Dao {public
	  list<ibatis> getList ();
	  Public Ibatis getbyname (String name);
	  Public Ibatis getById (String ID);
	  public void Insert (Ibatis Ibatis);
	  public void Delete (String id);
	  public void Update (Ibatis Ibatis);
}

Daoimp.java as follows

Package Com.tmall.dao;

Import java.util.List;

Import Org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;


Import Com.tmall.bean.Ibatis; public class Daoimp extends Sqlmapclientdaosupport implements Dao {public void delete (String id) {Getsqlmap
    Clienttemplate (). Delete ("deleteusers", id); Public Ibatis getById (String ID) {return (Ibatis) getsqlmapclienttemplate (). queryForObject ("Getusersbyid", I
    D); The public Ibatis getbyname (String name) {return (Ibatis) getsqlmapclienttemplate (). queryForObject ("ge
    Tusersbyname ", name); } @SuppressWarnings ("Unchecked") public list<ibatis> getList () {return getsqlmapclienttemplate (). Quer
    Yforlist ("getAllUsers", null);
    } public void Insert (Ibatis Ibatis) {getsqlmapclienttemplate (). Insert ("Insertusers", Ibatis);
    public void Update (Ibatis Ibatis) {getsqlmapclienttemplate (). Update ("Updateusers", Ibatis); }

}

In addition, there are three configuration files for the key issues. Applicationcontext.xml, Ibatis.xml, Sqlmapconfig.xml.

The Applicationcontext.xml file is a spring configuration file, as follows:

<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:x Si= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans-2.0.xsd "> <bean id=" DataSource "class=" Org.apache.commons.dbcp.BasicDataSource "> <property name=" driverclassname "> <value> com.mysql.jdbc.driver</value> </property> <property name= "username" > <value>root</value > </property> <property name= "password" > <value>123456</value> </property> &L T;property name= "url" > <value>jdbc:mysql://localhost:3306/test</value> </property> </bean&

Gt <bean id= "sqlmapclient" class= "Org.springframework.orm.ibatis.SqlMapClientFactoryBean" > <!-- The Ibatis configuration file should be injected here instead of the Sqlmap file, otherwise "there is no statement .... Exception "-<properTy name= "configlocation" > <value>sqlMapConfig.xml</value> </property> </bean> <bean Id= "Daoimp" class= "com.tmall.dao.DaoImp" > <property name= "DataSource" > <ref bean= "DataSource"/> &lt ;/property> <property name= "sqlmapclient" > <ref bean= "sqlmapclient"/> </property> &LT;/BEAN&G

T </beans>

Ibatis.xml content is as follows:

<?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 > <typealias type= "com.tmall.bean.Ibatis" alias= "user"/> <resultmap id= "ibatistest" class= "use R "> <result column=" id "property=" id "jdbctype=" VARCHAR "/> <result column=" name "property=" name "Jdb Ctype= "VARCHAR"/> </resultMap> <!--get full query list--<select id= "getAllUsers" resultmap= "ibatistest "> select * from Ibatis </select> <!--get user objects based on user name--<select id=" getusersbyname "result map= "Ibatistest" > select * from Ibatis where name= #value # </select> <!--get user objects by ID--&L 
  
   T;select id= "Getusersbyid" resultmap= "Ibatistest" > select * from Ibatis where id= #value # </select> <!--New User objects--<insert id= "insertusers" parameterclass= "user" > INSERT INTO IbatiS (id,name) VALUES (#id #, #name #) </insert> <!--Delete User objects--<delete id= "Deleteusers" > D Elete from Ibatis where id= #value # </delete> <!--Update User objects-<delete id= "Updateusers" Paramete rclass= "user" > Update ibatis set name= #name # where id= #id # </delete> </sqlMap>

The Sqlmapconfig.xml file is as follows:

<?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> 

<sqlmap resource=" Ibatis.xml "/> 

</ Sqlmapconfig>


Test statement

Package com.tmall.test;
Import java.util.ArrayList;
Import Java.util.Iterator;

Import java.util.List;
Import Org.springframework.context.ApplicationContext;

Import Org.springframework.context.support.ClassPathXmlApplicationContext;
Import Com.tmall.bean.Ibatis;

Import Com.tmall.dao.Dao; public class Test {/** * @param args */public static void main (string[] args) {//TODO auto-generated Method St
		 UB ApplicationContext context=new classpathxmlapplicationcontext ("Applicationcontext.xml");
	     DAO DAO = (dao) Context.getbean ("Daoimp"); 
	     Dao.insert (New Ibatis ("3", "NEW3"));
	     Ibatis Ibatis3 = Dao.getbyid ("2");
	     Ibatis3.setname ("New7");
Dao.update (IBATIS3);
	     Testdaoimpl.delete ("3");
	        System.out.println ("Get full query list");
	        List<ibatis> result=new arraylist<ibatis> ();
	        result = Dao.getlist (); for (iterator<ibatis> iter = Result.iterator (); Iter.hasnext ();)
	 {Ibatis element = (Ibatis) iter.next ();           System.out.println (Element.getname ());
 }
	}

}

Don't forget to create a table in the MySQL statement, and then use the MySQL query tool to see the results.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.