前言
使用IDEA+Maven进行搭建Hibernate5
步骤
导入
在pom.mxl中加入maven项目
1 2 3 4 5 6 7 8 9 10 11 12
| <dependencies> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-search-orm</artifactId> <version>5.5.5.Final</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>6.0.5</version> </dependency> <dependencies>
|
创建配置文件
在/src/main/resources/hibernate.cfg.xml
中配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="connection.url">jdbc:mysql://localhost:3306/test?serverTimezone=GMT&useUnicode=true&characterEncoding=utf8</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> <property name="show_sql">true</property> <property name="format_sql">true</property> <property name="hbm2ddl.auto">create</property> <mapping resource="students.hbm.xml"/> <mapping class="com.ahao.javaeeDemo.Students"/> </session-factory> </hibernate-configuration>
|
创建实体
1 2 3 4 5 6
| public class Student{ private int sid; private String sname; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <?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> <class name="com.ahao.javaeeDemo.Students" table="students"> <id name="sid" type="int"> <column name="sid"/> <generator class="increment"/> </id> <property name="sname" type="java.lang.String"> <column name="sname"/> </property> </class> </hibernate-mapping>
|
编写测试代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| public class MainTest { private static ServiceRegistry serviceRegistry; private static SessionFactory sessionFactory; private static Session session; private static Transaction transaction; @BeforeClass public static void initClass(){ serviceRegistry = new StandardServiceRegistryBuilder() .configure("hibernate.cfg.xml").build(); sessionFactory = new MetadataSources(serviceRegistry).buildMetadata().buildSessionFactory(); session = sessionFactory.openSession(); transaction = session.beginTransaction(); }
@Test public void testSave(){ Students s = new Students(); s.setSname("zhangsan"); s.setBirthday(new java.sql.Date(System.currentTimeMillis())); session.save(s); }
@AfterClass public static void destoryClass(){ transaction.commit(); session.close(); sessionFactory.close(); } }
|
操作
增
使用save()
1 2 3 4 5 6 7 8 9 10 11
| public class MainTest { @Test public void testSave(){ Students s = new Students(); s.setSname("zhangsan"); s.setBirthday(new java.sql.Date(System.currentTimeMillis())); session.save(s); } }
|
改
使用update()
1 2 3 4 5 6 7 8 9 10 11 12
| public class MainTest { @Test public void testUpdate(){ int sid = 1; Students s = session.get(Students.class, sid); s.setSname("Cat"); session.update(s);
} }
|
删
使用delete()
1 2 3 4 5 6 7 8 9 10
| public class MainTest { @Test public void testDelete(){ int sid = 1; Students s = session.get(Students.class, sid); session.delete(s); } }
|
查
查询单个记录
使用get()
或load()
1 2 3 4 5 6 7 8 9 10 11
| public class MainTest { @Test public void testRead(){ int sid = 1; Students s1 = session.get(Students.class, sid); System.out.println(s1.toString()); Students s2 = session.load(Students.class, sid); System.out.println(s2.toString()); } }
|