How to use SqlMapClientTemplate in iBaits
This article mainly introduces how to use SqlMapClientTemplate in iBaits. It is very detailed and has a certain reference value. Friends who are interested must finish it!
Apache iBatis (now moved to Google Code under the development, renamed MyBatis) is the current IT project widely used in a semi-automatic ORM framework, different from the fully automatic framework such as Hibernate, iBatis has more flexible control over the operation of the database, for those who often need to call local database functions to customize SQL statements, or like to optimize the efficiency of SQL implementation of developers, iBatis is a very good choice. The widely used open source enterprise architecture SpringFramework is also well integrated, which makes the use of iBatis in SpringFramework more convenient and fast. All the developer has to do is inherit the SqlMapClientDaoSupport class provided in SpringFramework. Next, I would like to share my experience with you:
1. The assembly of SqlMapClientFactoryBean
SqlMapClientFactoryBean is the basis for the use of SqlMapClientTemplate, if in the
If SqlMapClientFactoryBean is not installed in the SpringFramework application, SqlMapClientTemplate will not be available and null pointer error.
Bean >
2. Inherit and use SqlMapClientDaoSupport class
Declare the Java class:
. Import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;. Public class ReportDAOImpl extends SqlMapClientDaoSupport {. } assemble Java classes in SpringFramework configuration file: "reportDao" class= "com.test.dao.ReportDAOImpl" > "sqlMapClient" ref= "sqlMapClient" / >
Assemble the Java class in the SpringFramework configuration file:
3. Use SqlMapClientTemplate to query
Java Code:
When executing a query with no parameters:
List result = getSqlMapClientTemplate () .queryForList ("TestSpace.qryTest")
"TestSpace" is the namespace of the iBatis SqlMap file; "qryTest" is the query method id of iBatis SqlMap.
When obtaining a record information according to the primary key:
Long id = new Long ("2"); Object resultObj = getSqlMapClientTemplate () .queryForObject ("TestSpace.getTest", id)
When querying according to certain conditions:
ObjectA objA = new ObjectA (); objA.setParam1 ("test1"); objA.setParam2 ("test2");. List result = getSqlMapClientTemplate () .queryForList ("TestSpace.qryTestByParam", objA)
If you need to take 440 pieces of data:
List result = getSqlMapClientTemplate () .queryForList ("TestSpace.qryTestByParam", objA, 4,40)
You can also return Map
Map result = getSqlMapClientTemplate () .queryForMap ("TestSpace.qryTestByParam", objA, "MapKey")
4. Use SqlMapClientTemplate to add data
Java Code:
ObjectA objA = new ObjectA (); objA.setParam1 ("test1"); objA.setParam2 ("test2");. GetSqlMapClientTemplate () .insert (TestSpace.insertTest, objA)
5. Use SqlMapClientTemplate to update data
Java Code:
ObjectA objA = new ObjectA (); objA.setParam1 ("test1"); objA.setParam2 ("test2");. GetSqlMapClientTemplate () .update (TestSpace.updateTest, objA)
Update the first 20 records:
Java code
GetSqlMapClientTemplate () .update (TestSpace.updateTest, objA, 20)
6. Delete data using SqlMapClientTemplate
Java Code:
Long id = new Long ("2"); getSqlMapClientTemplate (). Delete ("TestSpace.deleteTest", id); above is all the content of this article "how to use SqlMapClientTemplate in iBaits". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!