您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息

Hibernate简介与实例介绍

2024/4/19 3:44:10发布6次查看
一、hibernate简介在很多场景下,我们不需要使用jdbctemplate直接操作sql语句,这时候可以用orm工具来节省数大量的的代码和开发时间。orm工具能够把注意力从容易出错的sql代码转向如何实现应用程序的真正需求。
spring对orm框架的支持提供了与这些框架的集成点以及一些附加的服务:
支持集成spring声明式事务;
透明的异常处理;
线程安全的、轻量级的模板类;
dao支持类;
资源管理。
hibernate是在开发者社区很流行的开源orm框架。
二、spring+hibernate实例1.创建数据库表mysql新建数据库store,然后执行如下sql:
1 create table category (2 id int not null,3 name varchar(80) null,4 constraint pk_category primary key (id)5 );6 7 insert into category(id,name) values (1,'女装');8 insert into category(id,name) values (2,'美妆');9 insert into category(id,name) values (3,'书籍');
db_store.sql
2.代码结构我用的ide是ideaiu,通过maven构建项目,通过xml配置spring。完成后的代码结构为:
3.创建实体类categoryclass category{ private int cateid; private string catename; //次数省略get,set方法 @override public string tostring() { return "id="+cateid+" name="+catename; } }
4.修改pom.xml,引入相关依赖。<dependencies> <dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version>4.12</version> </dependency> <dependency> <groupid>org.hibernate</groupid> <artifactid>hibernate-core</artifactid> <version>4.3.5.final</version> </dependency> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <version>5.1.30</version> </dependency> </dependencies>
5.配置applicationcontext.xml<?xml version="1.0" encoding="utf-8"?> <beans xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/tx "> <bean id="datasource" class="org.springframework.jdbc.datasource.drivermanagerdatasource"> <property name="driverclassname" value="com.mysql.jdbc.driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/store"></property> <property name="username" value="root"></property> <property name="password" value="root"></property> </bean> <bean id="sessionfactory" class="org.springframework.orm.hibernate4.localsessionfactorybean"> <property name="datasource" ref="datasource"/> <property name="configlocation" value="classpath:hibernate/hibernate.cfg.xml"/> </bean> <tx:annotation-driven/> <bean id="transactionmanager" class="org.springframework.orm.hibernate4.hibernatetransactionmanager"> <property name="sessionfactory" ref="sessionfactory"/> </bean> <bean id="categorydao" class="categorydao"> <constructor-arg ref="sessionfactory"></constructor-arg> </bean> </beans>
datasource没什么特别的,就不在解释了。看下其他几点:
①hibernate sessionfactory:
使用hibernate所需的主要接口是org.hibernate.session,session接口提供了crud等最基本的数据访问功能。通过hibernate的session接口,应用程序的repository能够满足所有的持久化需求。而获取hibernate session对象的标准方式是借助于hibernate sessionfactory接口的实现类。
在sessionfactory配置主要设置了两个属性:datasource设置了数据连接,configlocation设置了hibernate配置文件的路径。
②事务
要是数据库操作支持事务,需要配置<tx:annotation-driven/>和transactionmanager。
6.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> <property name="dialect">org.hibernate.dialect.mysqldialect</property> <property name="show_sql">true</property> <mapping resource="hibernate/category.hbm.xml"/> </session-factory> </hibernate-configuration>
②category.hbm.xml
<?xml version="1.0" encoding="utf-8"?> <!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="category" table="category"> <id name="cateid" column="id"> <generator class="native"/> </id> <property name="catename" column="name"/> </class> </hibernate-mapping>
7.数据访问实现类categorydao如果方法要支持事务,需要加注解@transactional。
public class categorydao { private sessionfactory sessionfactory; public categorydao(sessionfactory sessionfactory) { this.sessionfactory = sessionfactory; } private session currentsession() { return sessionfactory.getcurrentsession(); } @transactional public void save(category category) { currentsession().save(category); } @transactional public void update(category category){ currentsession().update(category); } @transactional public void delete(int id) { query query = currentsession().createsqlquery("delete from category where id=::id"); query.setinteger("::id", id); query.executeupdate(); } @transactional public int count() { return getall().size(); } @transactional public category getbyid(int id) { criteria criteria=currentsession().createcriteria(category.class); criteria.add(restrictions.eq("id",id)); return (category) criteria.uniqueresult(); } @transactional public list<category> getall() { return currentsession().createcriteria(category.class).list(); } }
8.测试@contextconfiguration(locations = "classpath:applicationcontext.xml") @runwith(springjunit4classrunner.class) public class testcategorydao { @autowired private categorydao categorydao; @test public void testadd() { category category = new category(); category.setcateid(4); category.setcatename("母婴"); categorydao.save(category); } @test public void testupdate() { category category = new category(); category.setcateid(4); category.setcatename("男装"); categorydao.update(category); } @test public void testgetbyid() { int id = 4; category category = categorydao.getbyid(id); if(category==null){ system.out.println("not exist"); }else { system.out.println(category.tostring()); } } @test public void testgetall() { list<category> categories = categorydao.getall(); for (category item : categories) { system.out.println(item); } } @test public void testcount() { int count = categorydao.count(); system.out.println(count); } @test public void testdelete() { int id = 4; categorydao.delete(id); } }
以上就是hibernate简介与实例介绍的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录