hibernate第一课
来源:互联网 发布:qq for mac os x 10.8 编辑:程序博客网 时间:2024/06/05 08:44
1、在hibernate官网下载相应文件,打开MyEclipse,创建项目,并添加hibernate相应jar包和mysq数据库驱动包。
2、Student.java
package com.wxh.hibernate.model;public class Student {public Student(){}private int id;private String name;private int age;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}3.Student.hbm.xml
<hibernate-mapping package="com.wxh.hibernate.model"><class name="Student" ><id name="id"></id><properties name="age"></properties><properties name="name"></properties></class></hibernate-mapping>
4.hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?><!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost/db</property> <property name="connection.username">root</property> <property name="connection.password">111</property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <mapping resource="com/wxh/hibernate/model/Student.hbm.xml"/> </session-factory></hibernate-configuration>
import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import com.wxh.hibernate.model.Student;public class StudentTest {public static void main(String[] args){Student s=new Student();s.setId(2);s.setName("s1");s.setAge(1);Configuration cfg=new Configuration();SessionFactory sf=cfg.configure().buildSessionFactory();Session session=sf.openSession();session.beginTransaction();session.save(s);session.getTransaction().commit();session.close();sf.close();}}
6.在mysql中新建数据库db和相应的表结构。
7.思考:为什么数据库中只能插入id的值,age和name的值没有?
8.解决办法:修改配置文件Student.hbm.xml,把其中的properties改为property,问题解决。
0 0
- Hibernate 第一课
- hibernate第一课
- hibernate入门第一课 认识Hibernate
- HIbernate第一课--基本原理,配置
- Hibernate笔记之1第一课
- hibernate第一课 环境的搭建
- Hibernate第一课-小应用实践
- [Hibernate学习第一课]入门案例
- Hibernate-----第一个hibernate项目
- Hibernate 第一讲 Hibernate 入门
- Hibernate:第一个Hibernate程序
- Hibernate 第一个Hibernate应用程序
- Hibernate------>第一个hibernate案例
- Hibernate 第一个hibernate程序
- 第一个Hibernate程序
- 第一个hibernate项目
- hibernate第一个例子
- Hibernate第一个应用
- UVA 11040(p342)----Add Bricks in the Wall
- 7.1 itertools--高效循环的创建函数
- 使用HashMap做一个缓存案例【缓存一张图片】
- poj Babelfish 2503 (字符串模拟)
- UVA 11054(p237)----Wine trading in Gergovia
- hibernate第一课
- Linux为什么受欢迎?
- Ambari,hadoop的配置,管理和监控项目入门
- 如何查看自己电脑的IP和网络运营商
- C语言高效编程
- 通过hashmap实现统计字符串数组中某个字符串出现的次数的方法
- 要求:通过创建一个货物类,添加货物基本信息,使用TreeMap进行比较输出 方法一为只比较价格;方法二为先比较价格再比较销量然后再输出
- UVA 11093(p246)----Just Finish it up
- 线程池的学习与深入研究