该项目使用到的jar包
1.我使用的是mysql关系型数据库,先建立一个数据库,并且创建一个customer(客户信息)表
2.创建一个Customer实体类(持久化类)
通常持久化类的编写应嘎遵循一些规则:
1).持久化类中必须提供无参数public构造器(如果没有提供任何构造方法,虚拟机会自动提供默认构造方法,但是如果提供了其他有参数的构造方法的话
虚拟机不再提供默认构造方法,必须手动编写无参构造方法).
2).持久化类中所有属性使用private修饰,提供public的setter/getter方法
3).必须提供标识属性OID,与数据库表中逐渐对应,例如下面Customer类id属性
4).持久化类属性应尽量使用基本数据类型的包装类型,例如int换成Integer,long换成Long,目的是为了与数据库表的字段默认值null一致。(例如int是区分不开null跟0的,不赋值的int类型默认值为0)
5).持久化类不要用final修饰,使用final修饰将无法生成代理对象进行优化.
- public class Customer {
- private Integer id;
- private String name;
- private Integer age;
- private String sex;
- private String city;
- /**
- * @return the id
- */
- public Integer getId() {
- return id;
- }
- /**
- * @param id the id to set
- */
- public void setId(Integer id) {
- this.id = id;
- }
- /**
- * @return the name
- */
- public String getName() {
- return name;
- }
- /**
- * @param name the name to set
- */
- public void setName(String name) {
- this.name = name;
- }
- /**
- * @return the age
- */
- public Integer getAge() {
- return age;
- }
- /**
- * @param age the age to set
- */
- public void setAge(Integer age) {
- this.age = age;
- }
- /**
- * @return the sex
- */
- public String getSex() {
- return sex;
- }
- /**
- * @param sex the sex to set
- */
- public void setSex(String sex) {
- this.sex = sex;
- }
- /**
- * @return the city
- */
- public String getCity() {
- return city;
- }
- /**
- * @param city the city to set
- */
- public void setCity(String city) {
- this.city = city;
- }
- //重写toString()方法
- @Override
- public String toString() {
- return "Customer [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + ", city=" + city + "]";
- }
- }
3.编写映射文件Customer.hbm.xml
实体类Customer目前还不具备持久化操作的能力,而Hibernate需要知道实体类Customer映射到数据库 Hibernate中的哪个表,以及类中的哪个属性
对应数据库表中的哪一个字段,这些都需要在映射文件中配置.
在实体类Customer所在的包中,创建一个名称为Customer.hbm.xml的映射文件,在该文件中定义了实体类Customer的属性是如何映射到customer表的列上的
开始编写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> <!-- name代表的是实体类名,table代表的是表名 --> <class name="com.hck.entity.Customer" table="customer"> <!-- name=id代表的是customer类中属性 column=id代表的是table表中的字段 --> <id name="id" column="id"> <!-- 主键生成策略 --> <generator class="native"/> </id> <!-- 其他属性使用property标签来映射 --> <property name="name" column="name" type="string"/> <property name="age" column="age" type="integer"/> <property name="sex" column="sex" type="string"/> <property name="city" column="city" type="string"/> </class> </hibernate-mapping>
4.编写核心配置文件hibernate.cfg.xml
Hibernate的映射文件反映了持久化类和数据库表的映射信息,
而Hibernate的配置文件则主要用来配置数据库连接以及Hibernate运行时所需要的各个属性的值.
在项目的src目录下创建一个名称为hibernate.cfg.xml的文件
xml的头部编写可以按下图所以查找复制
编写hibernate.cfg.xml文件
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE hibernate-configuration PUBLIC
- "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
- "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
- <hibernate-configuration>
- <session-factory>
- <!--指定方言 -->
- <property name="hibernate.dialect">
- org.hibernate.dialect.MySQLDialect
- </property>
- <!-- 数据库驱动 -->
- <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
- <!-- 连接数据库的url -->
- <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/tb_test</property>
- <!-- 数据库用户名 -->
- <property name="hibernate.connection.username">root</property>
- <!-- 数据库密码 -->
- <property name="hibernate.connection.password">123456</property>
- <!-- 其他配置 -->
- <!-- 格式化sql -->
- <property name="format_sql">true</property>
- <!-- 用来关联hbm配置文件 -->
- <mapping resource="com/hck/entity/Customer.hbm.xml"/>
- </session-factory>
- </hibernate-configuration>
5.编写单元测试类代码如下:
- public class HibernateTestDemo {
- //定义变量
- Configuration config;
- SessionFactory sessionFactory;
- Session session;
- Transaction transaction;
- //before表示在方法执行前执行
- @Before
- public void setUp()
- {
- //1.加载hibernate.cfg.xml配置
- config=new Configuration().configure();
- //2.获取SessionFactory
- sessionFactory=config.buildSessionFactory();
- //3.获得一个session
- session=sessionFactory.openSession();
- //4.开始事务
- transaction=session.beginTransaction();
- }
- //添加操作
- @Test
- public void insert()
- {
- //5.操作
- Customer customer=new Customer();
- customer.setId(1);
- customer.setName("zhangsan");
- customer.setAge(20);
- customer.setSex("man");
- customer.setCity("广州");
- session.save(customer);
- }
- //删除操作
- @Test
- public void delete()
- {
- //先查询
- Customer customer=(Customer)session.get(Customer.class, 1);
- //再删除
- session.delete(customer);
- }
- //查询操作
- @Test
- public void select()
- {
- Customer customer=(Customer)session.get(Customer.class, 1);
- System.out.println(customer);
- }
- //更新操作
- @Test
- public void update()
- {
- Customer customer=new Customer();
- customer.setId(1);
- customer.setName("zhangsan");
- customer.setAge(20);
- customer.setSex("man");
- //修改地址为北京
- customer.setCity("北京");
- //存在就更新,不存在就执行插入操作
- session.saveOrUpdate(customer);
- }
- //After表示在方法执行结束后执行
- @After
- public void closeTransaction()
- {
- //6.提交事务
- transaction.commit();
- //7.关闭资源
- session.close();
- sessionFactory.close();
- }
- }
1)执行插入操作后,打开mysql使用select * from customer;得到的结果:
2)执行查询操作(查看控制台输出)
3.执行更新操作
4.删除操作