hibernate环境配置

hibernate.cfg.xml文件

<!-- 约束在5.0.7.Final.jar内configuration-3.0.dtd  -->
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
    <!-- hibernate 配置 -->
    <hibernate-configuration>
     <!-- 必须配置:连接数据库四大参数与方言 -->
    <session-factory>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql:///hibernate</property>
    <property name="hibernate.connection.username">wshc</property>
    <property name="hibernate.connection.password">123456</property>
    <!-- 方言 -->
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <!-- 可以选配 -->
    <!-- 显示sql -->
    <property name="hibernate.show_sql">true</property>
    <!-- 改变sql格式 -->
    <property name="hibernate.format_sql">true</property>
    <!-- 对数据库的操作模式 -->
    <property name="hibernate.hbm2ddl.auto">update</property>
    <!-- 映射 -->
    <mapping resource="com/ithc/bean/Student.hbm.xml"/>
    </session-factory>
    </hibernate-configuration>

student.hbm.xml表的映射配置

<!-- 映射 -->
<!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:表名 -->
<class name="com.ithc.bean.Student" table="student">
<!-- 配置主键     class标签李只能有一个主键   column:配置数据库的字段名 name:实体类的属性名 -->
    <id column="id" name="id">
        <generator class="native"/>
    </id>
    
    <property name="name" column="name" unique="true"/>
    <property name="email" column="email"/>
</class>
</hibernate-mapping>

action持续层的调用

@WebServlet(value="/studentAction")
public class StudentAction extends HttpServlet{
    private static final long serialVersionUID = 1L;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        
        String name = new String(req.getParameter("name").getBytes("iso-8859-1"),"utf-8");
        String email = new String(req.getParameter("email").getBytes("iso-8859-1"),"utf-8");
        StudentService studentService = new StudentServiceImpl();
        System.out.println(name + email);
        studentService.insert(name,email);
    }

}

hibernate的session封装

public class GetSession {
    private static SessionFactory sessionFactory;
    static{
        Configuration configure = new Configuration().configure();      
        sessionFactory = configure.buildSessionFactory();
    }   
    public static Session getSession(){     
        return sessionFactory.openSession();
    }
}

dao层调用数据层步骤

public class StudentDaoImpl implements StudentDao{

    @Override
    public void insert(String name, String email) {
        Session session = GetSession.getSession();
    
        Transaction transaction = session.beginTransaction();
        
        Student student = new Student();
        student.setName (name);
        student.setEmail(email);
        session.save(student);
        
        transaction.commit();
        session.close();
    }
}
增、删、改、查(操作增、删、改时要先查询一下)
@Test
    public void test1(){
        //获取session
        Session session = GetSession.getSession();
        //
        Transaction transaction = session.beginTransaction();
        //查询
        Student student = session.get(Student.class, 5);
        student.setName("笑笑");
        //修改
        session.update(student);
        System.out.println(student);
        //删除
        session.delete(student);
        System.out.println(student);
        //增加
        Student student2 = new Student();
        student2.setName("小辉");
        student2.setEmail("xiaohui@163.com");
        System.out.println(student2);
        session.save(student2);
        //提交事务
        transaction.commit();
        //释放资源
        session.close();        
    }   
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,805评论 18 399
  • 一. Java基础部分.................................................
    wy_sure阅读 3,854评论 0 11
  • 本文包括: 1、CRM 项目的整体介绍 2、Hibernate 框架概述 3、Hibernate 快速入门 4、H...
    廖少少阅读 3,489评论 9 66
  • (一)Struts、Spring、Hibernate、Mybatis框技术 1.Struts2.0有几种标签库 【...
    独云阅读 3,300评论 0 62
  • 目录 接上节(连载)《石猿记》第十三章(49) 话说吸烟老仗,因至“桥”心更换烟沬,烫痛巨人,致使巨人护痛心急翻“...
    鲁西道童阅读 424评论 0 5