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