三大框架之hibernate入门学习教程增删改查

好久没更新分享了!现在发下三大框架的hibernate便于初学者学习!另外struts2的那些配置文件代码可以找我要,里面包括如何自定义拦截器等等。开始hibernate的学习吧!首先不多说先导包!

三大框架之hibernate入门学习教程增删改查


新建hibernate.cfg.xml文件

<hibernate-configuration>
	<session-factory>
		<property name="connection.url">
			jdbc:mysql://localhost/student?useUnicode=true&characterEncoding=UTF-8
		</property>
		<property name="connection.driver_class">
			com.mysql.jdbc.Driver
		</property>
		<property name="connection.username">
			root
		</property>
		<property name="connection.password">
			root
		</property>
		<property name="dialect">
			org.hibernate.dialect.MySQLDialect
		</property>
	</session-factory>
</hibernate-configuration>

主要是连接数据库

新建学生实体Student。

新建orm映射文件Student.hbm.xml

<hibernate-mapping>
	<class name="com.qm.entity.Student" table="students">
		<id name="id" column="studentId" type="java.lang.Integer">
			<generator class="identity"></generator>
		</id>
		<property name="name" column="studentName" type="string"></property>
		<property name="password" column="studentPassword" type="string"></property>
		<property name="hight" column="studentHight" type="int"></property>
		<property name="sex" column="studentSex" type="int"></property>
	</class>
</hibernate-mapping>

这个Student类和数据库表一一对应,我将id作为自增属性的

再在hibernate.cfg.xml加入来挂载orm映射

<mapping resource="com/qm/entity/Student.hbm.xml"/>

现在就可以来写持久层来进行增删改查了!

Configuration conf=new Configuration();
		conf.configure("hibernate.cfg.xml");
		//创建工厂
		SessionFactory sf=conf.buildSessionFactory();
		//取得session
		Session session=sf.openSession();
		//开始事务
		session.beginTransaction();
		Student student=new Student("quanmina","123",172,1);
		session.save(student);
		System.out.println("保存成功");
		session.getTransaction().commit();
		session.close();
		sf.close();

这是增加一个学生的代码!其他的见文件包!

总体结构截图

三大框架之hibernate入门学习教程增删改查

增加运行截图

三大框架之hibernate入门学习教程增删改查

修改截图三大框架之hibernate入门学习教程增删改查

查询一个截图!

三大框架之hibernate入门学习教程增删改查

查询所有截图!

三大框架之hibernate入门学习教程增删改查

这是我的数据表设计

三大框架之hibernate入门学习教程增删改查



  • 11
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值