<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.10.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.2.10.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-osgi</artifactId>
<version>5.2.10.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>5.2.10.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-proxool</artifactId>
<version>5.2.10.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-infinispan</artifactId>
<version>5.2.10.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>5.2.10.Final</version>
</dependency>
</dependencies>
複製代碼
建立核心配置文件和Mapper的xml文件裏java
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><
複製代碼
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
複製代碼
<!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.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/money2?userSSL=true&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT&allowMultiQueries=true</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!--配置SQL方言-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL57Dialect</property>
<!--自動建立表(通常不須要)-->
<property name="hbm2ddl.auto">update</property>
<!--顯示SQL-->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!--配置數據實體-->
<!--註冊mapper-->
<mapping resource="mapping/AccUser.hbm.xml"/>
</session-factory>
</hibernate-configuration>
複製代碼
<!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.libi.entity.AccUser" table="acc_user">
<!--對象標識OID-->
<id name="id" column="id">
<generator class="identity"/>
</id>
<!--屬性映射-->
<property name="userName" column="user_name"/>
<property name="password" column="password"/>
<property name="createTime" column="create_time"/>
</class>
</hibernate-mapping>
複製代碼
<session-factory>
標籤裏註冊mapping<!--註冊mapper-->
<mapping resource="mapping/AccUser.hbm.xml"/>
複製代碼
添加主類進行測試mysql
public class Application {
public static void main(String[] args) {
Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.getTransaction();
transaction.begin();
AccUser user = new AccUser();
user.setUserName("hibernate");
user.setPassword("1233333");
user.setCreateTime(System.currentTimeMillis());
session.save(user);
transaction.commit();
session.close();
sessionFactory.close();
}
}
複製代碼
<class name="com.libi.entity.AccUser" table="acc_user">
<!--對象標識OID-->
<id name="id" column="id">
<generator class="具體策略"/>
</id>
</class>
複製代碼
public List<AccUser> selectAllUser() {
Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
Query<AccUser> query = session.createQuery("select au from AccUser au", AccUser.class);
query.setFirstResult(0);
query.setMaxResults(10);
return query.getResultList();
}
複製代碼
條件查詢sql
public AccUser selectUserById(Long uid) {
Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
Query<AccUser> query = session.createQuery("select au from AccUser au where id = ?", AccUser.class);
//傳入佔位的ID實現查詢
query.setParameter(0, uid);
//獲取單個結果
return query.uniqueResult();
}
複製代碼
...
Query<AccUser> query = session.createQuery("select au from AccUser au where id = :id", AccUser.class);
...
query.setParameter("id", uid);
...
複製代碼
模糊查詢數據庫
"%查詢內容%"
HQL更新和刪除(相似SQL更新和刪除)數組
HQL查詢部分字段緩存
Query userName = session.createQuery("select userName from AccUser where id = 1",String.class);
複製代碼
Query userName = session.createQuery("select userName, password from AccUser where id = 1");
複製代碼
保存到本地線程bash
<property name="hibernate.current_session_context_class">thread</property>
複製代碼
Session currentSession = sessionFactory.getCurrentSession();
複製代碼
<many-to-noe>
默標籤裏添加lazy屬性,認是延遲lazy = true
)