Hibernate的配置及CURD

1.映射文件

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

<!--DTD约束-->
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <!-- 建立类与表的映射 --> <class name="实体类的全路径" table="数据库的表名"> <!-- 建立主键与类中属性的对应 --> <id name="cust_id" column="cust_id"> <generator class="native"/> </id>
    <!--普通属性与字段的对应-->
<property name="cust_name" column="cust_name" /> <property name="cust_source" column="cust_source" /> <property name="cust_industry" column="cust_industry" /> <property name="cust_level" column="cust_level" /> <property name="cust_phone" column="cust_phone" /> <property name="cust_mobile" column="cust_mobile" /> </class> </hibernate-mapping>

 【class标签的配置】

 标签用来建立类与表的映射关系

 属性:


 name                :类的全路径


table                 :表名(类名与表名一致,table可以省略)


catalog              :数据库名


【id标签的配置】


 标签用来建立类中的属性与表中的主键的对应关系


 属性:


 name                :类中的属性名


 column              :表中的字段名(类中的属性名和表中的字段名如果一致,column可以省略)


 length               :长度


 type                   :类型

 

2.核心配置文件

<?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>
        <!-- 连接数据库的基本参数 -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql:///数据库名</property>
        <property name="hibernate.connection.username">用户</property>
        <property name="hibernate.connection.password">密码</property>
        <!-- 配置Hibernate的方言 -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        
        <!-- 可选配置================ -->
        <!-- 打印SQL -->
        <property name="hibernate.show_sql">true</property>
        <!-- 格式化SQL -->
        <property name="hibernate.format_sql">true</property>
        <!-- 自动创建表 -->
        <property name="hibernate.hbm2ddl.auto">update</property>
    
        
        <mapping resource="映射文件的全路径"/>
    </session-factory>
</hibernate-configuration>
3.Demo案例
public class Demo1 {
    @Test
    public void test() {
        // 1.加载核心配置文件
        Configuration configuration = new Configuration().configure();
        // 2.创建sessionFactory对象:类似JDBC连接池
        SessionFactory sessionFactory = configuration.buildSessionFactory();
        // 3.通过seeionFactory得到session:类似JDBCConnection
        Session session = sessionFactory.openSession();
        // 4.手动开启事务
        Transaction transaction = session.beginTransaction();
        // 5.编写代码
        Customer customer = new Customer();
        customer.setCust_name("乐乐");
        session.save(customer);
        // 6.事务提交
        transaction.commit();
        // 7.资源释放
        session.close();
        sessionFactory.close();
    }
}

 


---------------------------------------------------------------------------------------------------------------------------
4.重要的API

i1 Configuration
i2 SessionFactory
i3 Session
i4 Transaction

查询

  T get(Class c,Serializable id);


  T load(Class c,Serializable id);

修改

 void update(Object obj);

  删除

 

  void delete(Object obj);s


 保存或更新


 void saveOrUpdate(Object obj)






 

posted @ 2018-12-31 16:39  Binary_tony  阅读(221)  评论(0编辑  收藏  举报