iBatis-- layer interface provides automatic DAO manipulation functions (Detailed)

iBatis-- layer interface provides automatic DAO manipulation functions (Detailed)

  When performed when using iBatis persistence layer management, discovery data updates using DAO layer updateByPrimaryKey, updateByPrimaryKeySelective method, the results vary. Because not carefully studied before iBatis framework, it is hereby query related articles collated and recorded automatically generated DAO layer interface provides detailed usage and operation function differences are as follows:

  iBator generated interface DAO layer provides the following functions:

No. Method name parameter return value abnormal effect Remark
1 countByExample UserExample example int Conditional Count  
2 deleteByPrimaryKey Integer int Press the Delete key primary  
3 deleteByExample UserExample example int  Conditional deletion  
4 insert User record String/Integer Insert (return value id value)  
5 selectByPrimaryKey Integer User  By primary key query  
6 selectByExample UserExample example List<?> Query by the conditions  
7 selectByExampleWithBLOGs UserExample example List<?> Query by the conditions (including BLOB fields)

When the field type data in the table are generated as will be binary.

8 updateByPrimaryKey User record int By primary key update  
9 updateByPrimaryKeySelective User record int Updating the primary key value is not null field  
10 updateByExample User record, UserExample example int  Conditional update  
11 updateByExampleSelective User record, UserExample example int Conditional Update field value is not null  

Detailed:

= UserDAO of UserDAOImpl will  new new  of UserDAOImpl will (SqlMapClientFactory. GetSqlMapClient ());
NOTE :. SqlMapClientFactory getSqlMapClient (): is a self-defined classes and methods aimed at obtaining SqlMapClient.

① selectByPrimaryKey()

User user = userDAO.selectByPrimaryKey(100); 相当于select * from user where id = 100

② selectByExample() 和 selectByExampleWithBLOGs()

UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("joe");
criteria.andUsernameIsNull();
example.setOrderByClause("username asc,email desc");
List<?>list = userDAO.selectByExample(example);
相当于:select * from user where username = 'joe' and username is null order by username asc,email desc

Note: in the file contains a static UserExample.java iBator generated inner class Criteria, there are many ways in Criteria, primarily query the custom SQL statement where.

③ insert()

User user = new User();
user.setId(101);
user.setUsername("test");
user.setPassword("123")
user.setEmail("[email protected]");
userDAO.insert(user);
相当于:insert into user(ID,username,password,email) values(101,'test','123','[email protected]');

 ④ updateByPrimaryKey() 和 updateByPrimaryKeySelective()

User user =new User();
user.setId(101);
user.setUsername("joe");
user.setPassword("joe");
user.setEmail("[email protected]");
userDAO.updateByPrimaryKey(user);
相当于:update user set username='joe',password='joe',email='[email protected]' where id=101

User user = new User();
user.setId(101);
user.setPassword("joe");
userDAO.updateByPrimaryKeySelective(user);
相当于:
update user set password='joe' where id=101

 updateByExample() 和 updateByExampleSelective()

UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("joe");
User user = new User();
user.setPassword("123");
userDAO.updateByPrimaryKeySelective(user,example);
相当于:update user set password='123' where username='joe'

 deleteByPrimaryKey()

userDAO.deleteByPrimaryKey(101);  相当于:delete from user where id=101

⑦ deleteByExample()

UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("joe");
userDAO.deleteByExample(example);
相当于:delete from user where username='joe'

⑧ countByExample()

UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("joe");
int count = userDAO.countByExample(example);
相当于:select count(*) from user where username='joe'

 

NOTE: The Essay The " iBatis-- automatic DAO layer interface provides functions Detailed operation " from finishing.

Guess you like

Origin www.cnblogs.com/ylhssn/p/4184974.html