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

来源:互联网 发布:c语言辗转相除法 编辑:程序博客网 时间:2024/06/02 12:11

好久没更新分享了!现在发下三大框架的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();//取得sessionSession 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入门学习教程增删改查



3 1
原创粉丝点击