上一篇使用hibernate的createQuery進行增刪查改,這一篇寫接口實現增刪查改。
hibernate的增刪查改
第一步:建表
數據庫名爲:salary
數據表名爲:user
第二步:目錄
這次所需要的類:
第三步:新建項目,取名[自己隨意],導jar包
hibernatejar包鏈接:https://pan.baidu.com/s/1DIGZD53ANre7cP5A7EYmQg
提取碼:w9hp
連接數據庫的jar包鏈接:https://pan.baidu.com/s/1X2u71nitKZ4UjlwBJX53PA
提取碼:oyda
在微信號:新白者,回覆關鍵字hibernate和數據庫也可領取。(有興趣可以關注一下,一起學習)
第四步:新建實體類user與其映射類user.hbm.xml
user.java:
package com.lwc.entity;
public class user {
private String tno;
private String teachername;
private String age;
private String sex;
private String title;
public user() {
super();
this.tno = tno;
this.teachername = teachername;
this.age = age;
this.sex = sex;
this.title = title;
}
public user(String tno, String teachername, String age, String sex, String title) {
super();
this.tno = tno;
this.teachername = teachername;
this.age = age;
this.sex = sex;
this.title = title;
}
public String getTno() {
return tno;
}
public void setTno(String tno) {
this.tno = tno;
}
public String getTeachername() {
return teachername;
}
public void setTeachername(String teachername) {
this.teachername = teachername;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public String toString() {
return "user [tno=" + tno + ", teachername=" + teachername + ", age=" + age + ", sex=" + sex + ", title="
+ title + "]";
}
}
user.hbm.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!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.lwc.entity.user" table="user">
<id name="tno" >
<column name="tno"></column>
</id>
<property name="teachername" >
<column name="teachername"></column>
</property>
<property name="age">
<column name="age"></column>
</property>
<property name="sex" >
<column name="sex"></column>
</property>
<property name="title">
<column name="title"></column>
</property>
</class>
</hibernate-mapping>
第五步:配置核心映射文件
hibernate.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/salary?characterEncoding=UTF-8&useSSL=false</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.current_session_context_class">thread</property>
<mapping resource="com/lwc/entity/user.hbm.xml"/>
</session-factory>
</hibernate-configuration>
第六步:寫入接口
package com.lwc.dao;
import com.lwc.entity.user;
/**
* 寫入接口Dao
* @author xinbai
*
*/
public interface userDao {
//增加
public void add(user us);
//查詢
public void find(user us);
//刪除
public void delete(String tno);
//修改
public void update(user us);
}
第七步:實現接口
package com.lwc.userDaoImpl;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import com.lwc.dao.userDao;
import com.lwc.entity.user;
import com.lwc.util.HibernateUtil;
public class userDaoImpl implements userDao{
public void add(user us) {
// TODO 自動生成的方法存根
SessionFactory sf = null;
Session session = null;
Transaction ts = null;
System.out.println("1");
try {
sf = HibernateUtil.getSessionFactory();
session = sf.getCurrentSession();
ts = session.beginTransaction();
session.save(us);
System.out.println(us.toString());
ts.commit();
} catch (HibernateException e) {
// TODO Auto-generated catch block
if(ts != null)
{
ts.rollback();
}
e.printStackTrace();
}
}
@Override
public void delete(String tno) {
// TODO 自動生成的方法存根
SessionFactory sf = null;
Session session = null;
Transaction ts = null;
try {
sf = HibernateUtil.getSessionFactory();
session = sf.getCurrentSession();
ts = session.beginTransaction();
user p = (user) session.get(user.class, tno);
session.delete(p);
System.out.println(p.getTno());
ts.commit();
} catch (HibernateException e) {
// TODO Auto-generated catch block
if(ts != null)
{
ts.rollback();
}
e.printStackTrace();
}
}
@Override
public void update(user us) {
// TODO 自動生成的方法存根
SessionFactory sf = null;
Session session = null;
Transaction ts = null;
try {
sf = HibernateUtil.getSessionFactory();
session = sf.getCurrentSession();
ts = session.beginTransaction();
session.update(us);
ts.commit();
} catch (HibernateException e) {
// TODO Auto-generated catch block
if(ts != null)
{
ts.rollback();
}
e.printStackTrace();
}
}
@Override
public void find(user us) {
// TODO 自動生成的方法存根
SessionFactory sf = null;
Session session = null;
Transaction ts = null;
//user us1 = new user();
try {
sf = HibernateUtil.getSessionFactory();
session = sf.getCurrentSession();
ts = session.beginTransaction();
Criteria criteria =session.createCriteria(user.class);
List<user> list = criteria.list();
//使用forEach遍歷集合
for (user user : list) {
System.out.println(user);
}
ts.commit();
} catch (HibernateException e) {
// TODO Auto-generated catch block
if(ts != null)
{
ts.rollback();
}
e.printStackTrace();
}
}
}
第九步:新建HibernateUtil的類
package com.lwc.util;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
return new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void main(String[] args) {
}
}
第十步:編寫測試類
package com.lwc.test;
import org.junit.jupiter.api.Test;
import com.lwc.dao.userDao;
import com.lwc.entity.user;
import com.lwc.userDaoImpl.userDaoImpl;
public class userTest {
userDaoImpl uImpl = new userDaoImpl();
@Test
public void testadd(){
user us = new user("9","小白","15","男","教師");
uImpl.add(us);
}
@Test
public void testfind() {
user user=new user();
uImpl.find(user);
}
@Test
public void testdelete() {
uImpl.delete("5");
}
@Test
public void testupdate() {
user us=new user("2", "老王","50", "男","學生");
uImpl.update(us);
}
}
熱愛開源,喜歡探索未知,知道的越多,才發現不知道的越多,點贊關注,一起學習。