Hibernate一

先安装插件 去官网 很简单 
先是导入jar包 
简单的导入hibernate的lib\required中的包

然后对类进行与表的绑定

创建一个.hbm.xml 
一:orm Object Relational Mapping 
对象关系映射

<hibernate-mapping package="crm.domain">
    <class name="Customer" table="cst_customer">
        <id name="cust_id" column="cust_id">
            <generator class="identity" />
        </id>
        <property name="cust_name" type="java.lang.String">
            <column name="cust_name" />
        </property>
        <property name="cust_create_id" type="java.lang.String">
            <column name="cust_create_id" />
        </property>
        <property name="cust_source" type="java.lang.String">
            <column name="cust_source" />
        </property>
        <property name="cust_industry" type="java.lang.String">
            <column name="cust_industry" />
        </property>
        <property name="cust_level" type="java.lang.String">
            <column name="cust_level" />
        </property>
        <property name="cust_linkman" type="java.lang.String">
            <column name="cust_linkman" />
        </property>
        <property name="cust_phone" type="java.lang.String">
            <column name="cust_phone" />
        </property>
        <property name="cust_mobile" type="java.lang.String">
            <column name="cust_mobile" />
        </property>
    </class>
</hibernate-mapping>
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

hibernate-mapping:有个package属性 写了之后下边就不用写全包名了

class:中有类名 还有对应的包名 name 和table

这里有个id属性 也就是主键 
class=”identity” 中间有7种 这里写三种吧 别的没看懂….(以后再加) 
1、identity:用于MySQL数据库。 
特点:递增 
注:对于mysql数据库使用递增序列时需要在建表时对主键指定为auto_increment属性。

2.、sequence:用于Oracle数据库 
3、assigned:用户自定义id;

property:有name就是对应的类的属性 , 
column就是对应的列名, 
length对应数据库的长度, 
not-null就是不能为空

二:接下来就是hibernate.cfg.xml 
直接创建到src下就行了

<hibernate-configuration>
    <session-factory>
    <!--#hibernate.dialect org.hibernate.dialect.MySQLDialect #hibernate.connection.driver_class 
            com.mysql.jdbc.Driver #hibernate.connection.url jdbc:mysql:///test #hibernate.connection.username 
            gavin #hibernate.connection.password -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://192.168.1.106:3306/test?characterEncoding=utf8&amp;useSSL=false</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">123456</property>

        <!-- 不是必须的 -->
        <!-- #hibernate.show_sql true ## format SQL in log and console hibernate.format_sql 
            true #hibernate.hbm2ddl.auto update -->
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <mapping resource="crm/domain/User.hbm.xml"/>
    </session-factory>
</hibernate-configuration>
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

这里只写mysql的…. 
没啥新奇的东西 
其中有个方言hibernate.dialect 我我觉得这个可以理解为各个数据库的语句不太一样所以用这个屏蔽数据库之间的差异

再下边的是show_sql 、format_sql 在控制台打印出sql语句并格式化

下边这个hibernate.hbm2ddl.auto 有四个 
create:每次执行的话是创建新表 也就是覆盖之前的表 
create-drop:这个也是每次创建新的,执行完之后会删除表 …感觉没啥用… 
update:如果没有表就创建表,如果没有列就在表中添加新的列(贼稳妥)不会删除东西(推荐用这个) 
validate :只验证表,一致才写入数据

最后有个导入orm元数据 
mapping 
resource属性:把src下.hbm.xml的路径写到这里…

在接下来就是一个执行过程 了

//写了一个工具类
public class HibernateUtils {
    private static SessionFactory sf;
    static {
        Configuration conf = new Configuration().configure(); //配置加载类,configure()是用来加载src下的那个配置文件
         sf = conf.buildSessionFactory(); //创建session工厂类,这里的session不是servlet中的session,导入包得导入hibernate中的包
    }
    public static Session getSession() {

        Session session = sf.openSession(); //打开一个新的session

        return session;

    }
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
        //之前的就是web service dao 什么的
        Session session = HibernateUtils.getSession();//得到session
        Transaction ts = session.beginTransaction(); //获得并开启事务
        session.save(user);//把从web层传过来的user对象写入数据库
        ts.commit();//提交事务
        session.close();//关闭session
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
0
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值