您的位置:首页 > 其它

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

2016-10-09 11:37 549 查看
好久没更新分享了!现在发下三大框架的hibernate便于初学者学习!另外struts2的那些配置文件代码可以找我要,里面包括如何自定义拦截器等等。开始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 框架