SSH Integration two hibernate standalone setup

Source: Internet
Author: User

1. First we need to copy the jar package that our hibernate needs


We also need to add our C3P0 jar package as we use the C3P0 connection pool in Hibernate

2. Write our relational mapping file Customer.cfg.xml (here the Customer.cfg.xml and the customer entity are similar level)

<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE hibernate-mapping Public
"-//hibernate/hibernate Mapping DTD 3.0//en"
"Http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" >
<class name= "Com.itheima.entity.Customer" table= "Cst_customer" >
<id name= "CustId" column= "cust_id" >
<generator class= "native" ></generator>
</id>
<property name= "CustName" column= "Cust_name" ></property>
<property name= "Custsource" column= "Cust_source" ></property>
<property name= "Custindustry" column= "Cust_industry" ></property>
<property name= "Custlevel" column= "Cust_level" ></property>
<property name= "custaddress" column= "cust_address" ></property>
<property name= "Custphone" column= "Cust_phone" ></property>
</class>

3. Write our hibernate core configuration file Hibernate.cfg.xml

<?xml version= "1.0" encoding= "UTF-8"?>

<! DOCTYPE hibernate-configuration Public

"-//hibernate/hibernate Configuration DTD 3.0//en"
"Http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd" >
<session-factory>
<property name= "Hibernate.connection.driver_class" >com.mysql.jdbc.Driver</property>
<property name= "Hibernate.connection.url" >jdbc:mysql:///ssh_280</property>
<property name= "Hibernate.connection.username" >root</property>
<property name= "Hibernate.connection.password" >root</property>
<property name= "Hibernate.dialect" >org.hibernate.dialect.MySQLDialect</property>
<!--Configure C3P0 connection pool--
<property name= "Hibernate.connection.provider_class" >org.hibernate.connection.c3p0connectionprovider</ Property>
<property name= "Hibernate.hbm2ddl.auto" >update</property>

<!--display SQL statements Show_sql true|false (default)--
<property name= "Hibernate.show_sql" >true</property>

<!--formatted SQL Format_sql true|false (default)--
<property name= "Hibernate.format_sql" >true</property>

<!--gets the session bound to the current thread -

<property name= "Hibernate.current_session_context_class" >thread</property>
<!--the location of the mapping file
Class: Specifies the fully qualified class name of the entity class that has the JPA annotations configured
Resource:xml-Formatted mapping file
-
<mapping resource= "Com/itheima/entity/customer.hbm.xml"/>
</session-factory>

4. Create our database ssh_280

/* Create Customer table */
CREATE TABLE ' Cst_customer ' (
' cust_id ' BIGINT (+) not NULL auto_increment COMMENT ' customer number (primary key) ', ' cust_name ' varchar (+) NOT null COMMENT ' customer name (company name) ',
' cust_source ' varchar (+) DEFAULT NULL COMMENT ' Customer information source ',
' cust_industry ' varchar (+) default NULL COMMENT ' Customer industry ',
' cust_level ' varchar (+) default NULL COMMENT ' client level No ',
' cust_address ' varchar (+) default null COMMENT ' customer contact address ',
' cust_phone ' varchar (+) default null COMMENT ' Customer Contact phone ',
PRIMARY KEY (' cust_id ')
) Engine=innodb auto_increment=94 DEFAULT Charset=utf8;

/* Add Data */
INSERT into ' Cst_customer ' VALUES (' 1 ', ' Wisdom Podcast Group ', ' 6 ', ' It Training ', ' 23 ', ' Beijing Changping District North seven home town revision building ', ' 010-66668888 ');
INSERT into ' Cst_customer ' VALUES (' 2 ', ' Black Horse Training camp ', ' 6 ', ' It Training ', ' 23 ', ' Beijing Changping District North seven home town revision building ', ' 010-66668888 ');
INSERT into ' Cst_customer ' VALUES (' 3 ', ' Jingxi Group ', ' 7 ', ' e-commerce ', ' 23 ', ' Jingxi Yuquan Mountain ', ' 010-65085588 ');
INSERT into ' Cst_customer ' VALUES (' 4 ', ' correction medicine ', ' 7 ', ' medicine ', ' 22 ', ' Beijing Changping District North seven Town ', ' 010-68909090 ');

5. Write our test class

  public class Hibernatetest {

@Test
public void Addcustomertest () {
Load configuration Hibernate's core configuration file
Configuration cfg = new configuration ();
Cfg.configure ();
Create Sessionfactory
Sessionfactory factory = Cfg.buildsessionfactory ();
Get Session Object
Session session = Factory.getcurrentsession ();
Open transaction
Transaction tx = Session.begintransaction ();
Performing an action crud
Customer customer = new Customer ();
Customer.setcustname ("Zhang Fei");
Session.save (customer);
Commit a transaction
Tx.commit ();
}
}

Finally, let's look at the results of the operation

I hope I can make a little progress every day!

SSH Integration two hibernate standalone setup

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.