Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the writing of the iBATIS test class

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report