• 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();
            
        }
    
    }
    复制代码
  • 相关阅读:
    K3s+Jetson Nano,在边缘端实现实时视频分析!
    15分钟连接Jetson Nano与K8S,轻松搭建机器学习集群
    配置高可用K3s集群完全攻略
    K3s+Sysdig,8分钟部署并保护集群安全!
    1款工具助力Rancher HA快速部署,极速提升研发测试效率
    连刷40道题,告别动态规划,谈谈我的经验
    直通BAT算法精讲视频教程分享
    关于三次握手和四次挥手,面试官想听到怎样的回答?
    Redisson 分布式锁实战与 watch dog 机制解读
    Spring 注解动态数据源设计实践
  • 原文地址:https://www.cnblogs.com/wangjiabin1990/p/6513755.html
Copyright © 2020-2023  润新知