写在前面:2020年面试必备的Java后端进阶面试题总结了一份复习指南在Github上,内容详细,图文并茂,有需要学习的朋友可以Star一下!
GitHub地址:https://github.com/abel-max/Java-Study-Note/tree/master
导入所需jar包
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.10.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
编写配置文件hibernate.cfg.xml
<?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="current_session_context_class">thread</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://39.99.188.50:3306/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<!--c3p0连接池-->
<property name="hibernate.connection.provider_class">org.hibernate.c3p0.internal.C3P0ConnectionProvider</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">300</property>
<!--控制台显示sql语句-->
<property name="hibernate.show_sql">true</property>
<!--格式化sql语句-->
<property name="hibernate.format_sql">true</property>
<!--hibernate方言-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!--配置hibernate映射文件位置-->
<mapping resource="Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
编写实体类
public class Student implements Serializable {
Integer id; String number;
String name;
//...get set toString
}
创建实体类映射文件 XXX.hbm.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 package="com.text">
<!--
name:即实体类的全名
table:映射到数据库里面的那个表的名称
catalog:数据库的名称
-->
<class name="Student" table="student" catalog="test">
<id name="id" column="id">
<!--主键策略-->
<generator class="native"/>
</id>
<property name="number" column="number" length="12"/>
<property name="name" column="name" length="20"/>
</class>
</hibernate-mapping>
创建hibernate工具类
public final class HibernateUtil {
private static SessionFactory sessionFactory;
private HibernateUtil() {
} static {
Configuration configuration = new Configuration();
configuration.configure(); sessionFactory = configuration.buildSessionFactory(); } public static SessionFactory getSessionFactory(){
return sessionFactory;
} public static Session getSession(){
return sessionFactory.openSession();
}}
创建测试类进行测试
import com.text.HibernateUtil;
import com.text.Student;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.List;
public class HibernateTest {
private Session session;
private Transaction transaction;
@Before
public void before() {
session = HibernateUtil.getSession(); transaction = session.beginTransaction(); } @After
public void after() {
transaction.commit(); session.close(); } @Test
public void testQuery(){
//根据id查询
Student student = session.get(Student.class, 2);
System.out.println(student);
//查询部分
Query query = session.createQuery("from Student where id>1");
List<Student> studentList = query.list();
System.out.println(studentList);
}
@Test
public void testQuery2() {
}
@Test
public void testUpdate(){
String hql = "update Student set number = '22222' where id>2";
Query query = session.createQuery(hql);
int i = query.executeUpdate();
System.out.println(i);
}
@Test
public void testDelete() {
Student student = new Student();
student.setId(3);
session.delete(student);
}
@Test
public void testAdd() {
Student student = new Student();
student.setName("张鑫龙8");
student.setNumber("500118");
session.saveOrUpdate(student);
}
}
详细的查询方法
查询
按照主键查询
- load()和get()@Test public void testQuery2() { Student student1 = session.get(Student.class, 1); Student student2 = session.load(Student.class, 2); System.out.println(student1); System.out.println(student2); }
HQL查询
查询语句必须是实体类的方法名,不能是表名称,必须和sql语句查询区别
@Test
public void testQuery3() {
Query query = session.createQuery("from Student s where s.id=1 or s.id>3");
List<Student> list = query.list();
System.out.println(list);
}
完全面向对象查询,Criteria查询,QBC查询
已过时
本地SQL查询
适合使用复杂的查询,但不能跨越数据库
@Test
public void testQuery4() {
NativeQuery sqlQuery = session.createSQLQuery("select * from student where id>3 or id=1");
sqlQuery.addEntity(Student.class); List<Student> list = sqlQuery.list();
System.out.println(list);
}