Detailed steps for building SSH framework in IntelliJ IDEA

Posted by JVassie on Tue, 01 Oct 2019 05:54:58 +0200

Don't say much, just get to the point!

1. Click File to create a new Module

2. Click and select Spring, then check Spring on the right (when checking Spring, we will prompt you to create spring-config.xml, here we check it for convenience), Web Application, Struts 2

Next drop-down and select Hibernate(Hibernate is integrated with Spring, so you don't need to check the configuration file)

3. Next is next, enter the name of the project, and finally Finish. It may take a few minutes after Finish, and Idea will automatically download the core jar package you need. The following is the initial project structure:

4. Then configure the Database. On the right side of Idea, click Database, then click the green plus sign, select Data Source, and select Database (MySQL is used by bloggers).

5. Make the following configuration in web.xml

 <?xml version="1.0" encoding="UTF-8"?>
 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
          version="3.1">

     <!-- Spring Framework core listener configuration -->
     <context-param>
         <param-name>contextConfigLocation</param-name>
         <param-value>classpath:spring-config.xml</param-value>
     </context-param>

     <listener>
         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
     </listener>

     <!-- Struts2 Framework Core Filter Configuration -->
     <filter>
         <filter-name>struts2</filter-name>
         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
     </filter>
     <filter-mapping>
         <filter-name>struts2</filter-name>
         <url-pattern>/*</url-pattern>
     </filter-mapping>
 </web-app>

6. Under the src directory, create a new jdbc.properties configuration file, which reads as follows (according to your own database configuration)

jdbc.driverClass = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/yourdatabasename
jdbc.username = yourusername
jdbc.password = yourpassword

7. Configure spring-config.xml, Spring integrates Hibernate

<!-- Introduce external property files -->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!-- To configure c3p0 Connection pool -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClass}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <!-- To configure Hibernate Related attributes -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <!-- Injection connection pool -->
        <property name="dataSource" ref="dataSource"/>
        <!-- To configure Hibernate Attribute -->
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
        <!-- Load Hibernate Mapping file in -->
        <property name="mappingResources">
            <list>
                <value></value>
            </list>
        </property>
    </bean>

Topics: JDBC Hibernate Spring xml