hibernate just started

As the saying goes, today's work is done today, and if it's not done today, there's nothing wrong with pushing it to tomorrow.

Cool, I'm really all kinds of pop-ups,

I used to use cool me to watch Jolin Tsai and Jay Chou's MVs in the third year of junior high

 

The document given has no word document, so annoying

think for yourself

 

What did you learn today

hibernate

encapsulated database

 

start from scratch

Code is not difficult

just a little more steps

 

1

Pilot pack, 9 packs, run,

9 packs,

hibernate3.jar a

In lib : one in jpa , 6 in required

Database Links : 1a

ok , exactly 9

By the way, it can be used as a small bottle

 

2

Create xml file under entity class

(By the way, I want to build a very large, very large database)

The name is not the name of the database

For example,  User_hbm.xml

The meaning of user hibernate mapping ~

What about the constraint dtd file, the last one under the hibernate3.jar package

hibernate-mapping-3.0.dtd ~

The jar package import is complete

Then start writing files

 

<hibernate-mapping>
<class name=”com.domain.User” table=”user”>
<! --The database is not used at all like this~! -->
<id name=”id” colunm=”id>
<generator class=”native”></generatro>
</id>
<!-- Oh, I want to write a non-primary key constraint give it a try-->
<property name=”username” column=”username”></property>
</class>
</hibernate-mapping>
3

 

 

ok write the second xml file

The constraint file is also the penultimate one, create this xml file under src

This xml name is very fixed ~ called

hibernate.cfg.xml

 

 

 

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">admin</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hbm2</property>
<!-- Display the sql statement generated by hibernate -->
<property name="hibernate.show_sql">true</property>
<!-- Display the sql statement after formatting -->
<property name="hibernate.format_sql">true</property>
<!-- hibernate will not automatically commit transactions by default. If you want to automatically commit transactions, you need to set -->
<!-- <property name="hibernate.connection.autocommit">true</property> -->
<!-- Database table generation strategy-->
<property name="hibernate.hbm2ddl.atuo">update</property>
<!-- Get the Session object bound to the current thread-->
<property name="hibernate.current_session_context_class">thread</property>
<!-- Indicate the location and name of the mapping file-->
<mapping resource="com/qianfeng/domain/User_hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
 

 

 

 

 

Hey, the format brush is still easy to use ~~

 

3

Okay, let's finally start writing.

 

It's not too difficult to write,

 

Configuration config = new Configuration().configure();
SessionFactory factory = config.buildSessionFactory();
Session session =factory.openSession();
 
session.beginTransaction();
session.getTransaction().commit();
 
session.save(user);//Increase
 
User user=(User )Session.get(User.class,1);
session.update(user);//Modify
 
User user=(User )Session.get(User.class,1);
session.delete(user);//Delete
 

 

 

Inquire! A lot of inquiries, oh boy ~

 

get

load ( delayed query)

 

 

Query query = session.createQuery(“from com qianfeng.domain.User where id=1”);
User user = (User)query.uniqueResult();

 

 

 

不想写了 等会写吧

一颗想创建大表的心扑通扑通的在跳动~

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326570864&siteId=291194637