一直在用ssm做项目,好久没有做ssh项目了。用ssm+oracle来开发真的是一步一个坑。基本上每个操作都会出问题。下面从环境搭建开始来说明各种坑。
一、使用各种环境的版本
Hibernate 5.x 其实只用到了4.x的部分功能
Struts2 2.3.16 (没用最新版)
Spring 4.x
Oracle 11g
省略其他小类库…………………………..
二、maven环境搭建
2.1 pom.xml文件
<properties>
<org.hibernate-version>4.3.1.Final</org.hibernate-version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!—本地仓库中的插件-->
<!-- https://mvnrepository.com/artifact/ojdbc/ojdbc -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0</version>
</dependency>
<!-- sqlserver依赖-->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>4.0</version>
</dependency>
<!--1.2 配置c3p0数据库连接池 -->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<!-- servlet3 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<!--三、配置spring相关 -->
<!--3.1 spring core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.3.RELEASE</version>
</dependency>
<!--3.2 spring ioc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.1.3.RELEASE</version>
</dependency>
<!--3.2 spring orm 相关 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.3.RELEASE</version>
</dependency>
<!--3.3 spring jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.1.3.RELEASE</version>
</dependency>
<!-- 3.4 spring 事务-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.1.3.RELEASE</version>
</dependency>
<!--3.2 spring 单元、集成测试 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.1.3.RELEASE</version>
<scope>test</scope>
</dependency>
<!-- 界面懒加载问题-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!--日志相关 -->
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- hiberante 4 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${org.hibernate-version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${org.hibernate-version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${org.hibernate-version}</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.0.0.GA</version>
<scope>provided</scope>
</dependency>
<!--enable hibernate to use its proxy model-->
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.18.1-GA</version>
</dependency>
<!--struts2 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.16</version>
<exclusions>
<exclusion>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.3.16</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-convention-plugin</artifactId>
<version>2.3.16</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-json-plugin -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-json-plugin</artifactId>
<version>2.3.16</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.21</version>
2.2 注意事项
2.2.1 加载oracle 驱动到本地仓库
由于oracle 没有提供第三方maven库,因此需要自己将oracle jar添加到本地maven库中去。在我本机中安装了oracle数据库,在安装包中自带了驱动。位置在下图的路径中
说明:之前用的驱动是ojdbc14.jar ,此图中ojdbc5.jar ojdbc6.jar .期中14代表支持jdk1.4版本以上。5代表支持5.0 ,6代表6.0 以此类推。我本地jdk是1.7 所以使用ojdbc6.
向本地maven库中添加ojdbc maven命令:
命令如下:
mvn install:install-file -Dfile=D:/app/zyz/product/11.2.0/dbhome_1/jdbc/lib/ojdbc6.jar -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0 -Dpackaging=jar
D:/app/zyz/product/11.2.0/dbhome_1/jdbc/lib/ojdbc6.jar:此为笔者的ojdbc6.jar绝对路径
安装完成后引入jar
<dependencies>
<!-- 添加oracle jdbc driver -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0</version>
</dependency>
</dependencies>
参考链接:http://www.th7.cn/db/Oracle/201502/92865.shtml
2.2.2 Struts2与hibernate中 关于javassist.jar 会有所冲突,因此要将低版本的javassist.jar 从依赖中删除。
删除依赖如下:
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.16</version>
<!—表示不接受传递依赖 --
<exclusions>
<exclusion>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
</exclusion>
</exclusions>
</dependency>
三、ssh环境搭建
系统工程架构如下:
3.1 数据库连接配置信息-jdbc.properties
# mysql连接配置
#driverClass=com.mysql.jdbc.Driver
#jdbcUrl=jdbc:mysql://localhost:3306/hibernate?useUnicode=true&characterEncoding=utf8
#user=root
#password=x5
#hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
# sqlserver连接配置
#driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
#url=jdbc:sqlserver://127.0.0.1;database=hibernate
#user=sa
#password=123
# oracle连接配置
driverClass=oracle.jdbc.driver.OracleDriver
jdbcUrl=jdbc:oracle:thin:@127.0.0.1:1521:orcl
user=keyan
password=keyan
#config hibernate properties
hibernate.dialect=org.hibernate.dialect.Oracle9Dialect
hibernate.show_sql=true
hibernate.format_sql=false
hibernate.jdbc.batch_size=10
hibernate.hibernate.connection.SetBigStringTryClob=true
hibernate.hibernate.archive.autodetection=class
hibernate.hibernate.connection.charSet=UTF-8
hibernate.hibernate.cache.use_query_cache=false
hibernate.hibernate.cache.use_second_level_cache=false
3.2 spring 配置信息
结构图如下:
3.2.1 dao配置
<?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:context="http://www.springframework.org/schema/context"
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.xsd">
<!--申明注解方式 -->
<context:annotation-config/>
<!-- 加载jdbc配置文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!--数据库连接属性 -->
<property name="driverClass" value="${driverClass}"/>
<property name="jdbcUrl" value="${jdbcUrl}"/>
<property name="user" value="${user}"/>
<property name="password" value="${password}"/>
<!-- 数据源连接配置-->
<property name="autoCommitOnClose" value="false"/>
<property name="checkoutTimeout" value="3000"/>
<property name="acquireRetryAttempts" value="3"/>
<property name="maxPoolSize" value="5"/>
<property name="minPoolSize" value="3"/>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!--扫描包配java备案配置 -->
<property name="packagesToScan" value="com.web.bean" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>
<prop key="hibernate.hibernate.connection.SetBigStringTryClob">${hibernate.hibernate.connection.SetBigStringTryClob}</prop>
<prop key="hibernate.hibernate.archive.autodetection">${hibernate.hibernate.archive.autodetection}</prop>
<prop key="hibernate.hibernate.connection.charSet">${hibernate.hibernate.connection.charSet}</prop>
<prop key="hibernate.hibernate.cache.use_query_cache">${hibernate.hibernate.cache.use_query_cache}</prop>