IDEA 整合 Struts2 + hibernate的demo

1、新建项目:

IDEA 整合 Struts2 + hibernate的demo_第1张图片

 

IDEA 整合 Struts2 + hibernate的demo_第2张图片

 

IDEA 整合 Struts2 + hibernate的demo_第3张图片

 

IDEA 整合 Struts2 + hibernate的demo_第4张图片

 

2、添加Jar包:

在pom.xml中:

 

   

   

   

   

      asm

      asm

      3.3

   

 

   

   

      asm

      asm-commons

      3.3

   

 

   

   

      asm

      asm-tree

      3.3

   

 

   

   

      commons-fileupload

      commons-fileupload

      1.2.2

   

 

   

   

      commons-io

      commons-io

      2.0.1

   

 

   

   

      org.apache.commons

      commons-lang3

      3.1

   

 

   

   

      org.freemarker

      freemarker

      2.3.19

   

 

   

   

      javassist

      javassist

      3.11.0.GA

   

 

   

   

      ognl

      ognl

      3.0.5

   

 

   

   

      org.apache.struts

      struts2-core

      2.3.4

   

 

   

   

      org.apache.struts.xwork

      xwork-core

      2.3.4

   

 

   

   

   

      antlr

      antlr

      2.7.7

   

 

   

   

      dom4j

      dom4j

      1.6.1

   

 

   

   

      org.hibernate.common

      hibernate-commons-annotations

      5.0.2.Final

   

 

   

   

      org.hibernate

      hibernate-core

      5.2.10.Final

   

 

   

   

      org.hibernate.javax.persistence

      hibernate-jpa-2.0-api

      1.0.1.Final

   

 

   

   

      org.javassist

      javassist

      3.15.0-GA

   

 

   

   

      org.jboss.logging

      jboss-logging

      3.3.0.Final

   

 

   

   

      org.jboss.spec.javax.transaction

      jboss-transaction-api_1.1_spec

      1.0.1.Final

   

 

   

   

   

   

      mysql

      mysql-connector-java

      8.0.13

   

 

   

   

      junit

      junit

      4.11

      test

   

   

      org.junit.jupiter

      junit-jupiter-api

      5.2.0

      compile

 




  javax.servlet
  javax.servlet-api
  4.0.1
  provided




  javax.servlet.jsp
  javax.servlet.jsp-api
  2.3.1
  provided

 

 

 

 

   

   

     

       

          org.apache.maven.plugins

          maven-compiler-plugin

          3.1   

         

            1.8

            1.8

            UTF-8

         

       

     

   

 

   

   

     

        src/main/resources

      

     

        src/main/java

       

          **/*.properties

          **/*.xml

       

       

        false

     

   

 

 

3、hibernate自动生成实体类、映射文件:

1、连接mysql

IDEA 整合 Struts2 + hibernate的demo_第5张图片

 

IDEA 整合 Struts2 + hibernate的demo_第6张图片

成功:

IDEA 整合 Struts2 + hibernate的demo_第7张图片

 

2、添加hibernate插件支持:

IDEA 整合 Struts2 + hibernate的demo_第8张图片

 

IDEA 整合 Struts2 + hibernate的demo_第9张图片

 

IDEA 整合 Struts2 + hibernate的demo_第10张图片

 

 

 

 

 

 

成功结果:

IDEA 整合 Struts2 + hibernate的demo_第11张图片

 

3、Hibernate映射文件,需要添加主键生成策略:

①这是主键自增:

IDEA 整合 Struts2 + hibernate的demo_第12张图片

<generator class="identity">generator>  这是自增-->

 

 

②这是uuid 字符串唯一:

<generator class="uuid"/> 

 

 

4、如果再次想生成实体类和映射文件:

IDEA 整合 Struts2 + hibernate的demo_第13张图片

 

5、创建hibernate核心配置文件:

Hibernate.cfg.xml

 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.Driverproperty>

        <property name="hibernate.connection.url">

            jdbc:mysql://127.0.0.1:3307/hibernate?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC

        property>

        <property name="hibernate.connection.username">rootproperty>

        <property name="hibernate.connection.password">passwordproperty>

        

        <property name="hibernate.dialect">org.hibernate.dialect.MySQL55Dialectproperty>

        

        <property name="hbm2ddl.auto">updateproperty>

        

        <property name="hibernate.show_sql">trueproperty>

        <property name="hibernate.format_sql">trueproperty>


<property name="hibernate.current_session_context_class">threadproperty>


        

        <mapping resource="com/lemon/pojo/Students.hbm.xml">mapping>

        <mapping resource="com/lemon/pojo/Users.hbm.xml">mapping>

    session-factory>

  hibernate-configuration>

 

 

 

注册实体:

IDEA 整合 Struts2 + hibernate的demo_第14张图片

配置数据库:

IDEA 整合 Struts2 + hibernate的demo_第15张图片

 

 

 

6、工具类:

HibernateUtils.java

package com.lemon.utils;

  

  

  import org.hibernate.Session;

  import org.hibernate.SessionFactory;

  import org.hibernate.cfg.Configuration;

  

  /**

 * 工具类:

 */

  public class HibernateUtils {

  

    private static SessionFactory sessionFactory;

  

    static {

        //1、加载配置

        Configuration configuration = new Configuration();

        configuration.configure("hibernate.cfg.xml");

        /*单例模式*/

        //得到工厂 工厂来生产操作数据库的对象

        sessionFactory = configuration.buildSessionFactory();

    }

  

    //2、得到session

    public static Session openSession() {

        return sessionFactory.openSession();

    }

  

    

    //获取session方式二:使用这个session必须开启事务

    public static Session getCurrentSession() {

        //受线程绑定,即在多线程下,线程是安全的

        return sessionFactory.getCurrentSession();

    }

}

 

 

 

 

 

7、测试:

package com.lemon;

  

  import com.lemon.pojo.Students;

  import com.lemon.utils.HibernateUtils;

  import org.hibernate.Session;

  import org.hibernate.query.Query;

  import org.junit.jupiter.api.Test;

  

  /**

 * 测试类

 *

 */

  public class App {

  

    //查找

    public void findById(){

        Session session = HibernateUtils.openSession();

        Query  query = session.createQuery("from Students where id = ?", Students.class);

        query.setParameter(0,"S0000001");

        Students students = query.uniqueResult();

        System.out.println(students.getSname() + "\t" + students.getGender());

        session.close();

    }

  

    public static void main(String[] args) {

        App app = new App();

  

        app.findById();

    }

  

}

 

 

 

 

 

 

 

 

测试成功:

IDEA 整合 Struts2 + hibernate的demo_第16张图片

 

4、Hibernate与Struts2整合

4.1、添加web:

IDEA 整合 Struts2 + hibernate的demo_第17张图片

 

IDEA 整合 Struts2 + hibernate的demo_第18张图片

 

4.2、配置web.xml


<filter>
    <filter-name>struts2filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class>
filter>
<filter-mapping>
    <filter-name>struts2filter-name>
    <url-pattern>/*url-pattern> 
filter-mapping>
<welcome-file-list>
    <welcome-file>index.jspwelcome-file>
welcome-file-list>

 

 

4.3、配置struts.xml

xml version="1.0" encoding="UTF-8"?>

struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
   
   
<package name="default" namespace="/" extends="struts-default">
       
    package>
struts>

 

 

 

你可能感兴趣的:(hibernate,Struts2)