分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 教程案例

hibernate之helloword(环境搭建)

发布时间:2023-09-06 01:26责任编辑:蔡小小关键词:word

 环境搭建

    hibernate.cfg.xml

  <?xml version="1.0" encoding="UTF-8"?>    <!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.jdbc.Driver</property><property name="hibernate.connection.url">jdbc:mysql:///web_shop</property><property name="hibernate.connection.username">root</property><property name="hibernate.connection.password">123</property><!-- 设置连接提供者 --><property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property><!-- c3p0连接池的配置 --><property name="hibernate.c3p0.max_size">20</property> <!-- 最大连接池 --><property name="hibernate.c3p0.min_size">5</property> <!-- 最小连接数 --><property name="hibernate.c3p0.timeout">120</property> <!-- 超时 --><property name="hibernate.c3p0.idle_test_period">3000</property> <!-- 空闲连接 --><!-- 可以将向数据库发送的sql显示出来 --><property name="hibernate.show_sql">true</property><!-- 格式化sql --><property name="hibernate.format_sql">true</property><!-- hibernate的方言 --><property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property><!-- 自动创建表 --><!-- <property name="hibernate.hbm2ddl.auto">update</property> --><!-- 用于设置事务提交方式 --><property name="hibernate.connection.autocommit">false</property><!-- 配置hibernate的映射文件所在位置 --><mapping resource="com/baidu/domain/User.hbm.xml"/></session-factory></hibernate-configuration>

   

 javabean

public class User {private String uid;private String username;private String password;private String name;private String email;private String telephone;private Date birthday;private String sex;private Integer state;private String code;public User() {super();}public User(String username, String password) {super();this.username = username;this.password = password;}public User(String uid, String username, String password, String name,String email, String telephone, Date birthday, String sex,Integer state, String code) {super();this.uid = uid;this.username = username;this.password = password;this.name = name;this.email = email;this.telephone = telephone;this.birthday = birthday;this.sex = sex;this.state = state;this.code = code;}public String getUid() {return uid;}public void setUid(String uid) {this.uid = uid;}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;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public String getTelephone() {return telephone;}public void setTelephone(String telephone) {this.telephone = telephone;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public Integer getState() {return state;}public void setState(Integer state) {this.state = state;}public String getCode() {return code;}public void setCode(String code) {this.code = code;}@Overridepublic String toString() {return "User [uid=" + uid + ", username=" + username + ", password="+ password + ", name=" + name + ", email=" + email+ ", telephone=" + telephone + ", birthday=" + birthday+ ", sex=" + sex + ", state=" + state + ", code=" + code + "]";}}

    xxx.hbm.xml

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping PUBLIC ????"-//Hibernate/Hibernate Mapping DTD 3.0//EN" ???"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> ???<hibernate-mapping> ???<!--name表示类的全路径名 ?table表名 catalog表示数据库名 --> ???<class name="com.baidu.domain.User" table="user" catalog="web_shop"> ???<!-- 表示主键 --> ???<id name="uid" column="uid"> ???<!-- <generator class="native"></generator> --> ???</id> ???<property name="username" column="username" length="20"></property> ???<property name="password" column="password" length="20"></property> ???<property name="name" column="name" length="20"></property> ???<property name="email" column="email" length="20"></property> ???<property name="telephone" column="telephone" length="20"></property> ???<property name="birthday" column="birthday" ?></property> ???<property name="sex" column="sex" length="10"></property> ???<property name="state" column="state" length="11"></property> ???<property name="code" column="code" length="64"></property> ???</class> ???</hibernate-mapping>

  导包:

                     

                      数据库连接池包

      

      数据库连接

      

      hibernate连接池依赖c3p0包

      

                    

    创建工具类

public class HibernateUtils {private static SessionFactory sessionFactory=null;static{//获取config ?加载配置文件Configuration configure = new Configuration().configure();sessionFactory = configure.buildSessionFactory();}//获取sessionpublic static Session getSession(){Session session = sessionFactory.openSession();return session;}}

  测试类:

          @Test //删除方法public void test1(){// 创建一个User对象//保证每个用户去出来的session都不相同User user = new User();user.setUid("1234");Session session = HibernateUtils.getSession(); // 相当于得到一个Connection。// 开启事务Transaction transaction = session.beginTransaction();// 操作//删除时必须根据主键进行删除session.delete(user);// 事务提交transaction.commit();//不需要关闭Configuration ?因为Configuration不是轻量级的。这样一个项目就只产生一个Configuration//configuration相当于连接池session.close();}

hibernate之helloword(环境搭建)

原文地址:http://www.cnblogs.com/fjkgrbk/p/hibernate_rumen.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved