1. Create a Web project.
2. Introduce the class library.
3. Configuring the CPU
<servlet> <servlet-name>springmvc</servlet-name> <servlet-class> org.springframework.web.servlet.dispatcherservlet</servlet-class> <!-- Specify the location of the SPRINGMVC configuration file with initialization parameters- <init-param> <param-name>contextconfiglocation</ param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param ></servlet><servlet-mapping> <servlet-name>springmvc</servlet-name> < Url-pattern>/</url-pattern></servlet-mapping>
4. Write Springmvc-servlet.xml:
class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" > <!--prefix/web-inf/ jsps/home.jsp--> <property name= "prefix" value= "/web-inf/jsps/" ></property> <!--suffix-- > <property name= "suffix" value= ". JSP" ></property></bean>
5. Write Beans.xml:
<!--annotations Drive--><mvc:annotation-driven/><!--components scan--><context:component-scan base-package = "Cn.itcast.springmvc" ></context:component-scan>
6. Define the entity class:
Public classPersonImplements java.io.Serializable{PrivateString ID; PrivateString name; PrivateString address; Public FinalString getId () {returnID; } Public Final voidsetId (String id) {SYSTEM.OUT.PRINTLN ("Calling SetID method, id=" +ID); This. ID =ID; } Public FinalString GetName () {returnname; } Public Final voidsetName (String name) { This. Name =name; } Public FinalString getaddress () {returnaddress; } Public Final voidsetaddress (String address) { This. Address =address; } @Override PublicString toString () {return"{ID:" +id+ ", Name:" +name+ ", Address:" +address+ "}"; }}
7. Define a mapping file for the person entity class Person.hbm.xml
class name= "Cn.itcast.springmvc.domain.Person" table= "Person" > <id name= "id" column= "id" type= "string" > class= "uuid"/> </id> <property name= "name" column= "name" type= "string"/> < Property name= "Address" column= "Address" type= "string"/> </class>
8. Create a person table
9. The initialization of the spring container is generally done when the project is started , Web. xml:
<!--Specify the location of the spring configuration file with context parameters-<listener> <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:beans.xml</param-value> </context-param> <servlet> <s Ervlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <!--Specify the location of the SPRINGMVC configuration file with initialization parameters--<init-param> <param-name>contextconfigl Ocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </i nit-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
10. Create a controller class that handles the person, leaving the process out of the way, regardless of the other.
@Controller @requestmapping (Value= "/person") // Follow the path Public class PersonController { @RequestMapping (value= "/save") public String Saveperson (person p) { System.out.println ("Saveperson () ..."); return "Success"; }}
Http://localhost:8080/springmvc-3/person/save
11. Integrate hibernate, you must have a data source, connect to the database.
Beans.xml:
<!--defining a data source--><bean id= "DataSource"class= "Com.mchange.v2.c3p0."Combopooleddatasource"> <property name=" driverclass "value=" Com.mysql.jdbc.Driver "/> <property name=" Jdbcurl "value= "Jdbc:mysql://localhost/springmvc"/> <property name= "user" value= "root"/> <property name= "Pass Word "value=" root "/> <property name=" initialpoolsize "value=" ten "/> <property name=" max Poolsize "value=" "/> <property name=" minpoolsize "value="/></bean> "<bean id= sessionFacto" Ryclass= "Org.springframework.orm.hibernate3."Localsessionfactorybean"> <property name=" dataSource "ref=" DataSource "></property> <!--the location of the hibernate mapping file --<property name= "Mappingdirectorylocations" > <value>classpath:cn/itcast/springmvc/doma in/</value> </property> <property name= "Hibernateproperties" > <props> <prop key= "Hibernate. Dialect ">org.hibernate.dialect.MySQL5Dialect</prop> <prop key=" Hibernate.show_sql ">true</prop> <prop key= "Hibernate.hbm2ddl" >update</prop> </props> < /property></bean>
<!--transaction Manager--
<bean id= "Txmanager" class= "Org.springframework.orm.hibernate3.HibernateTransactionManager" >
<property name= "Sessionfactory" ref= "Sessionfactory" ></property>
</bean>
<!--Configure the propagation characteristics of things (things notifications)--
<tx:advice id= "Txadvice" transaction-manager= "Txmanager" >
<tx:attributes>
<tx:method name= "save*" propagation= "REQUIRED"/>
<tx:method name= "delete*" propagation= "REQUIRED"/>
<tx:method name= "update*" propagation= "REQUIRED"/>
<tx:method name= "find*" read-only= "true"/>
<tx:method name= "*" read-only= "true"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:advisor pointcut= "Execution (* cn.itcast.springmvc.service.*.* (..))" advice-ref= "TxAdvice"/>
</aop:config>
12. Define Service,dao
Public Interface Ipersondao { publicvoid Saveperson (person P); }
@Repository (value= "Persondao" ) public class persondaoimpl implements Span style= "color: #000000;" > Ipersondao {@Resource (name = "Sessionfactory") // name is the name in Beans.xml private Sessionfactory sessionfactory; public void Saveperson (person p) {sessionfactory. Getcurrentsession () . Save (p);//config transaction can}}
Public Interface Ipersonservice { publicvoid Saveperson (person p);}
@Service (value= "Personservice")publicclassimplements Ipersonservice { @Resource (Name= "Persondao") Private Ipersondao Persondao; Public void Saveperson (person p) { Persondao.saveperson (p); }}
@Controller @requestmapping (value= "/person")//Follow the path Public classPersonController {@Resource (name= "Personservice") PrivateIpersonservice Personservice; @RequestMapping (Value= "/save") PublicString Saveperson () {person P=NewPerson (); P.setname ("AA"); P.setaddress ("Beijing"); Personservice.saveperson (P); System.out.println ("Saveperson () ..."); return"Success"; } }
Successfully saved to the database.
------------------------------------------------------------------------
A complete crud operation.
Integration of Spring MVC and Hibernate