hibernate入門例項
阿新 • • 發佈:2018-11-19
###Hibernate5簡單應用例項
1.下載Hibernate所需jar包
2.新建java專案,並匯入jar包
3.新建User.java檔案,User類的屬性於資料庫表的欄位一一對應
-- ----------------------------
-- Table structure for `p_user`
-- ----------------------------
DROP TABLE IF EXISTS `p_user`;
CREATE TABLE `p_user` (
`user_id` int(11) NOT NULL AUTO_INCREMENT ,
`user_name` varchar(20) DEFAULT NULL,
`user_pwd` varchar(20) DEFAULT NULL,
`user_email` varchar(30) DEFAULT NULL,
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of p_user
-- ----------------------------
INSERT INTO `p_user` VALUES ('1', 'yxc', '123456', '[email protected]');
INSERT INTO `p_user` VALUES ('2', '張飛', '123456', '[email protected]');
INSERT INTO `p_user` VALUES ('5', '李四', '123456', null);
INSERT INTO `p_user` VALUES ('6', 'LeBronJames', '123456', '[email protected]');
package com.model;
public class User {
private int userId; //使用者編號
private String userName; //使用者名稱
private String userPwd; //密碼
private String userEmail;//郵箱
public int getUserId() {
return userId;
}
public String getUserName() {
return userName;
}
public String getUserPwd() {
return userPwd;
}
public String getUserEmail() {
return userEmail;
}
public void setUserId(int userId) {
this.userId = userId;
}
public void setUserName(String userName) {
this.userName = userName;
}
public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}
public void setUserEmail(String userEmail) {
this.userEmail = userEmail;
}
}
4.新建User.hbm.xml檔案,建立User類和p_user表的對映關係
<?xml version="1.0"?>
<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
-->
<!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.model.User" table="p_user" lazy="false">
<id name="userId" column="user_id">
<generator class="increment"/>
</id>
<property name="userName" type="java.lang.String">
<column name="user_name" length="30"/>
</property>
<property name="userPwd" type="java.lang.String">
<column name="user_pwd"/>
</property>
<property name="userEmail" type="java.lang.String">
<column name="user_email"/>
</property>
</class>
</hibernate-mapping>
5.在src目錄下新建hibernate.cfg.xml檔案
<!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.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">Root</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/pblog</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="com/model/User.hbm.xml"/>
<mapping class="com.model.User"/>
</session-factory>
</hibernate-configuration>
6.建立HibernateUtil工具類,用來獲取hibernate的session物件
package com.utils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
public class HibernateUtil {
private static SessionFactory factory = null;
static {
Configuration configuration = new Configuration().configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
.applySettings(configuration.getProperties())
.buildServiceRegistry();
factory = configuration.buildSessionFactory(serviceRegistry);
}
public static Session getSession() {
return factory.openSession();
}
}
7.建立UserDao.java檔案,對User進行增刪改查操作
package com.dao;
import com.model.User;
import com.utils.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class UserDao {
public User getUserByID(int id)
{
Session session = HibernateUtil.getSession();
User user = (User)session.get(User.class, id);
session.close();
return user;
}
public void addUser(User user) {
Session session = HibernateUtil.getSession();
Transaction ts = session.beginTransaction();
session.save(user);
ts.commit();
session.close();
}
public void updateUser(User user) {
Session session = HibernateUtil.getSession();
Transaction ts = session.beginTransaction();
session.saveOrUpdate(user);
ts.commit();
session.close();
}
public void deleteUser(User user) {
Session session = HibernateUtil.getSession();
Transaction ts = session.beginTransaction();
session.delete(user);
ts.commit();
session.close();
}
}
8.測試執行
package com.test;
import com.dao.UserDao;
import com.model.User;
public class Test {
public static void main(String[] args) {
User user=new User();
user.setUserName("LeBronJames");
user.setUserPwd("123456");
user.setUserEmail("[email protected]");
UserDao userDao=new UserDao();
userDao.addUser(user);
}
}
檢視控制檯列印資訊
Hibernate: select max(user_id) from p_user
Hibernate: insert into p_user (user_name, user_pwd, user_email, user_id) values (?, ?, ?, ?)
實現了往資料庫中插入一條記錄的操作
獲取原始碼地址https://github.com/yangxuechen/ssh-learning-example/tree/master/HibernateTest