如何搭建SSH+JPA+mysql项目

1.打开eclipse,新建一个dynamic web project

2.导入所需的lib(需要提前下载好)

struts-lib: struts\apps\struts2-blank\WEB-INF\lib

spring-lib:spring-framework-4.1.0.RELEASE\libs

hibernate-lib:hibernate-release-4.3.10.Final\lib\required

jpa-lib: hibernate-release-4.3.10.Final\lib\jpa

二级缓存lib: hibernate-release-4.3.10.Final\lib\optional\ehcache

mysql-lib: mysql-connector-java-5.1.7-bin.jar

c3p0-lib:hibernate-release-4.3.10.Final\lib\optional\c3p0

并删除lib中 javassist-*.jar中版本低的,然后build path

3.新建db.properties数据库配置文件

user=root
password=
jdbcUrl=jdbc:mysql:///sshjpa
driverClass=com.mysql.jdbc.Driver

4.获取struts.xml
从struts-2.3.20.1\apps\struts2-blank\WEB-INF\src\java中复制struts.xml到src目录下

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE 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>

5.获取web.xml
从struts-2.3.20.1\apps\struts2-blank\WEB-INF中复制web.xml到WEB-INF目录下

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Struts Blank</display-name>

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

    <!-- needed for ContextLoaderListener -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <!-- Bootstraps the root web application context before servlet initialization -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

</web-app>

6.新建applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
    <!-- 导入资源文件 -->
    <context:property-placeholder location="classpath:db.properties"/>
    <!-- 配置数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" p:user="${user}" p:password="${password}" p:jdbcUrl="${jdbcUrl}" p:driverClass="${driverClass}" p:minPoolSize="1" p:maxPoolSize="10" p:acquireIncrement="2" p:maxStatements="5" />

    <!-- 配置EntityManagerFactory -->
    <bean id="EntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <!-- 数据源 -->
        <property name="dataSource" ref="dataSource"></property>
        <!-- 配置JPA提供商的适配器 -->
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
        </property>
        <!-- 配置JPA基本属性 -->
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
        <!-- 配置实体类所在的包 -->
        <property name="packagesToScan" value="com.sshjpa.entities"/>
    </bean>
    <!-- 配置JPA的事务管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="EntityManagerFactory"></property>
    </bean>
    <!-- 配置支持基于注解的事务配置 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
    <!-- 启动AspectJ支持 -->
    <aop:aspectj-autoproxy/>
        <!-- 扫描组件 -->
    <context:component-scan base-package="com.sshjpa.entities"/>
</beans>

7.建立一个package,命名为com.sshjpa.entities

8.在com.sshjpa.entities下建立一个class,命名为User

package com.sshjpa.entities;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import org.springframework.stereotype.Component;

@Component
@Table(name="users")
@Entity
public class User {
    private Integer id;
    private String name;

    @Column(name="ID")
    @GeneratedValue
    @Id
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    @Column(name="NAME")
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

最后,启动项目,看数据库是否产生users表,如果有,则成功,反之,失败

全部评论

相关推荐

10-09 19:35
门头沟学院 Java
洛必不可达:java的竞争激烈程度是其他任何岗位的10到20倍
点赞 评论 收藏
分享
蚂蚁 基架java (n+6)*16 签字费若干
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务