Springmvc+spring4.0+hibernate Simple Integration

Source: Internet
Author: User

Learn the initial one Hello world.

1. build up the environment

工欲善其事, the first of them will benefit

This is the jar needed

The simple saying:

Standard.jar This jar package is used when we use the JSTL tag in JSP. You can also use Springel.

Servlet-api.jar This is the class you use in Springmvc for HttpServletRequest.


Imported packages are OK, upload downloaded package I did not guide. Now it's time to go to the config file.

2. Configuration files

2.1web.xml

What framework the project uses. Look at this configuration and you know it.

<listener>

<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>

</listener>

<!--Configure spring's listener for initializing container objects--

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<context-param>

<param-name>contextConfigLocation</param-name>

<!--Note that my applicationcontext.xml and the following SPRINGMVC configuration files are placed under src --

<param-value>classpath:applicationcontext*.xml</param-value>

</context-param>

<!--Central Controller--

<servlet>

<servlet-name>springmvc</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath*:springmvc-servlet.xml</param-value>

</init-param>


<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>springmvc</servlet-name>

<!--struts are accustomed to using/*, SPRINGMVC no matter how

<url-pattern>*.do</url-pattern>

</servlet-mapping>


Configuration of the 2.2 Applicationcontext.xml

My database connection uses SPIRNG to connect. The file is then decoupled using the mysql.properties configuration.

Note If your spring, hibernate version is not the same as mine. You modify the following green font version number.

<!--automatically scan and assemble beans--

<context:component-scan base-package= "Org.pk.ssh" ></context:component-scan>

<!--Import external properties Files--

<context:property-placeholder location= "classpath:mysql.properties"/>

<!--configuration Sessionfactory --

<bean id= "sessionfactory" class= "Org.springframework.orm. Hibernate4. Localsessionfactorybean ">

<!--specify Hibernate profile Location--

<property name= "configlocation" value= "Classpath:hibernate.cfg.xml" ></property>

<!--configuring C3P0 database Connection Pool--

<property name= "DataSource" >

<bean class= "Com.mchange.v2.c3p0.ComboPooledDataSource" >

<!--data connection information--

<property name= "Jdbcurl" value= "${jdbcurl}" ></property>

<property name= "Driverclass" value= "${driverclass}" ></property>

<property name= "user" value= "${user}" ></property>

<property name= "password" value= "${password}" ></property>

<!--other Configurations--

<!--get three connections when initializing, the value should be between Minpoolsize and Maxpoolsize. Default:3--

<property name= "Initialpoolsize" value= "3" ></property>

<!--the minimum number of connections that are kept in the connection pool. Default:3--

<property name= "Minpoolsize" value= "3" ></property>

<!--The maximum number of connections that are kept in the connection pool. Default:15--

<property name= "Maxpoolsize" value= "5" ></property>

<!--c3p0 The number of connections that are fetched at the same time when the connection in the connection pool is exhausted. Default:3--

<property name= "Acquireincrement" value= "3" ></property>

<!--controls the number of preparedstatements loaded within the data source. If both maxstatements and maxstatementsperconnection are 0, the cache is closed. Default:0--

<property name= "maxstatements" value= "8" ></property>

<!--maxstatementsperconnection defines the maximum number of cache statements that a single connection in a connection pool has. Default:0--

<property name= "Maxstatementsperconnection" value= "5" ></property>

<!--maximum idle time, unused in 1800 seconds, the connection is discarded. If 0, it will never be discarded. Default:0--

<property name= "maxidletime" value= "1800" ></property>

</bean>

</property>

</bean>

<!--Configure declarative transaction management (using annotations)--

<bean id= "Txmanager" class= "Org.springframework.orm. Hibernate4. Hibernatetransactionmanager ">

<property name= "sessionfactory" ref= "Sessionfactory" ></property>

</bean>

<tx:annotation-driven transaction-manager= "Txmanager"/>


2.3 Mysql.propertiese

#注意格式

Jdbcurl = jdbc:mysql:///spring

Driverclass = com.mysql.jdbc.Driver

User = root

Password = root


configuration of 2.4 hibernate ?

<!--1, database connection information--

<property name= "dialect" >

Org.hibernate.dialect.MySQL5InnoDBDialect

</property>

<!--2, other configurations--

<property name= "Show_sql" >true</property>

<property name= "Hbm2ddl.auto" >update</property>

<!--3, import the mapping file, I'm using hibernate annotations, if you use a config file--

<!--<mapping resource= "Org/pk/ssh/model/user"/> configuration file--

<mapping class= "Org.pk.ssh.model.User"/>

</session-factory>


2.5 springmvc ? ?configuration ? ? ?? SP?? RINGMVC-SERVLET.XM????

<!--MVC Annotations Drive

<mvc:annotation-driven/>

<!--scanner already has this MVC annotation-driven feature above, all that's not needed--

<context:component-scan base-package= "Org.pk.ssh.controller"/>

<!--prefix + viewName + suffix---

<bean class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" >

<!--webroot The path to a specified folder--

<property name= "prefix" value= "/web-inf/jsp/" ></property>

< suffix!--View name--

<property name= "suffix" value= ". JSP" ></property>

</bean>


3, the configuration file is OK, I mainly write a login HelloWorld

I am in the way of the annotation to achieve.

Model:helloworld class


DAO and its implementation class, log in I will directly put the user name and password into my database. In fact, it should be registered. But it's a beginner. That's what I wrote. Hey. And then slowly to improve it.

Now it's time for the Controller class to debut. We welcome it.

Request for JSP interface

Create a JSP directory under Web-in. JSP interface is put here. Why do you put it here instead of Webroot ? Here are some of the online statements

http://blog.csdn.net/saygoodbyetoyou/article/details/9944773

http://uule.iteye.com/blog/1853531

? ? The above is my login interface. Request attention Add ? ? *.do because my configuration interception is like this.

And then this is the output in another JSP interface, I'm using jstl for output.

<c:out value= "${user.name}"/>


Springmvc+spring4.0+hibernate Simple Integration

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.