This article does not want to introduce the principle of struts,spring,hibernate system architecture, etc., the purpose of this article is to introduce a more complex example of how to integrate struts,spring,hibernate, online existing examples, although also can achieve the goal, but the function is relatively single , there are unexpected problems with complex examples. This article has already possessed the basic knowledge of the above framework for the reader hypothesis. And the basic concepts that have been known about struts,spring,hibernate, but have not yet experienced struts+spring+hibernate developers in more complex projects. 1
StrutsAlthough not too much to introduce the principle of struts, but the introduction is still necessary. Struts itself is MVC. Here is responsible for the user data descendant business layer, and the business layer processing results returned to the user, this system belongs to a simpler Web application, using the Opensessioninview mode to deal with lazyload problem, so we can use in the user view Get,set method to easily get the associated object. In order to handle the huge action and actionform problems, we are ready to use Dynaactionform (Dynavalidatorform) and dispatchaction as well as dynamic validation framework to solve this problem. and use tile to solve the framework problem. Use custom labels to work with paging and authentication issues.
2 SpringThe spring framework is best known for its seamless links with hibernate, although spring offers more than 90% packages to hibernate, so we don't have to care about the session's establishment, shutdown, and transactions that allow us to focus on the business logic. But some special situations like query and Criteria objects, paging, and so on, spring can't support us, and you can't always write a hibernatecallbackup on your DAO. The role of Spring is not to encapsulate a layer of Hibernate, but to get you into contact with Hibernate APIs, but to help you manage sessions and Transaction. Here the solution is: first write a IBase interface, and a Basedao implementation. In the implementation of the hibernatetemplate, the implementation of its functions, while taking into account where spring is not supported, we have to manage the session ourselves, so join the public session opensession (), Public Query getquery (String sql), public Criteria Getcriteria (Class clazz), and pagination methods. Then for each entity is established to inherit from the above class of ientity, with Entitydao. Here you can add special method implementations to entity, such as adding similar user authentication to Studentsdao.java. The above is the data access layer. Next, the business logic method is completed in the service layer through a reference to DAO. In the following example we are the Student module, teacher module, Administrator module to build service layer, Studentsserviceimpl,teachersserviceimpl,adminserviceimpl.
3 HibernateWith spring's encapsulation, what we want to do with hibernate is to properly implement the mapping of object relationships. Because it is at the bottom of the system, the accurate implementation of the association mapping between objects will play a crucial role. In short, after understanding the struts,spring,hibernate principle and the relationship, the rest of the work is like building blocks in a framework that is represented by struts with Spring as its core. The diagram below can help us understand the relationship between the struts,spring,hibernate better.
twoCase Brief: The design idea mainly originates from the university elective course, this system may facilitate the processing student in the course Xuan, the credit inquiry, the result inquiry, as well as the achievement release and so on. The system takes the class as the core, one course can correspond to many classes, a teacher may also bring the different class, the student may Xuan the different curriculum corresponding class, the class own present number, and the maximum number, as well as the class time, the class place attribute. After the students Xuan the class, the number of the classes will be automatically added until the maximum number is equal, and the other students will have a full number of false hints. Similarly, if a student chooses a different class in the same course, he or she will receive an error message. Students have passwords, departments, credits, addresses, telephone and other attributes. Teachers in the system mainly responsible for the release of results, teachers can change the students ' grades in their class, and the system will judge whether the students pass the exam by the result of 60来, if the course will be added to the students ' credits, if the teacher has modified the grade two times, and less than 60, The system will deduct the course score from the student credits. The curriculum is embodied in the system as a class, with its own credit attributes.
The system has a number, name attributes, and can be used as a link between teachers, courses, students and bridges.
Functional Modules• Authentication module: Forwards users to different modules according to user name, password, user category. L Student Module: View the course, view the class, Xuan course, view the selected courses, score query. L Teacher module: Input score L Admin module: For students, teachers, courses, classes, the department of Increase, delete, check, change.
threeConcrete Practice code Download Http://www.blogjava.net/Files/limq/StudentManger.rar 1Object Relational Mappings:First, the library table is mapped to the data model (SQL is viewed in the source code), and the converted data model is shown below:
From this we can see the association relationship: 1 Students and Contacts (contact) a pair of relations. 2 Students and History (elective history) One-to-many Relationship 3 Students and Classes many-to-many relationships. 4 Classes and Classes_info a one-to-many relationship. 5 Classes and Teachers are more than one relationship. 6 Classes and courses are more than one relationship. 7 Course and Department (lines) of a many-to-many relationship. 8 Teachers and Department are more than one relationship. 9 Students and Department are more than one relationship. In hibernate, the above relationship one by one maps, such as Students and History one-to-many relationships Students.cfg.xm.: 1 < set name = "History"
2 table = "History"
3 Cascade = "All"
4 inverse = "true"
5 lazy = "true" >
6 < key column = "student_id"/>
7 < One-to-many class = "Limq.hibernate.vo.History"
8/>
9 set>
10 with MyEclipse development hibernate know that MyEclipse will help us to generate persistent objects and abstract objects, we want to add a reference to history in the Students.java private Set history=new HashSet (); Public Set Gethistory () {return history; The public void Sethistory (Set history) {this.history = history;} In the meantime, delete Abstracthistory.java in student_id and The Get,set method should be history.java into the private Students student; Public Students getstudent () {return student; public void Setstudent (Students student) {this.student = student; Check the source code for specific content.
1 public Interface Idepartment extends Ibasedao {}
2
3 public class Departmentdao extends Basedao implements Ibasedao {}
4
3 ServiceLayerHere you need to think carefully about the persistence Layer objects and DAO that each business logic can use, and to complete the configuration Spring framework, first I'll take a look at Applications-service.xml
1 XML version= "1.0" encoding= "UTF-8"?>
2 DOCTYPE Beans Public "-//spring//dtd bean//en"
3 "HTTP://WWW.SPRINGFRAMEWORK.ORG/DTD/SPRING-BEANS.DTD" >
4 <beans>
5 <bean id= "DataSource" class= "Org.apache.commons.dbcp.BasicDataSource" destroy-method= "Close" >
6 <property name= "Driverclassname" >
7 <value>com.mysql.jdbc.Drivervalue>
8 property>
9 <property name= "url" >
Ten <value>jdbc:mysql://localhost:3306/Studentvalue>
One property>
<property name= "username" >
<value>rootvalue>
Property>
<property name= "Password" >
<value>value>
Property>
Bean>
<bean id= "Sessionfactory" class= "Org.springframework.orm.hibernate.LocalSessionFactoryBean" >
<property name= "DataSource" >
<ref local= "DataSource"/>
Property>
<property name= "Mappingresources" >
<list>
<value>limq/hibernate/vo/Admins.hbm.xmlvalue>
Num <value>limq/hibernate/vo/Classes.hbm.xmlvalue>
<value>limq/hibernate/vo/Courses.hbm.xmlvalue>
<value>limq/hibernate/vo/Students.hbm.xmlvalue>
<value>limq/hibernate/vo/ClassesInfo.hbm.xmlvalue>
<value>limq/hibernate/vo/Contact.hbm.xmlvalue>
<value>limq/hibernate/vo/Department.hbm.xmlvalue>
<value>limq/hibernate/vo/History.hbm.xmlvalue>
<value>limq/hibernate/vo/Teachers.hbm.xmlvalue>
List>
Km property>
<property name= "Hibernateproperties" >
Panax Notoginseng <props>
<prop key= "Hibernate.dialect" >net.sf.hibernate.dialect.MySQLDialectprop>
<prop key= "Hibernate.show_sql" >trueprop>
Props>
Property>
Bean>
<bean id= "Mytransactionmanager" class= "Org.springframework.orm.hibernate.HibernateTransactionManager" >
<property name= "Sessionfactory" >
<ref local= "Sessionfactory"/>
Property>
47
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.