• 初识Hibernate框架,进行简单的增删改查操作


    Hibernate的优势

    优秀的Java 持久化层解决方案  (DAO

    主流的对象—关系映射工具产品

     简化了JDBC 繁琐的编码

     将数据库的连接信息都存放在配置文件

     自己的ORM框架

     一定要手动实现Hibernate(模拟Hibernate实现)

    2:创建一个大配置文件

    <?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>
    
            <!-- Database connection settings 数据库连接设置-->
            <!-- 驱动类 -->
            <property name="connection.driver_class">oracle.jdbc.OracleDriver</property><!-- 数据库驱动语言 -->
            <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property><!-- 数据库连接字符串 -->
            <property name="connection.username">cgg</property><!-- 用户名 -->
            <property name="connection.password">1</property><!-- 密码 -->
            <!-- SQL dialect (sql的方言)-->
            <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property><!-- 通过oracle数据库的版本规定sql方言 -->
            <!-- Echo all executed SQL to stdout 在控制台打印后台的sql语句-->
            <property name="show_sql">true</property><!-- 可省,展示生成的sql语句 -->
            <!-- 格式化显示sql -->
            <property name="format_sql">true</property><!-- 可省,将展示出的sql语句格式化-->
            <!-- Drop and re-create the database schema on startup 序列化-->
            <property name="hbm2ddl.auto">update</property><!-- 数据库的建表语句首选create,之后选择update -->
         <!--线程绑定-->
         <property name="hibernate.current_session_context_class">thread</property>
        <mapping resource="cn/happy/entity/Student.hbm.xml" /><!-- 关联小配置文件 --> 
      </session-factory>

    </hibernate-configuration>

    创建一个小配置文件

    <?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 package="cn.happy.entity"><!-- 关联哪个包下的类 -->
        <class name="Student" table="STUDENT"><!-- 符合一类对应一表 -->
            <id name="sid" column="SID"><!-- 生成主键(唯一标识) -->
                <!-- 主键生成策略:native: native:如果后台是Oracle 后台是MySQL,自动应用自增 -->
                <generator class="increment" />
            </id>
            <property name="sname" type="string" column="NAME" />
            <property name="sage"  type="int" column="AGE"/>
        </class>
    </hibernate-mapping>

    测试数据

    public class myTest {
        Configuration cfg;
        Session session;
        Transaction tran;
        @Before
        public void myBefore(){
            //读取并解析配置文件
            cfg=new Configuration().configure("hibernate.cfg.xml");
            //读取并解析映射信息,创建SessionFactory对象
            SessionFactory factory = cfg.buildSessionFactory();
            //打开session
            //使用的getCurrentSession()方法,会自动关闭session
            session = factory.getCurrentSession();
            //开始一个事务(增删改操作必须,查询操作可选)
            tran=session.beginTransaction();
        }
        @After
        public void myAfter(){
            //结束事务
            tran.commit();;
        }
        //添加
        @Test
        public void addTest(){
            Student stu=new Student();
            stu.setSage(18);
            stu.setSname("安哲安哲");
            session.save(stu);
        }
        @Test
        public void findTest(){
            Student stu = session.load(Student.class, 11);
            System.out.print(stu.getSname());
        }
        @Test
        public void upTest(){
            Student stu = session.load(Student.class, 11);
            stu.setSname("安哲大傻逼");
        }
        @Test
        public void delTest(){
            Student stu = session.load(Student.class, 12);
            session.delete(stu);
        }

    通过以上代码,我们就能轻松的使用框架向db端操作数据了

  • 相关阅读:
    celery worker的工作模式
    动态加载js
    PHP加密解密
    js加载div, 元素事件不生效问题
    任意页面加载聊天框组件(也可用于其他)
    重装系统
    vue 跨域问题
    859. Buddy Strings
    316. Remove Duplicate Letters
    654. Maximum Binary Tree
  • 原文地址:https://www.cnblogs.com/chuangege/p/6269247.html
Copyright © 2020-2023  润新知