您的位置:首页 > 其它

hibernate入门配置

2015-10-18 00:00 302 查看

第一步:建立Hibernate工程

第二步:建立数据库,可将编码格式设置成UTF-8

第三步:引入hibernate相关的jar包

包结构的详解:





第四步:持久化对象Customer.java

在cn.itcast.a_primer中创建持久化对象的javabean,hibernate的实质就是操作对象的过程,就是操作数据库表。

创建表和对应的javabean文件:


1

2

3

4

5

6

7

8
public
class
Customer
implements
Serializable{



&nbs 3ff8 p;
private
Integerid;


private
Stringname;


private
Integerage;


private
Stringdes;


//省略set和get方法

}

第五步:hibernate.cfg.xml
在cn.itcast.a_primer中创建hibernate.cfg.xml,hibernate.cfg.xml是hibernate的核心配置文件,用来查询数据库,配置如何操作数据库,加载映射文件等。

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
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>

<!DOCTYPEhibernate-configurationPUBLIC

"-//Hibernate/HibernateConfigurationDTD3.0//EN"

"http://hibernate.sourceforge.net/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://localhost:3306/itcasthibernate?useUnicode=true&characterEncoding=utf8</
property
>

<
property
name
=
"hibernate.connection.username"
>root</
property
>

<
property
name
=
"hibernate.connection.password"
>root</
property
>

<!--其他配置-->

<!--方言:用来指定hibernate操作的数据库-->

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

<!--hibernate操作对象用来执行操作数据库表,在控制台中显示sql语句-->

<
property
name
=
"hibernate.show_sql"
>true</
property
>

<!--


hibernate.hbm2ddl.auto:用来建立对象和表的关联


*create:每次都会先删除表,再创建表,问题不能保存数据


*none:默认值:有表的时候,可以直接操作,没有表就会报错,默认向表中追加数据


*update:没有表就创建表,有表就直接操作数据


-->

<
property
name
=
"hibernate.hbm2ddl.auto"
>update</
property
>

<!--加载映射文件

1:class="cn.itcast.a_primer.Customer"(要求类和映射文件放在同一个目录下,而且文件名要相同)

2:resource=cn/itcast/a_primer/Customer.hbm.xml(第二种方式)



<mappingresource="cn/itcast/a_primer/Customer.hbm.xml"/>-->

</
session-factory
>

</
hibernate-configuration
>

创建Hibernate配置文件,Hibernate从其配置文件中读取和数据库连接的有关信息,这个文件应该位于应用的classpath下.

注:该映射文件的规范在org.hibernate.hibernate-configuration-3.0.dtd文件中







第六步:customer.hbm.xml

在javabean的同级目录下创建hibernate的映射文件(映射文件用来将持久化对象和属性关联数据库表和字段

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

32

33

34

35

36

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

<!DOCTYPEhibernate-mappingPUBLIC


"-//Hibernate/HibernateMappingDTD3.0//EN"


"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<!--

建立持久化对象和数据库表的关联关系(映射文件)


*类映射表


*类中的属性映射表中的字段


-->

<
hibernate-mapping
>

<!--

对象关联表

name:对象的全路径(底层使用反射)

table:映射表的名称


-->

<
class
name
=
"cn.itcast.a_primer.Customer"
table
=
"a_customer"
>

<!--

对象的属性映射表的字段

type:表示hibernate的映射类型,用来建立java类型和数据库类型的桥梁

*使用java类型,比如java.lang.Integer

*integer


name:对象中的属性名称


column:表中的字段名称

*generator:hibernate的生成策略(重点)

sql-type="varchar(20)",指定数据库表中列的类型,默认是varchar(255)


-->

<
id
name
=
"id"
type
=
"integer"
>

<
column
name
=
"id"
></
column
>

<
generator
class
=
"increment"
></
generator
>

</
id
>

<
property
name
=
"name"
type
=
"string"
>

<
column
name
=
"name"
></
column
>

</
property
>

<
property
name
=
"age"
column
=
"age"
type
=
"integer"
></
property
>

<
property
name
=
"des"
column
=
"des"
type
=
"string"
></
property
>

</
class
>

</
hibernate-mapping
>

这里

创建对象-关系映射文件

lHibernate采用XML格式的文件来指定对象和关系数据之间的映射.在运行时Hibernate将根据这个映射文件来生成各种SQL语句

l映射文件的扩展名为.hbm.xml这里Customer.hbm.xml文件

注:该映射文件的规范在org.hibernate.hibernate-mapping-3.0.dtd
3ff0
文件中



第七步:测试单表操作的CRUD

使用测试代码进行测试,创建App的测试类,使用junit进行测试

(1)hibernate的SessionFactory在初始化阶段只初始化一次即可,所有使用静
态代码块进行封装,其中Configuration是用来加载hibernate的配置文件和映射文件的,加载后可以获取SessionFactory。

1

2

3

4

5

6

7
private
static
SessionFactorysf=
null
;

static
{


Configurationconfiguration=
new
Configuration();


configuration.configure(
"cn/itcast/a_primer/hibernate.cfg.xml"
);


configuration.addClass(Customer.
class
);


sf=configuration.buildSessionFactory();

}

(2)新增保存:save方法

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15
/**保存*/

@Test

public
void
save(){


Sessions=sf.openSession();


Transactiontr=s.beginTransaction();


Customerc=
new
Customer();


c.setName(
"洪七公"
);


c.setAge(
60
);


c.setDes(
"帮助"
);


s.save(c);


tr.commit();
//实质上执行了2步操作,1:s.flush();//清理缓存,让session缓存中的数据与数据库同步,2:事务提交


s.close();
//Session的缓存就没有了

}

(3)更新:update方法

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15
/**更新*/

@Test

public
void
update(){


Sessions=sf.openSession();


Transactiontr=s.beginTransaction();


Customerc=
new
Customer();


c.setId(
3
);


c.setName(
"黄老邪"
);


c.setAge(
59
);


c.setDes(
"药师"
);


s.update(c);


tr.commit();


s.close();

}

(4)删除:delete方法

1

2

3

4

5

6

7

8

9

10

11
@Test

public
void
delete(){


Sessions=sf.openSession();


Transactiontr=s.beginTransaction();


Customerc=
new
Customer();


c.setId(
3
);


s.delete(c);


tr.commit();


s.close();

}

(5)查询:get和load方法

1

2

3

4

5

6

7

8

9

10
/**使用ID查询信息*/

@Test

public
void
findCustomerById(){


Sessions=sf.openSession();


Transactiontr=s.beginTransaction();


Customerc=(Customer)s.get(Customer.
class
,
2
);


System.out.println(c.getId()+
""
+c.getName()+
""
+c.getAge()+
""
+c.getDes());


tr.commit();


s.close();

}

(6)查询:query查询(支持hql语句,sql语句,qbc语句)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17
/**查询所有信息*/

@Test

public
void
findAllCustomerList(){


Sessions=sf.openSession();


Transactiontr=s.beginTransaction();

/**使用Hql语句:操作持久对象和属性


*复习sql语句:操作数据库表和数据库表的列*/


Queryquery=s.createQuery(
"fromCustomero"
);


List<Customer>list=query.list();


if
(list!=
null
&&list.size()>
0
){


for
(Customerc:list){


System.out.println(c.getId()+
""
+c.getName()+
""
+c.getAge()+
""
+c.getDes());


}

}


tr.commit();


s.close();

}

附录:



来自为知笔记(Wiz)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: