Struts + Spring + Hibernate Upload Download - 2

xiaoxiao2021-04-09  288

Data persistence layer 1, domain objects and mapping files You can use Hibernate Middle Gen, Hibernate Tools, Hibernate Syhchronizer and other tools or manual ways to write Hibernate domain objects and mapping files. The domain object TFile.java corresponding to the T_FILE table is: code 1 domain object TFile

Package sshfile.model; 2. Private string fileId; 5. Private string filename; 6. Private byte [] filecontent; 7. Private string remark; 8. ... // getter and setter9.

It is important to note that the database table is the filecontent type of the BLOB type in TFile is BYTE []. TFile's Hibernate mapping file tfile.hbm.xml is placed in the same directory of the TFile .java class file: code 2 domain object mapping file

1. 2. 5. 6. 7. 8. 9. 10. 13. ... // Other general field mapping 14. 15.

The FileContent field maps to the BLOBBYTEARRAYTYPE type provided by Spring, and BlobByteArRayType is a user-defined data type that implements hibernate's org.hibernate.UserType.Usertype interface. BlobbyteArrayType uses the LOB operating handle of the sessionFactory to save the data of Byte [] into the BLOB Database field. In this way, we have no need to pass the hard-coded way, first INSERT, then complete the persistence of BLOB type data, this original difficult experience is finally civilized. See the contents of this article for the configuration of the LobHandler. Furthermore, when Lazy = "true" is returned to the entire TFile object, the data of the FileContent this field is not returned, and only the filecontent data is obtained from the database only when the TFile.GetFileContent () method is explicitly invoked. This is the new feature introduced by hibernate3. For table fields containing heavyweight data, this extraction method improves the flexibility of large field operations, otherwise load the results of the TFile object, if it always returns FileContent, this batch Data extraction will cause "flooding effect" of the database. 2, DAO write and configure Spring to emphasize the interface programming, so we define all the data operations of TFile in the TFiledao interface, these interface methods are: · FindbyFildId (String fileID) · Save (TFile Tfile) · List FindAll () TFiledaoHibernate provides Hibernate based on the TFiledao interface, such as code 3: Code 3 Based on Hibernate's FileDao implementation class 1. Package sshfile.dao; 2.3. Import sshfile.model. *; 4. Import Org.SpringFramework. orm.hibernate3.support.HibernateDaoSupport;. 5 import java.util.List;.... 6.7 public class TfileDAOHibernate8 extends HibernateDaoSupport implements TfileDAO9 {10. public tfile findByFildId (String fileId) 11 {12. return (tfile) getHibernateTemplate () . Get (TFile.Class, FileID); 13.} 14. Public void save (tfile tfile) 15. {16. gethibernateTemplate (). save (tfile); 17. gethibernateTemplate (). Flush (); 18.} 19 PUBLIC LIST FINDALL () 20. {21. Return gethibernateTemplate (). loadingAll (tfile.class); 22.} 23.

TfileDAOHibernate established by extending Hibernate support classes HibernateDaoSupport Spring provided, HibernateDaoSupport encapsulates HibernateTemplate, and HibernateTemplate encapsulates Hibernate provided almost all data operation method, such as execute (HibernateCallback action), load (Class entityClass, Serializable id), save Final Object Entity, etc. So our DAO only needs to simply call the HibernateTemplate of the parent class to complete almost all database operations. Since Spring completes the operation of the data layer by proxy Hibernate, the original Hibernate configuration file hibernate.cfg.xml is also transferred to the Spring profile: code 4 Spring About Hibernate configuration information

1. 2. Configuration / / -> -> 3. 6. 7. 8. 9. 10. 11. 13. 14. 15. 16. classpath: / sshfile / model 17. 18. 19. Org.hibernate.diaalect.racledialect 22. True 23. 24. 25. 26. // -> 27. 29"> "sessionFactory" ref =

"sessionFactory" /> 30. 31. 33. 34. 35. ... 36. Define a data source defined, the category is Apache's BasicDataSource, and the 11th to 25th line defines. Hibernate's session factory, session factory class with LocalSessionFactoryBean maintenance provided by Spring, which is injected into data source and resource mapping files, and the properties required to set Hibernate via some key values. The 16th line is loaded by the mapping of the class path, and the mapping file of all domain objects in the SSHFILE.MODEL class package directory comes in. In the example of this article, it will load into the TFile.hbm.xml mapping file. If multiple mapping files need to be declared, use class path mapping methods obviously easier to specify the mapping file name directly. Lines 27 ~ 30 define the HibernateTemplate template for the Spring proxy Hibernate data, while the 32-34 line is injecting the template into TFiledao. It is necessary to specify that Spring 1.2.5 provides two sets of Hibernate support packs, where Hibernate 2 is located in org.springframework.orm.hibernate2. * Pack, while Hibernate 3.0's package is located in Org.SpringFramework.orm. In the hibernate3. * Pack, you need to make correct choices based on your Hibernate version. 3, LOB field processing configured that we have pointed out that Oracle's LOB fields and general types of fields have a clear difference in operation - that is, you must first initialize the LOB field through Oracle's EMPTY_BLOB () / EMPTY_CLOB (), then Get references to this field, change its value by this reference. So to complete the operation of the LOB field, Hibernate must perform two steps of database access, first insert and then Update. After using the blobbyteaRayType field type, why can we operate the blob field like a general field type? The point that can be determined is that blobbyteaRayType cannot pass overborn manborn operation mode, which is the function of the BLOBBYTEARRAYTYPE data type itself. It hides two data accesses through LobHandler, so that the operation of the blob field is on behalf of the BLOB field. Other general fields have no different types, so LobHandler is the behind-the-scenes hero that "bitter me one, happiness". LobHandler must be injected into the Hibernate session factory sessionFactory because SessionFactory is responsible for generating session interactions with a database. LobHandler configuration, such as code 5: code 5 LOB field processing handle configuration

1. 2. ... 3. 6. 8. 9. 10. 12. ... 13. First, you must define a extractor that can extract a local database JDBC object (such as OracleConnection, OracleResultSet, etc.) from the connection pool: NativeJDBCextractor, This can do some specific database operations. For those simple data connection pools that only package only statement, SimpleNativejdbcextractor is the highest efficiency extractor implementation class, but the specific to Apache's BasicDataSource connection pool, which encapsulates all JDBC objects, then you need to use CommonsdbcPnativeJDBCextractor. . Spring provides a JDBC data source extractor for several well-known Web server: · WebLogic: WebLogicNativeJdbcExtractor · WebSphere: WebSphereNativeJdbcExtractor · JBoss: JBossNativeJdbcExtractor After defining the JDBC extractor, and then define lobHandler. Spring 1.2.5 provides two LobHandler: · DefaultLobHandler: Suitable for most databases, such as SQLServer, MySQL, also apply to Oracle 10g, but not for Oracle 9i (it seems that Oracle 9i is really a freak, who is Oracle The company all said that Oracle 9i is a transitional product). · Oraclelobhandler: Suitable for Oracle 9i and Oracle 10g. Since our database is Oracle9i, Oraclelobhandler is used. After the LobHandler is configured, it also needs to be injected into the SessionFactory's bean. The following is the configuration of the SessionFactory Bean after calling: Code 6 Injects LobHandler into the configuration in SessionFactory.

1. 2. ... 3. 6 7. 8. ... 9. 10. ... 11. As shown in Section 7, beans are injected through the sessionFactory's LobHandler attribute.

转载请注明原文地址:https://www.9cbs.com/read-132973.html

New Post(0)