hibernate----初体验

2017-05-25  本文已影响0人  倬永

hibernate 主要是和数据库打交道,管理实体类的持久化操作
操作步骤
1.导入hibernate包,如果用maven就方便多了
2.创建实体类
package entity;

public class User {

private int id;
private String username;
private String password;
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getUsername() {
    return username;
}
public void setUsername(String username) {
    this.username = username;
}
public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}

}

3.配置实体类的映射文件 xxx.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="entity">

<class name="User" table="user">
    <id name="id" column="id" type="integer">
        <generator class="native"/>
    </id>
    <property name="username"/>
    <property name="password"/>
</class>

</hibernate-mapping>
4.配置hibernate的映射文件 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">

<hibernate-configuration>

<session-factory>

    <!-- Database connection settings -->
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql:///shopping</property>
    <property name="connection.username">root</property>
    <property name="connection.password">123456</property>


<property name="hbm2ddl.auto">create</property>

<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>

    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>
        <property name="format_sql">true</property>
   

    <mapping resource="entity/Pass.hbm.xml"/>
  

</session-factory>

</hibernate-configuration>

5.写测试文件
package text;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import entity.User;

public class text01 {

public static void main(String[] args) {
    Configuration config=new Configuration().configure("hibernate.cfg.xml");
    SessionFactory sessionFactory=config.buildSessionFactory();
    Session session=sessionFactory.openSession();
    Transaction transaction=session.beginTransaction();
    User user =new User();
    user.setUsername("AAA");
    user.setPassword("123456");
    session.save(user);
    session.delete(user);
    transaction.commit();
    session.close();
    
    
}
public static void delete(User user ){
    Configuration config=new Configuration().configure("hibernate.cfg.xml");
    SessionFactory sessionFactory=config.buildSessionFactory();
    Session session=sessionFactory.openSession();
    Transaction ts=session.beginTransaction();
    
    session.delete(1);
    
    ts.commit();
    session.close();
    
    
}

}

上一篇下一篇

猜你喜欢

热点阅读