学习Hibernate的第一部分(笔者一共会写四部分)

  今天开始学习SSH框架,虽然在网上看到有人评论说SSH不火了,学习SSM吧。 但是,我这个人比较老实(哈哈哈)  先从SSH开始吧(一起学习,一起努力)

  1、今天学习了Hibernate的环境配置(我不知道为什么那些网上下载jar包什么的还需要CSDN积分(很烦).......)

      先分享一下在后来我们需要用到的东西(网上找的)    其中作者只用到了里边的jar包 如果对下边博客内容有什么问题的可以发消息或者私聊我,我会回复的。

      链接: https://pan.baidu.com/s/1QmLyMyFAVpNI0pwqBtQbrg 提取码: ww3k

开始今天的学习之路吧

--------------------------------------------------------------------------

首先,我们要知道两个xml文件的配置

第一个是: 类名+hbm+.xml 这个xml文件应该放在与你要实现的那个类同一目录下

    比如(Customer与Customer.hbm.xml是同一目录下)

(1)Customer.hbm.xml文件下的内容是用来与数据库中的信息进行配对的(我在下面贴上Customer的代码)

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC 
 3     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4     "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 5 <hibernate-mapping>
 6     <!-- 建立类与表的映射 -->
 7     <class name="Nuc.Test.Customer" table="cst_customer">
 8         <!-- id标签建立类中的属性与表中的主键对应 -->
 9         <id name="cust_id" column="cust_id">
10             <generator class="native"/> <!-- 主键生成策略 -->
11         </id>
12         
13         <!-- 建立类中的普通属性和表的字段的对应 -->
14         <property name="cust_name" column="cust_name"></property>
15         <property name="cust_source" column="cust_source"></property>
16         <property name="cust_industry" column="cust_industry"></property>
17         <property name="cust_level" column="cust_level"></property>
18         <property name="cust_phone" column="cust_phone"></property>
19         <property name="cust_mobile" column="cust_mobile"></property>
20         
21     </class>
22 </hibernate-mapping>
Customer.hbm.xml

 在上边Customer.hbm.xml的文件配置中我们需要用到约束(我在下边以图片的形式展出<有点简陋>)

------->

----->----->

在这里边你就找到你的约束了

(2)下边我们来介绍hibernate.cfg.xml文件,在这个文件我们也用到了约束,只不过我们这次用的是下边这个文件下的

cfg与configuration对应(与上边给出的一样)

 这个文件是对数据库中的url,username,password进行配置的

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE hibernate-configuration PUBLIC
 3     "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 4     "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
 5 <hibernate-configuration>
 6     <session-factory>
 7         <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
 8         <property name="hibernate.connection.url">jdbc:mysql:///hibernate_day01</property>
 9         <property name="hibernate.connection.username">root</property>
10         <property name="hibernate.connection.password">123456</property>
11         <!-- 第二部分: 配置hibernate信息  可选的-->
12         <!-- 输出底层sql语句 -->
13         <property name="hibernate.show_sql">true</property>
14         <!-- 输出底层sql语句格式 -->
15         <property name="hibernate.format_sql">true</property>
16         <!-- hibernate帮创建表,需要配置之后 
17             update: 如果已经有表,更新,如果没有,创建
18         -->
19         <property name="hibernate.hbm2ddl.auto">update</property>
20         <!-- 配置数据库方言
21             在mysql里面实现分页 关键字 limit,只能使用mysql里面
22             在oracle数据库,实现分页rownum
23             让hibernate框架识别不同数据库的自己特有的语句
24          -->
25         <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
26         
27         <mapping resource="Nuc/Test/Customer.hbm.xml"/>
28         
29     </session-factory>
30 </hibernate-configuration>
hibernate.cfg.xml

在写完这两个xml文件之后,我们就可以开始写我们的代码了

(3)我们可以开始写我们的HibernateDemo1类了  下边附上代码(代码里边有解释)

 1 package Nuc.Test;
 2 
 3 import org.hibernate.Session;
 4 import org.hibernate.SessionFactory;
 5 import org.hibernate.Transaction;
 6 import org.hibernate.cfg.Configuration;
 7 import org.junit.Test;
 8 
 9 /*
10  * Hibernate的入门案例
11  * @autor zsz
12  * */
13 public class HibernateDemo1 {
14     @Test
15     //保存客户的案例
16     public void demo1(){
17         //1.加载Hibernate的核心配置文件
18         Configuration configuration = new Configuration().configure();
19         
20         //2.创建SessionFactory对象,类似JDBC中的连接池
21         SessionFactory sessionFactory = configuration.buildSessionFactory();
22         
23         //3.通过SessionFactory获取Session对象,获取JDBC中的Connection
24         Session session = sessionFactory.openSession();
25         
26         //4.手动开启事务
27         Transaction transaction = session.beginTransaction();
28         
29         //5.编写代码
30         
31         Customer customer = new Customer();
32         customer.setCust_name("张三");
33         
34         session.save(customer);
35         
36         //6.事务提交
37         transaction.commit();
38         
39         //7.资源释放
40         session.close();
41     }
42 }
HibernateDemo1

  写到这,已经晚上21.37了 今天开始学习的时间并不长,大概晚上8.30开始学习的(其中遇到好几个BugQ=Q),不管怎么说,自己收获也很多

(4)说一下自己的Bug问题

     4.1、我在写代码过程中比较着急,没有把类中的属性与数据中的匹配好

     4.2、还有在创建数据库的时候主键如果是Int类型,一定要设置自动增加,一定要设置自动增加,一定要设置自动增加。

     4.3、写的时候一定要仔细,仔细,仔细

(5)贴一下自己的项目图片,这样有助于大家理解(是不是在这里边还能发现我的学校(嘻嘻) 说不定是一个学校的哦)

//2018.11.24   博主会持续更新的。

猜你喜欢

转载自www.cnblogs.com/Yinchen-One/p/10013658.html