SSH——Hibernate简单配置


         作为SSH开发框架中的ORM部分,感觉这个ORM跟以前用过的一些ORMapping框架相比,配置起来还是相对麻烦的。


     一,引入Jar包


               


           刚开始只是引入了hibernat非常基本的jar包进去,后来debug的时候,发现缺失了很多jar包,就又引了一批进去。所以特别想问问大家引包的时候,有什么方法可以提高引入jar包的效率吗?



    二,创建实体类及实体类_数据库对应xml文件


         例如,表结构如下:


            


      然后我建立如下实体类:


       

package net.blogjava.nokiaguy.models;

public class MapEntity {
	private int id;
	private String keyword;
	private String value;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getKeyword() {
		return keyword;
	}
	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}
	public String getValue() {
		return value;
	}
	public void setValue(String value) {
		this.value = value;
	}
	

}


         并对这个实体类建立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="net.blogjava.nokiaguy.models.MapEntity" table="t_map">
    		<!-- 将id属性应射成自增类型 -->
    		<id name="id" column="id" type="int">
    			<generator class="increment"/>
    		</id>
    		<property name="keyword" column="keyword"/>
    		<property name="value" column="value"/>
    	</class>
    </hibernate-mapping>



          之后是核心配置文件:hibernate.cfg.xml:


         

<?xml version="1.0" encoding="UTF-8"?>
<!--表明解析本XML文件的DTD文档位置,DTD是Document Type Definition 的缩写,即文档类型的定义,XML解析器使用DTD文档来检查XML文件的合法性。hibernate.sourceforge.net/hibernate-configuration-3.0dtd可以在Hibernate3.1.3软件包中的src\org\hibernate目录中找到此文件--> 
<!DOCTYPE hibernate-configuration PUBLIC 
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
          
<!--表明以下的配置是针对session-factory配置的,SessionFactory是Hibernate中的一个类,这个类主要负责保存HIbernate的配置信息,以及对Session的操作--> 
<hibernate-configuration>
<!--配置数据库的驱动程序,Hibernate在连接数据库时,需要用到数据库的驱动程序--> 
	<session-factory>
		 <!--设置数据库的连接url:jdbc:mysql://localhost/**,其中localhost表示mysql服务器名称,此处为本机,    **是数据库名--> 
		<property name="connection.url">
			jdbc:mysql://localhost/testhibernate
		</property>
		
		<!--hibernate.dialect 只是Hibernate使用的数据库方言,就是要用Hibernate连接那种类型的数据库服务器。--> 
		<property name="dialect">
			org.hibernate.dialect.MySQLDialect
		</property>
		
		<property name="connection.username">root</property>
		<property name="connection.password"></property>
		<!-- 显示hibernate生产的SQL语句 -->
		<property name="show_sql">true</property>
		<property name="connection.driver_class">
			com.mysql.jdbc.Driver
		</property>
		<!-- 指定Map.hbm.xml的位置 -->
		<mapping resource="net/blogjava/nokiaguy/models/Map.hbm.xml"/>
	</session-factory>
</hibernate-configuration>


          这个里面主要是配置连接的一些需要的参数及实体类_表的配置文件的位置。



三,action中通过hibernate调用操作数据库


                   

public String execute()
		throws Exception
	{
		Configuration configuration=new Configuration();
		SessionFactory sessionFactory;
		configuration.configure("/hibernate.cfg.xml");
		sessionFactory=configuration.buildSessionFactory();
		//开始一个会话
		Session session=sessionFactory.openSession();
		Transaction transaction=session.beginTransaction();
		//开始事物
		transaction.begin();
		MapEntity mapEntity1=new MapEntity();
		mapEntity1.setKeyword("ihu");
		mapEntity1.setValue("bc...");
		MapEntity mapEntity2=new MapEntity();
		mapEntity2.setKeyword("iou");
		mapEntity2.setValue("bc...");
		//持久化两个对象
		session.save(mapEntity1);
		session.save(mapEntity2);
		//提交事务
		transaction.commit();
		session.close();
		result="保存成功";
		return "success";
	}



       对比下.net平台下的Nhibernate(http://blog.csdn.net/lhc1105/article/details/48709295),会发现98%的东西都是一致的。






        

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值