第一步:hibernate的配置文件 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代表一个数据库 --> <session-factory> <!-- 连接数据库的用户名 --> <property name="connection.username">root</property> <!-- 连接数据库的密码 --> <property name="connection.password">root</property> <!-- 连接数据库的Driver --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <!-- 连接数据库的url --> <property name="connection.url">jdbc:mysql://localhost:3306/hibetnate?useUnicode=true&characterEncoding=UTF-8</property> <!-- 方言 --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- validate加载hibernate时,验证数据库的结构 update 加载hibetnate时,检查数据库,如果 表不存在,则创建 ,如果存在,则更新 create 每次加载hibetnate,都会创建表 create-drop 每次加载hibernate,创建,卸载hibernate时,销毁 --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 打印sql语句 --> <property name="hibernate.show_sql">true</property> <!--加载映射文件--> <mapping resource="com/itheima09/hibetnate/domain/Person.hbm.xml"/> </session-factory> </hibernate-configuration> 第二步:hibernate所对应的实体类 Person.java package com.itheima09.hibetnate.domain; import java.io.Serializable; public class Person implements Serializable { private static final long serialVersionUID = 1L; private Long pid; private String name; private String description; public Long getPid() { return pid; } public void setPid(Long pid) { this.pid = pid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } }
第三步:hibernate的映射文件 <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.itheima09.hibetnate.domain.Person" table="PERSON"> <id name="pid" length="5" type="java.lang.Long" column="pid"> <generator class="increment"></generator> </id> <property name="name" column="name" type="java.lang.String" length="20"></property> <property name="description" column="description" type="java.lang.String" length="50"></property> </class> </hibernate-mapping> 第四步:PersonDAO.java package com.itheima09.hibetnate.dao; import java.util.List; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.classic.Session; import org.junit.Test; import com.itheima09.hibetnate.domain.Person; public class PersonDAO { @Test public void savePerson(){ //创建配置文件对象 Configuration configuration = new Configuration(); //加载配置文件 1、该配置文件必须放在根目录下 2、名称必须为hibetnate.cfg.xml configuration.configure(); //创建一个sessionFactory SessionFactory sessionFactory = configuration.buildSessionFactory(); //打开一个session Session session = sessionFactory.openSession(); //打开一个事务 Transaction transaction = session.beginTransaction(); Person person = new Person(); person.setName("王二麻子"); person.setDescription("给力"); session.save(person); transaction.commit(); } @Test public void updatePerson(){ Configuration configuration = new Configuration(); configuration.configure(); SessionFactory sessionFactory = configuration.buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); Person person = (Person) session.get(Person.class, 1L); person.setDescription("1111"); session.update(person); transaction.commit(); session.close(); } @Test public void queryPerson(){ Configuration configuration = new Configuration(); configuration.configure(); SessionFactory sessionFactory = configuration.buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); Person person = (Person) session.get(Person.class, 1L); List<Person> list = session.createQuery("from Person").list(); transaction.commit(); session.close(); for (Person p : list) { String name = p.getName(); System.out.println(name); } } @Test public void delete(){ Configuration configuration = new Configuration(); configuration.configure(); SessionFactory sessionFactory = configuration.buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); Person person = (Person) session.get(Person.class, 1L); session.delete(person); transaction.commit(); session.close(); } }
时间: 2024-10-04 01:15:20