SPRINGMVC Framework project, how to access the database using DAO in the controller

Source: Internet
Author: User

in the Springmvc Framework, Controller and the other Bean is scanned separately, because if this is not configured, Controller will be scanned two times, leading to things and other problems.

Typically, Controller is not allowed to use directly DAO , a reasonable design is the sevice to call the DAO .

But if there is a temporary need to Controller to access the database, what should I do?

There are two ways.

Method 1 : In Web. XML configured in the Spring The control of a thing designs the entire life cycle of the request.

     <!--ConfigurationSpringof theOpensessioninviewfilterto solve the problem of lazy loading exceptions -

<filter>

          <Filter-name>Opensessioninviewfilter</Filter-name>

          <Filter-class>Org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</Filter-class>

<init-param>

                <!--Specifysessionfactorythe name, default tosessionfactory -

                <param-name>Sessionfactorybeanname</param-name>

                <param-value>sessionfactory</param-value>

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; </ init-param >

</filter>

This can also be solved in JSP page where you go to the object . Object . object, I tell you Session The issue has been closed.

Method 2 :

Direct Use sessionfactory . Access to the database is performed through sessionfactory , so you can inject sessionfactory directly into the controller object, opensession when accessing the database .

There is no simple and quick solution on the internet, which is recorded here.

SPRINGMVC Framework project, how to access the database using DAO in the controller

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.