Summary of essays-SPIRNGMVC

Source: Internet
Author: User


Build springmvc+hibernate+sping Frame

    1. The first required jar is imported into the project;

    2. Thespring configuration file is introduced in Web. XML, the project initiates the initialization of the spring controller, Spingbean injection, <!--controller method call rule definition--><!-- Page view layer basic information Settings--><!--servlet mapping list, where the servlet of all control layer controllers is defined--

      Xml

<?xml version= "1.0" encoding= "UTF-8"?
<web-app version= "2.5"
    xmlns= "http:// Java.sun.com/xml/ns/javaee "
    xmlns:xsi=" Http://www.w3.org/2001/XMLSchema-instance "
    xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee
    http:// Java.sun.com/xml/ns/javaee/web-app_2_5.xsd

    <servlet>
         <servlet-name>dispatcherservlet</servlet-name>
         <servlet-class>
            Org.springframework.web.servlet.DispatcherServlet
        </ Servlet-class>
        <init-param>
             <param-name>contextconfiglocation</param-name>

<!--Refer to some configuration files, easy to manage put dao,service,hibernate,web a separate profile, another way to refer to the note 1-->
<param-value>/web-inf/hib-config.xml,/web-inf/web-config.xml,/web-inf/service-config.xml,/web-inf/ Dao-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>


Note 1: introducing a configuration file using the Context-param servlet context

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

<!--the configuration file that starts with spring--
<param-value>classpath:spring-*.xml</param-value>

</context-param>


<!--load the controller that scans spring--
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>

<!--project Start instantiating a sub-servlet, be sure to have <init-param> specify a sping profile--
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>


Web-config.xml

<?xml version= "1.0" encoding= "UTF-8"?>
<beans xmlns= "Http://www.springframework.org/schema/beans"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
Xsi:schemalocation= "
Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-2.5.xsd ">

<!--controller method call rule definition--
<bean id= "Paramethodresolver"
class= "Org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver" >
<property name= "ParamName" value= "action"/>
<property name= "defaultmethodname" value= "list"/>
</bean>

<!--Page view layer Basic info set--
<bean id= "Viewresolver"
class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name= "Viewclass"
Value= "Org.springframework.web.servlet.view.JstlView"/>
<!--<property name= "prefix" value= "/myjsp/"/>-->
<property name= "suffix" value= ". jsp"/>
</bean>

<!--servlet mapping list, where the servlet for all control layer controllers is defined--
<bean id= "UrlMapping"
class= "Org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" >
<property name= "Mappings" >
<props>
<prop key= "User.do" >userController</prop>
</props>
</property>
</bean>

<bean id= "Usercontroller" class= "Com.sxt.action.UserController" >
<property name= "UserService" ref= "UserService" ></property>
</bean>
</beans>


Hib-config.xml


<?xml version= "1.0" encoding= "UTF-8"?>
<beans xmlns= "Http://www.springframework.org/schema/beans"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"
xmlns:tx= "Http://www.springframework.org/schema/tx"
xmlns:context= "Http://www.springframework.org/schema/context"
Xsi:schemalocation= "
Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
Http://www.springframework.org/schema/tx
Http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
Http://www.springframework.org/schema/aop
Http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-2.5.xsd
">
<context:component-scan base-package= "COM.SXT"/>
<!--support AOP annotations--
<aop:aspectj-autoproxy/>

<!--data source information can be placed in the Properties folder, note 2-->
<bean id= "DataSource"
class= "Org.apache.commons.dbcp.BasicDataSource" >
<property name= "Driverclassname" value= "Com.mysql.jdbc.Driver" > </property>
<property name= "url" value= "Jdbc:mysql://localhost:3306/myhib" ></property>
<property name= "username" value= "root" ></property>
<property name= "Password" value= "123456" ></property>
</bean>

