1. Getting Started
Hibernate is dealing with databases, and it's not easy to deal with databases.
Primitive, low-level direct operation, coding amount is large, time-consuming, frame efficient
The original 1.1-point realization of things, now give a semi-finished, not on the top of the time, the focus on business, rather than the bottom
DAO layer Hibernate MyBatis are for the DAO layer of the simplified framework and delete the database, the two frameworks have advantages and disadvantages, the market is very big, you do not know, it is not appropriate
MVC Call Part Struts2.0 SPRINGMVC
Spring
Core features: The object of the way to achieve the database additions and deletions to change the operation
Soul: Map
Adding and deleting changes is the method of adjustment
The difficulty: the state of the persisted object and its transformation
2. Hibernate understanding
There are many repetitive content in the development process, can you define a class I directly to the class and attributes to him, he helped me wrap it up and give me, in the future when you need to adjust it, such as every time after the results are taken out to traverse, can define a class, each time you need to traverse, I tune it
Semi-finished products guided by this, the focus on the specific details of the project maintenance, code reuse convenience
Help to complete the underlying function The bank requires a safe speed that may not be constrained by the framework
Tiered latency speed Various method calls are deferred and performance is delayed
The mention of persistence is linked to a database
3.
Persistence: The process of translating program data between instantaneous and persistent states
For example, listen to a song, downloaded on the disk, but when I listen to the song, the data is on the CPU, the CPU is instantaneous power loss, the disk is persistent
Two ways to persist:
A, serialization
What does serialization mean?
Persist the generated object information to a file or stream, save the stream of bytes written to a binary state, and achieve platform independence
DataInputStream Dataoutstream Read-write binary files
FileInputStream Fileoutstream Read-write text file
BufferedReader FileReader character stream can also be read in a text file
What is the name Soket (socket)?
Two computers connected by network communication, to achieve the exchange of data
Persistence diagram:
Memory User Information transient state
JDBC Operations database additions and Deletions | |
Database saves a table persistence state corresponding to memory
JDBC Review:
What are the drawbacks of the JDBC operations database that have spawned so many frameworks to solve this problem?
More repetitive work
Code redundancy
Before the DAO layer has been the data encapsulated into objects to manipulate, such as the add data, only to the DAO layer, only to take it apart, or can not execute the SQL statement
In turn, the same query.
Advantages: High Efficiency
Code porting difficulties: such as database migration, such as the initial data volume is not large in MySQL, and later the data volume is large, need to transfer data to Oracle, because two database statements vary greatly, later a page of a page modification statement is very troublesome
With JDBC, before DAO is always object-oriented, with an object in the form of passing, to the DAO layer is to use SQL statements to manipulate the database, you must break the object, equal to this part is not object-oriented, Java pay attention to the low coupling, but in this part is coupled with the SQL statement, In the future, if we change the database, this part will have to be completely re-written
Now I'm not going to go directly to the underlying JDBC stuff, directly using hibernate to provide the methods, or object form to pass, do not have their own disassembly, Hibernate helped me to do the split
Cons: Analytical type, large data volume
Advantages: Transaction-type additions and deletions
ORM: Object Relational Mapping
Process data as an object when writing programs
When you save data, you store it in the form of relational data.
4. Hibernate configuration
A. Select the desired jar package: All of the jar packages in the required, JPA (persistent Interface) folder under Hibernate3.jar lib in the root directory are placed under the project's Lib file with a OJDBC6 (Oracle database-driven JAR package)
B, configuration files
The name must be called Hibernate.cfg.xml.
Under src new-->other-->myeclipse-->xml-->xml (Basic ...) Click Next Create to select the first select to select a second hibernate/configration DTD3.0
DTD file when writing XML file format, used to standardize what can be written in what is not, there will be code hints
<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-configuration Public "-//hibernate/hibernate configuration DTD 3.0//en" "http// Hibernate.sourceforge.net/hibernate-configuration-3.0.dtd ">
A few points to note:
User session: HttpSession
Hibernate and Database Exchange: Session, is Hibernate core API, do not care what to do, get the session first, through the conversation factory to get session
When you run the program, the configuration file is read before the session factory is created by configration
Persistent class is also the Entity Class PO package =entity package = Bean Package
All entity classes, data passing, caches need to be serialized, and then a sequence of versions is generated, primarily to determine whether the class you have is the same as this class.
When you define a persisted class, it corresponds to database one by one, the data type should write the wrapper class as much as possible, do not write the value type, because the database is allowed to be empty, and the value type is not allowed to be empty, unless the database also writes NOT NULL.
C, mapping files
The name must be called Book.hbm.xml.
Build a package under the project create a book class
Select the package, and then create a new XML select for the second HIBERNATE/MAPING/DTD 3.0
Finally go back to Hibernate.cfg.xml under Configuration view maping--Add Select Book.hbm.xml
Dialect setting the corresponding database for dialect selection oracle9/10g because hibernate can automatically generate additions and deletions based on dialect
In the properties that add Select Show SQL value to True function is to put the SQL statement executed by hibernate to the console output automatically; then choose Foramt SQL to format the SQL statement, easy to read
hibernate--Basic and XML configuration