Hibernate

映射文件<classname>.hbm.xml

<hibernate-mapping>
<class name="Employee" table="EMPLOYEE">
<id name="id" type="int" column="id">
<generator class="native"/>
</id>
<property name="firstName" column="first_name" type="string"/>
<property name="lastName" column="last_name" type="string"/>
<property name="salary" column="salary" type="int"/>
</class>
</hibernate-mapping>

映射类型

映射类型Java 类型ANSI SQL 类型
integer int 或 java.lang.Integer INTEGER
long long 或 java.lang.Long BIGINT
short short 或 java.lang.Short SMALLINT
float float 或 java.lang.Float FLOAT
double double 或 java.lang.Double DOUBLE
big_decimal java.math.BigDecimal NUMERIC
character java.lang.String CHAR(1)
string java.lang.String VARCHAR
byte byte 或 java.lang.Byte TINYINT
boolean boolean 或 java.lang.Boolean BIT
yes/no boolean 或 java.lang.Boolean CHAR(1) ('Y' or 'N')
true/false boolean 或 java.lang.Boolean CHAR(1) ('T' or 'F')

 

映射类型Java 类型ANSI SQL 类型
date java.util.Date 或 java.sql.Date DATE
time java.util.Date 或 java.sql.Time TIME
timestamp java.util.Date 或 java.sql.Timestamp TIMESTAMP
calendar java.util.Calendar TIMESTAMP
calendar_date java.util.Calendar DATE

 

映射类型Java 类型ANSI SQL 类型
binary byte[] VARBINARY (or BLOB)
text java.lang.String CLOB
serializable any Java class that implements java.io.Serializable VARBINARY (or BLOB)
clob java.sql.Clob CLOB
blob java.sql.Blob BLOB

 

映射类型Java 类型ANSI SQL 类型
class java.lang.Class VARCHAR
locale java.util.Locale VARCHAR
timezone java.util.TimeZone VARCHAR
currency java.util.Currency VARCHAR

 

新增

SessionFactory factory = new Configuration().configure().buildSessionFactory();
Session session = factory.openSession();
Transaction tx = null;
Integer employeeID = null;
try{
tx = session.beginTransaction();
Employee employee = new Employee(fname, lname, salary);
employeeID = (Integer) session.save(employee);
tx.commit();
}catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
}finally {
session.close();
}

查询

session.createQuery("FROM Employee").list();

更新

Employee employee = (Employee)session.get(Employee.class, EmployeeID);
employee.setSalary( salary );
session.update(employee);

 

删除

Employee employee = (Employee)session.get(Employee.class, EmployeeID);
session.delete(employee);

注解

@Entity
@Table(name = "EMPLOYEE")
public class Employee {
@Id @GeneratedValue
@Column(name = "id")
private int id;

@Column(name = "first_name")
private String firstName;

@Column(name = "last_name")
private String lastName;

@Column(name = "salary")
private int salary;

HQL

HQL是一种面向对象的查询语言,类似于 SQL,但不是去对表和列进行操作,而是面向对象和它们的属性。
String hql = "SELECT E.firstName FROM Employee E WHERE E.id > 10 ORDER BY E.salary DESC";
Query query = session.createQuery(hql);
List results = query.list();

HQL 查询功能支持命名参数。既能接受来自用户的简单输入,又无需防御 SQL 注入攻击。
String hql = "FROM Employee E WHERE E.id = :employee_id";
Query query = session.createQuery(hql);
query.setParameter("employee_id",10);
List results = query.list();

更新/删除

String hql = "UPDATE Employee set salary = :salary WHERE id = :employee_id";
Query query = session.createQuery(hql);
query.setParameter("salary", 1000);
query.setParameter("employee_id", 10);
int result = query.executeUpdate();
String hql = "DELETE FROM Employee WHERE id = :employee_id";
Query query = session.createQuery(hql);
query.setParameter("employee_id", 10);
int result = query.executeUpdate();

分页

String hql = "FROM Employee";
Query query = session.createQuery(hql);
query.setFirstResult(1);
query.setMaxResults(10);
List results = query.list();

原生 SQL

createSQLQuery
String sql = "SELECT * FROM EMPLOYEE";
SQLQuery query = session.createSQLQuery(sql);
query.addEntity(Employee.class);
List results = query.list();

 

posted @ 2018-03-20 21:01  AaronCnblogs  阅读(104)  评论(0编辑  收藏  举报