Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the writing of the iBATIS test class

Shulou Source: shulou.com Published: 2022-06-02 04:02:28 10月04日 Update

IBATIS test class is written what is, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

Detailed Writing of iBATIS Test Class

Header Writing of ◆ iBATIS Test Class

Package com.ibatis.test; import java.io.IOException; import java.io.Reader; import java.sql.SQLException; import java.util.Iterator; import java.util.List; import com.ibatis.common.resources.Resources; import com.ibatis.sqlmap.client.SqlMapClient; import com.ibatis.sqlmap.client.SqlMapClientBuilder; import com.wyq.pojo.User

Establishment of ◆ iBATIS Test Class

Public class TestIbatis {public void readSqlMapConfig () {} public static void main (String args []) {Reader reader=null

◆ SqlMapClient

It is the core component of iBATIS and provides a basic platform for data manipulation, which can be created through sqlMapClientBuilder.

SqlMapClient sqlMap=null; String resource= "sqlMapConfig.xml"; try {reader=Resources.getResourceAsReader (resource); sqlMap=SqlMapClientBuilder.buildSqlMapClient (reader); sqlMap.startTransaction ()

◆ data write operation (insert,update,delete)

User user=new User (); user.setName ("Erica"); user.setSex (new Integer (1)); sqlMap.insert ("User.insertUser", user)

◆ data query operation (select)

Integer key=new Integer (1); User users= (User) sqlMap.queryForObject ("User.getUsers", key); System.out.println ("id===" + users.getId ()); System.out.println ("name===" + users.getName ()); System.out.println ("sex===" + users.getSex ())

◆ puts the query results in the specified object

Integer key=new Integer (1); User user=new User (); sqlMap.queryForObject ("User.getUsers", key,user); System.out.println ("id===" + user.getId ()); System.out.println ("name===" + user.getName ()); System.out.println ("sex===" + user.getSex ()); * /

◆ executes batch query

List list=sqlMap.queryForList ("User.getUsers", null); Iterator it=list.iterator (); while (it.hasNext ()) {User user= (User) it.next (); System.out.println ("id===" + user.getId ()); System.out.println ("name===" + user.getName ()); System.out.println ("sex===" + user.getSex ());}

◆ queries data within a specified range

List list=sqlMap.queryForList ("User.getUsers", null,0,3); Iterator it=list.iterator (); while (it.hasNext ()) {User user= (User) it.next (); System.out.println ("id===" + user.getId ()); System.out.println ("name===" + user.getName ()); System.out.println ("sex===" + user.getSex ());}

Execution of ◆ iBATIS test classes

SqlMap.commitTransaction (); System.out.print ("successful operation!") ;} catch (IOException e) {System.out.print ("failed to read configuration file!") ;} catch (SQLException ex) {System.out.print ("abnormal operation data!") ;} is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Tags: Test data query writing help clear successful in content can pass basic this object platform data query file article novice more core Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Shulou Tech Info Linux Shulou Information MySQL