个人网站制作图片,弹幕网站制作,班级网站页面设计,如何做转运网站一、Hibernate导入相关的包参考#xff1a;http://blog.csdn.net/tunni/article/details/54982160这些包包括相应数据库驱动、hibernate.zip下lib目录下的jar包#xff0c;其中必须包是required目录下的.jar二、在项目classpath#xff08;类路径#xff0c;即src目录下http://blog.csdn.net/tunni/article/details/54982160这些包包括相应数据库驱动、hibernate.zip下lib目录下的jar包其中必须包是required目录下的.jar二、在项目classpath类路径即src目录下配置hibernate.cfg.xml并且配置数据库连接hibernate.cfg.xml配置文件
!DOCTYPE hibernate-configuration PUBLIC -//Hibernate/Hibernate Configuration DTD 3.0//EN http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd hibernate-configuration session-factory !-- mysql数据库驱动 -- property namehibernate.connection.driver_classcom.mysql.jdbc.Driver/property !-- mysql数据库名称 -- property namehibernate.connection.urljdbc:mysql://localhost:3306/hibernate_db/property !-- 数据库的登陆用户名 -- property namehibernate.connection.usernameroot/property !-- 数据库的登陆密码 -- property namehibernate.connection.passwordadmin/property !-- 方言为每一种数据库提供适配器方便转换 -- property namehibernate.dialectorg.hibernate.dialect.MySQLDialect/property !-- 建议配置方便在日志中查看sql语句-- propertynamepropertynamehibernate.show_sqltrue/property propertynamehibernate.format_sqltrue/property !--配置类与表的映射文件 -- mapping resourcecom/hibernate/User.hbm.xml//session-factory
/hibernate-configurationcom.hibernate.User类
package com.hibernate;public class User { private String id; private String username; private String password; public String getId() { return id; } public void setId(String id) { this.id id; } public String getUsername() { return username; } public void setUsername(String userName) { this.username userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password password; }
} User.hbm.xml配置文件
?xml version1.0?
!DOCTYPE hibernate-mapping PUBLIC -//Hibernate/Hibernate Mapping DTD 3.0//EN http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd hibernate-mapping !-- 类与数据库的表对应 -- class namecom.hibernate.User tableuser !-- 主键名 -- id nameid columnid !-- 生成策略 -- generator classuuid/ /id !-- 其他类属性与表字段 -- property nameusername columnusername/ property namepassword/ /class
/hibernate-mapping hibernate访问工具类package hibernate;
/*** hibernate工具* author maokun* */
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;public class HibernateUtil {private static final SessionFactory sessionFactory;static{try{ //配置文件放在classpath路径即src目录下//如果hibernate的配置文件目录为hibernate.cfg.xml则//Configuration config new Configuration().configure();//或Configuration config new Configuration().configure(hibernate.cfg.xml);//或Configuration config new Configuration().configure(hibernate.cfg.xml); //配置其他路径如下://Configuration config new Configuration().configure(hibernate/hibernate.cfg.xml);Configuration config new Configuration().configure(/hibernate/hibernate.cfg.xml);sessionFactory config.buildSessionFactory();}catch(Throwable e){throw new ExceptionInInitializerError(e);}}public static final ThreadLocal session new ThreadLocal();public static Session currentSession() throws HibernateException{Session s (Session)session.get();//如果线程没有session打开新的sessionif(s null || !s.isOpen()){s sessionFactory.openSession();session.set(s);}return s;}public static void closeSession() throws HibernateException{Session s (Session)session.get();session.set(null);if(s ! null)s.close();}}三、第一个hibernate例子先创建hibernate_db数据库接着创建user表包含idusernamepassword。Test类package hibernate;import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;import com.hibernate.User;public class Test {public static void main(String[] args) {User user new User();user.setUsername(name);user.setPassword(pass);Session session HibernateUtil.currentSession();//生成Session实例Transaction tx session.beginTransaction();try{session.save(user); //保存持久类对象tx.commit(); //提交到数据库session.close(); }catch(HibernateException e){e.printStackTrace();tx.rollback();}}
}