<bean id= "Sessionfactory"
class= "Org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" >
<property name= "DataSource" > <ref bean= "DataSource"/></property>
<property name= "Hibernateproperties" >
<props>
<!--Key's name is preceded by Hibernate. -
<prop key= "Hibernate.dialect" >
Org.hibernate.dialect.MySQLDialect
</prop>
<prop key= "Hibernate.show_sql" >true</prop>
<prop key= "Hibernate.hbm2ddl.auto" >update</prop>
</props>
</property>
<property name= "Packagestoscan" >
<value>com.sxt.po</value>
</property>
</bean>

<bean id= "Hibernatetemplate" class= "Org.springframework.orm.hibernate3.HibernateTemplate" >
<property name= "Sessionfactory" ref= "Sessionfactory" ></property>
</bean>

<!--Configure a JdbcTemplate instance--
<bean id= "JdbcTemplate" class= "Org.springframework.jdbc.core.JdbcTemplate" >
<property name= "DataSource" ref= "DataSource"/>
</bean>

<!--configuration Transaction Management Note 3 --
<bean id= "Txmanager" class= "Org.springframework.orm.hibernate3.HibernateTransactionManager" >
<property name= "Sessionfactory" ref= "Sessionfactory" ></property>
</bean>
<tx:annotation-driven transaction-manager= "Txmanager"/>
<aop:config>
<aop:pointcut expression= "Execution (public * com.sxt.service.impl.*.* (..))" id= "Businessservice"/>
<aop:advisor advice-ref= "Txadvice" pointcut-ref= "Businessservice"/>
</aop:config>
<tx:advice id= "Txadvice" transaction-manager= "Txmanager" >
<tx:attributes>
<tx:method name= "find*" read-only= "true" propagation= "not_supported"/>
<!--get starts with a method that does not need to run in a transaction.
In some cases, it is not necessary to use transactions, such as fetching data. Turning on the transaction itself has a certain impact on performance--
<tx:method name= "*"/> <!--other ways to run in practice
</tx:attributes>
</tx:advice>

</beans>


Hibernate configuration is mainly the!!! :

The package to scan (hibernate annotations);

data source; sessionfactory;

Hibernatetemplate;

Configure things (Get data does not start a transaction)


NOTE 2:

<!--introduce a property file--
<context:property-placeholder location= "Classpath:dbconfig.properties"/>

This gets the value ${JDBC.URL.JEECG}

Db.properties Content
Hibernate.dialect=org.hibernate.dialect.mysqldialect
Validationquery.sqlserver=select 1
Jdbc.url.jeecg=jdbc:mysql://localhost:3306/projectdb?useunicode=true&characterencoding=utf-8
Jdbc.username.jeecg=root
Jdbc.password.jeecg=xlj123
Jdbc.dbtype=mysql


Note 3:

<!--Configure the things manager, write @transactional in *serviceimpl can enable the management of things, in the search method plus read-only read-only, @Transactional (Readonly=true, propagation=propagation.not_supported)//query does not need to open a transaction--
<bean name= "TransactionManager"
class= "Org.springframework.orm.hibernate4.HibernateTransactionManager" >
<property name= "Sessionfactory" ref= "Sessionfactory" ></property>
</bean>
<tx:annotation-driven transaction-manager= "TransactionManager"/>



Service-config.xml

<?xml version= "1.0" encoding= "UTF-8"?>
<beans xmlns= "Http://www.springframework.org/schema/beans"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
Xsi:schemalocation= "
Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-2.5.xsd ">

<bean id= "UserService" class= "Com.sxt.service.UserService" >
<property name= "Userdao" ref= "Userdao" ></property>
</bean>

</beans>

Dao-config.xml

<?xml version= "1.0" encoding= "UTF-8"?>
<beans xmlns= "Http://www.springframework.org/schema/beans"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
Xsi:schemalocation= "
Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-2.5.xsd ">

<bean id= "Userdao" class= "Com.sxt.dao.UserDao" >
<property name= "Hibernatetemplate" ref= "Hibernatetemplate" ></property>
</bean>
</beans>


Summary of essays-SPIRNGMVC

Related Article

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.