Springmvc + hibernate is a simple example.
I just entered a new company and saw that all the projects were in the form of webservice + springmvc. I used Struts all the time before. I think it is necessary to record it. Because I have recorded the annotation injection method before, but I have not been in touch with it for so long, and I forget it. Fortunately, I recorded it in my blog.
Environment: spring4.0.4.RELEASE, hibernate4.3.10Final. The packages are all downloaded on the official website.
Springmvc + hibernate made by eclipse + mysql adopts the annotation form.
OK. Let's start with the next step.
Eclipse first sets up a web project. If you use web3.0, you need to create web. xml (not created by default)
First, let's take a look at web. xml.
Springmvc
ContextConfigLocation
Classpath: spring-beans.xml
Org. springframework. web. context. ContextLoaderListener
EncodingFilter
Org. springframework. web. filter. CharacterEncodingFilter
Encoding
UTF-8
OpenSessionInViewFilter
Org. springframework. orm. hibernate4.support. OpenSessionInViewFilter
SessionFactoryBeanName
SessionFactory
OpenSessionInViewFilter
*. Do
Springmvc
Org. springframework. web. servlet. DispatcherServlet
1
Springmvc
*. Do
Index.html
Index.htm
Index. jsp
Default.html
Default.htm
Default. jsp
Springmvc the servlet name is associated with the configuration file under the WEB-INF, the naming method is [servletname]-servlet. xml, I am here springmvc.
Creating a WEB-INF under a springmvc-servlet.xml is as follows:
The view parser divides the prefix suffix and then merges it with the string returned by your controller to form a page path. (All are modifiable paths)
Put it under the WEB-INF just for safety.
Basically, springmvc needs to be configured.
The controller code is as follows:
/****@authorDaven.Tsang**/@ControllerpublicclassHelloController{@Resource(name=userService)privateUserServiceuserService;/****@paramname*@parammodel*@return*/@RequestMapping(value=/hello.do)publicStringsayHello(Modelmodel){Stringsql=fromUser;List
users=userService.findAll(sql);model.addAttribute(users,users);returnhello;}}
UserService is injected into the business. My current code is divided into three layers, dao, service, controller (equivalent to the previous action) can also be said to be a view. Only the controller is used to control the view.
The returned value "hello" is as mentioned above. It defines the path of a page together with the prefix + hello + suffix.
See index. jsp and hello. jsp.
Index. jsp is as follows:
If your user class has these attributes, you can get the parameters directly in the controller. (Automatic encapsulation)
Hello. jsp
$ {S. user_name}
Download the project.
Basically, such a project is ready.