About data access (DAO)

Source: Internet
Author: User

In those years, there was a high wind of business processing everywhere, with some MVC framework, spring-managed service, and Dao,ssh, SSI, and so on represented by Ibatis and hibernate, wherever and whenever. However, is it really necessary for us to study the code carefully?

First, the implementation of DAO, in the case of Hibernate, userdao.create (user user), where user can have a lot of attributes, but in this method, we pass in a User object, many properties provided to the latter is not clear, which is necessary, And which are optional.

Second, DAO's last use, basically has the DAO's place to have the service, seems DAO is the service proxy, similar code:

Userservice.create (user user) {userdao.cerate (user);};

In these scenarios above, and even more scenes, DAO as a layer is completely unnecessary, as an auxiliary implementation of the service is the best way to handle it.

In flying, DAO is no longer a separate layer, through the declaration of DAO annotations and Param annotations, at run time, complete the DAO processing, the code is as follows:

@Service ("UserService")

public class UserService extends abstractservice{

@DaoCreate (entity= "Security.user")

Public Data Create (

@Param (value= "username", required=true) String username,

@Param (value= "password", required=true) String password,

@Param (value= "Org_name", Required=true) String Org_name,

@Param (value= "Real_name", Required=true) String real_name) throws Exception {

return null;

}

@DaoUpdate (entity= "Security.user")

Public Data Update (

@Param (value= "Org_name", Required=true) String Org_name,

@Param (value= "Real_name", Required=true) String Real_name,

@Param (value= "user_id", required=true) long user_id) throws Exception {

return null;

}

@DaoUpdate (entity= "Security.user")

Public Data ChangePassword (

@Param (value= "password", required=true) String password,

@Param (value= "user_id", required=true) long user_id) throws Exception {

return null;

}

@DaoRemove (entity= "Security.user")

Public Data Remove (

@Param (value= "user_id", required=true) long user_id) throws Exception {

return null;

}

@DaoRemove (entity= "Security.user")

Public Data Removebyusername (

@Param (value= "username", required=true) String username) throws Exception {

return null;

}

@DaoQuery (Single=true, entity= "Security.user.perms")

Public Data Findbyusernamepassword (@Param (value= "username", required=true) String username,

@Param (value= "password", required=true) String password) throws Exception {

return null;

}

}

For Daocreate, the runtime generates the corresponding INSERT statement based on the entity property and completes the SQL execution with the values in the parameters.

Implementation principle: At runtime, the service consumer invokes the service method, the service executes an annotation related to the service method, and extracts the relevant service parameters, uses the service parameters and annotation-related parameters to assemble SQL, such as insert SQL, and completes execution in JdbcTemplate.

For Daocreate, all method parameters are insert XX values (?). ,? ) in the parameters, other details are welcome to participate in the technical discussion group discussion.

Frame Source: https://github.com/hifong/flying

Demo Application: Https://github.com/hifong/pas

Technology QQ Group: 455852142

About data access (DAO)

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.