• hibernate之helloword(环境搭建)


     环境搭建

        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;
    	}
    	@Override
    	public 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();
    	}
    	//获取session
    	public 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();
    		}
  • 相关阅读:
    Win10+Ubuntu18.04 UEFI启动模式SSD+HDD
    Chap1:全景图[Computer Science Illuminated]
    [IDE] ECLIPSE取消自动更新
    [Unit Test] Unit Test Brief Introduction
    [ English ] 俚语 “Ping me=打我电话”
    Some Useful Resources for the Future Usage
    python错误记录
    django-用户认证模型
    Djiango-富文本编辑器
    Djiango-建立模型抽象基类
  • 原文地址:https://www.cnblogs.com/fjkgrbk/p/hibernate_rumen.html
Copyright © 2020-2023  润新知