A simple understanding of the sevice layer and DAO layer

Source: Internet
Author: User
Today, from their practical use to talk about the service and the DAO layer of understanding, I hope you god guidance. DAO layer and Service layer in the interface is the same, two layers of understanding and difference in the implementation class, 1.dao layer is directly related to the database, in the implementation of the specific SQL statements, execution and results return results. Abstraction is the logical layer that connects to the database, namely the data access layer. 2.service layer, literally is the service layer, that is, the business layer, the business in Java development is very important, the realization of the function is to achieve each business, in the business layer does not need to directly connect the database, you can directly invoke other encapsulated data can be connected to the class, to achieve data additions and deletions to check, Of course, in the Sevice also call other classes, personal understanding, the business layer is to invoke other methods, do not need to implement, this is also the MVC pattern of an application layered very thin layer, their respective roles. Sevice layer is the business, of course, plus things to facilitate the actual development of the operation, I usually use the spring of AOP, the following is my code to help you understand, but also hope to guide. 1.dao interface public interface Iadmindao {
/**
* Declaration method implementation class specific implementation
*/
Public Admin Login (admin admin);} 1.1.dao interface Implementation class Public classes Admindaoimpl implements Iadmindao {
Private DBHelper db = new DBHelper ()//write yourself a tool class for DBHelper database operations
method to implement, including SQL statements, execution and result return
Public Admin Login (admin admin) {
String sql= "SELECT * from admin where aname=?" and pwd=? and status!=0 ";
List<object> params=new arraylist<object> ();
Params.add (Admin.getaname ());
Params.add (Admin.getpwd ());
List<admin> login=db.find (SQL, params, admin.class);//Call tool class execution get value
if (Login!=null && login.size () >0) {
Return Login.get (0);
}
return null;
} 2.service Interface public interface Filmservice {
Declaration method implementation class concrete implementation
Public list<filminfo> Getfilmsbyfilmbean (Filmbean filmbean); The 2.1service interface implements public class Filmserviceimpl implements Filmservice {
@Autowired//spring_ioc Automatic annotation method, not important here
Private Filminfomapper Filminfomapper; A class that accesses a database (DAO layer) that is generated by using the mapping of the MyBatis database access layer

@Override
Public list<filminfo> Getfilmsbyfilmbean (Filmbean filmbean) {
List<filminfo> Ls=filminfomapper.getfilmsbyfilmbean (Filmbean);//Call Data access Class (DAO layer) to implement specific business return data
return LS;
}

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.