• Hibernate4学习一


    1.导入所有相关jar包

    https://pan.baidu.com/s/1i5OakiL

    2.配置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="connection.driver_class">com.mysql.jdbc.Driver</property>
            <!-- qq_db为你的数据库名称 -->

              <property name="hibernate.connection.url">
              <![CDATA[jdbc:mysql://localhost:3306/qq_db?useUnicode=true&characterEncoding=utf8]]>
              </property>

            
            <property name="connection.username">root</property>
            <property name="connection.password">root</property>
    
            <!-- JDBC connection pool (use the built-in) -->
            <property name="connection.pool_size">1</property>
    
            <!-- SQL方言 -->
            <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    
            <!-- Enable Hibernate's automatic session context management -->
            <property name="current_session_context_class">thread</property>
    
            <!-- Disable the second-level cache -->
            <property name="cache.provider_class">
                org.hibernate.cache.NoCacheProvider
            </property>
    
            <!-- 显示SQL语句 -->
            <property name="show_sql">true</property>
            <!-- 格式化输出SQL语句 -->
            <property name="format_sql">true</property>
    
            <!-- validate 加载hibernate时,验证创建数据库表结构 -->
            <!-- create 每次加载hibernate,重新创建数据库表结构,这就是导致数据库表数据丢失的原因。 -->
            <!-- create-drop 加载hibernate时创建,退出是删除表结构 -->
            <!-- update 加载hibernate自动更新数据库结构 -->
            <property name="hbm2ddl.auto">create</property>
            <property name="myeclipse.connection.profile"></property>
    
    
            <!-- 使用annotion注解时,hibernate与数据库对象关系映射表示 -->
            <!-- 推荐使用annotion注解来表示映射关系 -->
            <mapping class="QQUser" />
        </session-factory>
    
    </hibernate-configuration>
    复制代码

    3.新建映射类

    复制代码
    import java.io.Serializable;
    import java.util.Date;
    
    import javax.persistence.Entity;
    import javax.persistence.Id;
    import javax.persistence.Table;
    
    @Entity
    @Table(name="tb_user")
    public class QQUser implements Serializable {
    
        private String qq_num;
        private String nick_name;
        private String gender;
        private Date birthday;
        private int star_sit;
        
        @Id
        public String getQq_num() {
            return qq_num;
        }
        public void setQq_num(String qq_num) {
            this.qq_num = qq_num;
        }
        public String getNick_name() {
            return nick_name;
        }
        public void setNick_name(String nick_name) {
            this.nick_name = nick_name;
        }
        public String getGender() {
            return gender;
        }
        public void setGender(String gender) {
            this.gender = gender;
        }
        public Date getBirthday() {
            return birthday;
        }
        public void setBirthday(Date birthday) {
            this.birthday = birthday;
        }
        public int getStar_sit() {
            return star_sit;
        }
        public void setStar_sit(int star_sit) {
            this.star_sit = star_sit;
        }
        
        
        
    }
    复制代码

    4.测试

    复制代码
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;
    
    public class Test {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
                 QQUser u = new QQUser();
                 u.setQq_num("7890654");
                 u.setNick_name("小明");
                 u.setGender("男");
                 
    
                Configuration cfg = new Configuration();
                // 读取hibernate.cfg.xml中的配置
                cfg.configure();
                // 获取SessionFactory
                SessionFactory sf = cfg.buildSessionFactory();
                // 获取Session
                Session session = sf.openSession();
                // 开启事务
                session.beginTransaction();
                // 保存
                session.save(u);
                // 提交事务
                session.getTransaction().commit();
                // 关闭连接
                session.close();
                sf.close();
            
        }
    
    }
    复制代码
  • 相关阅读:
    ACM学习历程—HDU5410 CRB and His Birthday(动态规划)
    ACM学习历程—HDU1028 Ignatius and the Princess III(递推 || 母函数)
    ACM学习历程—HDU5396 Expression(递推 && 计数)
    ACM学习历程—HDU 5317 RGCDQ (数论)
    ACM学习历程—HDU 5326 Work(树形递推)
    ACM学习历程—SNNUOJ 1110 传输网络((并查集 && 离线) || (线段树 && 时间戳))(2015陕西省大学生程序设计竞赛D题)
    jquery开发js插件
    requireJS
    java进阶之-Maven,svn,git,maven合拼多个项目
    java 项目中每个jar包的作用总结
  • 原文地址:https://www.cnblogs.com/wangjiabin1990/p/6513755.html
Copyright © 2020-2023  润新知