hibernate增刪差改
User.hbm.xml
HibernateUtil.java
user.java
UserDao.java
UserDaohiberbateImpl.java
DaoTest.java
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
package="cn.xwy.domain">
<class name="User" >
<id name="id">
<generator class="native"/>
</id>
<property name="name" unique="true"/>
<property name="birthday"/>
</class>
</hibernate-mapping>
<!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="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql:///test</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<mapping resource="cn/xwy/domain/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
HibernateUtil.java
package cn.xwy.dao;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
public final class HibernateUtil {
private static SessionFactory sessionFactory ;
private HibernateUtil(){
}
static{
Configuration cfg = new Configuration();
cfg.configure();
sessionFactory = cfg.buildSessionFactory();
}
public static SessionFactory getSessionFactory(){
return sessionFactory;
}
public static Session getSession(){
return sessionFactory.openSession();
}
}
user.java
package cn.xwy.domain;
import java.util.Date;
public class User {
private int id;
private String name;
private Date birthday;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
UserDao.java
package cn.xwy.dao;
import cn.xwy.domain.User;
public interface UserDao {
public void saveUser(User user);
public User findUserByName(String name);
public User findUserById(int id);
public void updateUser(User user);
public void remove(User user);
}
UserDaohiberbateImpl.java
package cn.xwy.dao.impl;
import org.apache.tools.ant.taskdefs.Tstamp;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Restrictions;
import cn.xwy.dao.HibernateUtil;
import cn.xwy.dao.UserDao;
import cn.xwy.domain.User;
public class UserDaohiberbateImpl implements UserDao{
@Override
public void saveUser(User user) {
Session s = null;
Transaction tx = null;
try {
s = HibernateUtil.getSession();
tx = s.beginTransaction();
s.save(user);
tx.commit();
} finally{
if(s!=null)
s.close();
}
}
@Override
public User findUserByName(String name) {
Session s = null;
try {
s = HibernateUtil.getSession();
Criteria c = s.createCriteria(User.class);
c.add(Restrictions.eq("name",name));
User user = (User)c.uniqueResult();
return user;
} finally{
if(s!=null)
s.close();
}
}
public User findUserByName1(String name) {
Session s = null;
try {
s = HibernateUtil.getSession();
String hql = "from User as user where user.name=:n";
Query q = s.createQuery(hql);
q.setString("n",name);
User user = (User)q.uniqueResult();
return user;
} finally{
if(s!=null)
s.close();
}
}
@Override
public User findUserById(int id) {
Session s = null;
try {
s = HibernateUtil.getSession();
User user = (User) s.get(User.class,id);
return user;
} finally{
if(s!=null)
s.close();
}
}
@Override
public void updateUser(User user) {
Session s = null;
Transaction tx = null;
try {
s = HibernateUtil.getSession();
tx = s.beginTransaction();
s.update(user);
tx.commit();
} finally{
if(s!=null)
s.close();
}
}
@Override
public void remove(User user) {
Session s = null;
Transaction tx = null;
try {
s = HibernateUtil.getSession();
tx = s.beginTransaction();
s.delete(user);
tx.commit();
} finally{
if(s!=null)
s.close();
}
}
}
DaoTest.java
package cn.xwy;
import java.util.Date;
import cn.xwy.dao.UserDao;
import cn.xwy.dao.impl.UserDaohiberbateImpl;
import cn.xwy.domain.User;
public class DaoTest {
public static void main(String[] args){
UserDao dao = new UserDaohiberbateImpl();
User user = new User();
user.setName("name");
user.setBirthday(new Date());
System.out.println("1111");
dao.saveUser(user);
user.setName("new name");
System.out.println("2222");
dao.updateUser(user);
User u = dao.findUserByName(user.getName());
System.out.println("3333");
dao.remove(u);
}
}
相關文章
- Hibernate--增刪改查
- Hibernate進行增刪查改
- Hibernate對單條記錄的增刪改查
- Hibernate--與Spring整合及增刪改查Spring
- 增刪改查
- indexedDB 增刪改查Index
- SQL增刪改查SQL
- 文件的增刪改
- mysql增刪改查MySql
- Mongoose查增改刪Go
- FMDB增刪改查
- mysql增查刪改MySql
- hibernate 自定義表名與列名 - 增刪改查分頁 - 相容Mysql和OracleMySqlOracle
- layui的增刪改查UI
- sql指令,增,刪,查,改SQL
- EFCore之增刪改查
- 資料的增刪改
- 列表的增刪改查
- 字典的增刪改查
- redist的增刪改查Redis
- Mybatis的增刪改查MyBatis
- mysql基本增刪改查MySql
- MongoDB增刪改查操作MongoDB
- MongoDB的增刪改查MongoDB
- ThinkPHP的增、刪、改、查PHP
- mongodb 基本增刪改查MongoDB
- Elasticsearch增刪改查 之 —— Delete刪除Elasticsearchdelete
- elasticsearch7.2增刪改查Elasticsearch
- 【ztree】zTree節點增刪改
- pandas(10):資料增刪改
- SQL 基礎增、刪、改、查SQL
- iOS CoreData (一) 增刪改查iOS
- CoreData - 簡單 增刪改查
- JS基礎_dom增刪改JS
- [express+mongoose](增刪改查)ExpressGo
- rust sqlx 使用---增刪改查RustSQL
- PHP操作MongoDB(增刪改查)PHPMongoDB
- JavaWeb中jdbc增刪查改JavaWebJDBC