Spring Frame Fourth Day

Source: Internet
Author: User

Spring Framework's fourth day case one: SSH Framework consolidation Save Customer

Demand analysis

1. 案例一:SSH框架整合保存客户
Integration of the SSH framework for technical analysis

A basic review of the development of SSH framework for technical analysis

Technical analysis of the three big SSH frameworks required by the JAR package

1. Struts2框架    * struts-2.3.24\apps\struts2-blank\WEB-INF\lib\*.jar        -- Struts2需要的所有jar包    * struts2-spring-plugin-2.3.24.jar                          ---Struts2整合Spring的插件包2. Hibernate框架    * hibernate-release-5.0.7.Final\lib\required\*.jar          -- Hibernate框架需要的jar包    * slf4j-api-1.6.1.jar                                       -- 日志接口    * slf4j-log4j12-1.7.2.jar                                   -- 日志实现    * mysql-connector-java-5.1.7-bin.jar                        -- MySQL的驱动包3. Spring框架    * IOC核心包    * AOP核心包    * JDBC模板和事务核心包    * Spring整合JUnit测试包    * Spring整合Hibernate核心包    * Spring整合Struts2核心包

Technical Analysis of SSH configuration files required by the three major frameworks

1. STRUTS2 Framework * Configure core Filters <filter> <filter-name>struts2</filter-name> in Web. xml        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <u Rl-pattern>/*</url-pattern> </filter-mapping> * Create struts.xml in src directory to configure Action2. Hibernate Framework * Creates a hibernate.cfg.xml profile in the SRC directory * under the JavaBean package that is located under the configuration file 3. Spring Framework * Configure the Integrated Web listener <listener> <listener-class>org.springframework.web.context in XML . contextloaderlistener</listener-class> </listener> <context-param> <param-na Me>contextconfiglocation</param-name> <param-value>classpath:applicationcontext.xml</param-va Lue> </context-param> * Create applicationcontext.xml in src directory * in src directoryNext Log4j.proerties 

Spring Framework for Technical Analysis Integration STRUTS2 Framework

1. Import the CRM Project UI page, find the page to add the customer, modify the form form, and access the Action2. Write a Customeraction receive request, complete the configuration of the action in Struts.xml <package name= "CRM" extends= "Struts-default" namespace= "/" > & Lt;action name= "customer_*" class= "com.itheima.web.action.CustomerAction" method= "{1}" > </action> </ Package>3. Get to service in action (development will not be used because of trouble) * Can be webapplicationcontextutils.getwebapplicationcontext ( Servletactioncontext.getservletcontext ()); To get, but writing code in this way is too much trouble!! 4. Spring is the first way to integrate the STRUTS2 framework (action is created by the STRUTS2 framework) * Because the imported Struts2-spring-plugin-2.3.24.jar package comes with a configuration file Struts-plugin.xml  , the configuration file has the following code * <constant name= "struts.objectfactory" value= "Spring"/> To open a constant, if the constant is turned on, then the following constants can be used * Struts.objectFactory.spring.autoWire = name, the constant is the class of action that can be used to automatically assemble bean objects!! 5. Spring's second way of consolidating the STRUTS2 framework (action is created by the Spring Framework) (recommended for use by everyone) * Put the specific action class profile in the config file applicatoncontext.xml, but note: Struts.xml needs to be modified * Applicationcontext.xml * <bean id= "Customer Action "class=" Com.itheima.web.action.CusTomeraction "scope=" prototype "> * struts.xml changes, change the full path to ID value * <action name=" customer_* "class=" customeract Ion "method=" {1} "> * The second way requires two notes * Spring framework default generation Customeraction is Singleton, and STRUTS2 framework is multi-instance. So you need to configure Scope= "prototype" * CustomerService now you have to manually inject the

The spring framework of the

Technical Analysis integrates the Hibernate framework (a configuration file with Hibernate.cfg.xml. Emphasis: cannot be bound when the configuration of the front thread)

1. Write the Customerdaoimpl code, add the configuration and complete the injection 2 in the Customerserviceimpl. Write the mapped configuration file, and introduce the mapped profile 3 in the Hibernate.cfg.xml configuration file. In the Applicationcontext.xml configuration file, configure the load hibernate.cfg.xml configuration <bean id= "Sessionfactory" class= " Org.springframework.orm.hibernate5.LocalSessionFactoryBean "> <property name=" configlocation "value=" Classpath:hibernate.cfg.xml "/> </bean>4.    To complete the addition of data in Customerdaoimpl, the Spring Framework provides a Hibernatedaosupport tool class that can be inherited by DAO in the future!!            public class Customerdaoimpl extends Hibernatedaosupport implements Customerdao {public void Save (Customer c) {            SYSTEM.OUT.PRINTLN ("Persistent layer ...");        This.gethibernatetemplate (). Save (c); }} <bean id= "Customerdao" class= "Com.itheima.dao.CustomerDaoImpl" > <property name= "sessionfactory" ref= "Sessionfactory"/> </bean>5. Turn on the configuration of the transaction * First configure the transaction manager, note that you are using the Hibernate framework now, so you need to use the Hibernate Framework transaction manager <bean id= "TransactionManager" class= "Org.sprin Gframework.orm.hibernate5.HibernateTransactionManAger "> <property name=" sessionfactory "ref=" sessionfactory "/> </bean> * Open annotation Transaction <tx:annotation-driven transaction-manager= "TransactionManager"/> * Add transaction annotations in the service class @Transactional

Spring Framework for technical Analysis integrates Hibernate framework (configuration file without hibernate.cfg.xml)

1. Hibernate config file * database connection basic parameters (4 large parameters) * Hibernate related properties * Connection pool * Map file 2. Start configuration * Configure connection pool related information <bean id= "DataSource" class= "Com.mchange.v2.c3p0.ComboPooledDataSource" > & Lt;property name= "Driverclass" value= "Com.mysql.jdbc.Driver"/> <property name= "Jdbcurl" value= "Jdbc:mysql :///xxx "/> <property name=" user "value=" root "/> <property name=" password "value=" root "/&        Gt </bean> * Modify the Localsessionfactorybean property configuration because there is no hibernate.cfg.xml configuration file, so you need to modify the configuration to inject the connection pool <bean id= "Tran Sactionmanager "class=" Org.springframework.orm.hibernate5.HibernateTransactionManager "> <property name=" D Atasource "ref=" DataSource "/> </bean> * continue to be configured in Localsessionfactorybean, use the Hibernateproperties property to continue to configure additional property, note that the value is properties properties file <!--Configure additional attributes--<property name= "Hibernateproperties" > <prop s> <prop key= "Hibernate.dialect" &Gt;org.hibernate.dialect.mysqldialect</prop> <prop key= "Hibernate.show_sql" >true</prop> <prop key= "Hibernate.format_sql" >true</prop> <prop key= "Hibernate.hbm2ddl.auto" >update</prop> </props> </property> <!--configuration Map--<property Name= "Mappingresources" > <list> <value>com/itheima/domain/customer.hbm.xml</va Lue> </list> </property>

Frequently used methods for technical analysis of Hibernate templates

1. 增删改的操作:    * 添加:        * save(Object obj);    * 修改:        * update(Object obj);    * 删除:        * delete(Object obj);2. 查询的操作:    * 查询一条记录:        * Object get(Class c,Serializable id);        * Object load(Class c,Serializable id);3. 查询多条记录:    * List find(String hql,Object... args);

Delay loading problem of technical analysis

1. 使用延迟加载的时候,再WEB层查询对象的时候程序会抛出异常!    * 原因是延迟加载还没有发生SQL语句,在业务层session对象就已经销毁了,所以查询到的JavaBean对象已经变成了托管态对象!    * 注意:一定要先删除javassist-3.11.0.GA.jar包(jar包冲突了)2. 解决办法非常简单,Spring框架提供了一个过滤器,让session对象在WEB层就创建,在WEB层销毁。只需要配置该过滤器即可    * 但是:要注意需要在struts2的核心过滤器之前进行配置        <filter>            <filter-name>OpenSessionInViewFilter</filter-name>            <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>        </filter>        <filter-mapping>            <filter-name>OpenSessionInViewFilter</filter-name>            <url-pattern>/*</url-pattern>        </filter-mapping>

Spring Frame Fourth Day

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.