Struts2+spring+hibernate Step by step 06 integration Hibernate

Source: Internet
Author: User
Tags one table

Note: This series of tutorials. Part of the content from Wang Jian teacher to write SSH integration development tutorial

Hibernate is an excellent ORM (object Relation mapping-Objects relational Mapping) tool. One of the three best-known frameworks for the struts, spring project, and the Java world.

Hibernate is primarily a mapping from tables to Java classes (objects).

This section of code takes the code described earlier.

Step one: Integrate hibernate with all the packages copied to the Web-inf/lib folder:


Figure-1

Step two: Under the Com/xuzheng/model folder, create a mapping class file for the users table User.java such as the following:

Tip: Write a mapping file for the Hibernate class. A class file maps a data table, because at the moment we have only one table in the SSH database. So just create a file to be able to.

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvehv6agvuz19qyxzh/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast "/>

Figure-2

Step three: Write the hibernate mapping file. Hibernate's mapping file ends with a. Hbm.xml For example the following:

Tip: It is recommended that you place the mapping files and classes under the same folder


Figure-3

User.hbm.xml source files such as the following:

<?xml version= "1.0"?

><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">

Description:

1, to <! Document type definitions beginning with DOCTYPE

2. Define this user class with the Users table in <class></class>

3, <generator></generator> for declaring the primary key

4. <property></property> declare other properties

Fourth step: Change the Applicationcontext.xml file, configure hibernate for example the following:

<!--1, before gazing at spring's transaction management--><!--<bean id= "TransactionManager" class= " Org.springframework.jdbc.datasource.DataSourceTransactionManager "><property name=" datasource "ref=" DataSource "></property></bean>--><!--2, sessionfactory--><bean id= of the declaration of Hibernate Sessionfactory "class=" Org.springframework.orm.hibernate3.LocalSessionFactoryBean "> <!--2.1| injection DataSource-- > <property name= "dataSource" ref= "DataSource" ></property> <property name= "Hibernateproperties" > <props> <!--2.2, declaring hibernate properties--<prop key= "Hibernate.dialect" > org.hibernate.dialect.mysql5dialect</prop> <prop key= "Hibernate.show_sql" >true</prop> <prop key= "Hibernate.current_session_context_class" >thread</prop> </props> </property> < Property name= "Mappingresources" > <list> <!--2.3, configure Hbm.xml profile and <value>com/xuzheng/model/ User.hbm.xml</value> </list>   </property></bean><!--3, TransactionManager--><bean id= "TransactionManager" stated in Hibernate class= "Org.springframework.orm.hibernate3.HibernateTransactionManager" ><property name= "sessionfactory" ref = "Sessionfactory" ></property></bean>
Tips: change and configure in accordance with the


Fifth step: After the configuration is good. Publish the Project Initiation program. Until you can start the project normally. If it does not start properly, you should check the file for errors.


Sixth step: Change Onedaoimpl.java, change the HQL statement to use Hibernate to query, such as the following:

Package Com.xuzheng.dao.impl;import Java.sql.types;import Java.util.list;import java.util.map;import Javax.sql.datasource;import Org.springframework.jdbc.core.jdbctemplate;import Org.springframework.jdbc.core.sqlparameter;import Org.springframework.jdbc.object.sqlfunction;import Org.springframework.orm.hibernate3.support.hibernatedaosupport;import Com.xuzheng.dao.ionedao;import Com.xuzheng.model.user;public class Onedaoimpl extends Hibernatedaosupport implements IONEDAO{//1, adding inheritance to class HIBERNATEDAOSUPPORT//2, remove the original injected Datasourcepublic map login (map map) {String name = (string) map.get ("name"); string pwd = (string) map.get ("pwd"),//3, Declaration HQL statement, note in: Namestring sql = "SELECT COUNT (1) from users where Name=:name and P wd=:p WD ";//4, use session query List temp = GetSession (). CreateQuery (SQL). SetString (" name ", name). setString (" pwd ", pwd). List (); int i = Integer.parseint (temp.get (0). toString ()); System.out.println ("Number of users of this username:" +i), if (I >= 1) {map.put ("result", "1");} return map;} Querying all User lists public list query ({///5, rewrite the Query method list List = GetSession (). Createcriteria (User.class). List (); return list;}} 
hint: According to the sequence of the order to achieve

Seventh Step: Change the Onedao in the Applicationcontext.xml file for example the following:

<bean id= "Onedao" class= "Com.xuzheng.dao.impl.OneDaoImpl" ><property name= "sessionfactory" ref= " Sessionfactory "></property></bean>

Tip: Inject the original datasource attribute. Change into injected sessionfactory
Eighth step: Publish the execution project. See if you can start and interview normally.


SOURCE Download:

http://download.csdn.net/detail/u014548782/7218965



Struts2+spring+hibernate Step by step 06 integration Hibernate

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.