第一次写博客大家多多见谅
1.首先pom文件jar包的引入
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0modelVersion> <groupId>com.sshgroupId> <artifactId>SSHartifactId> <packaging>warpackaging> <version>1.0-SNAPSHOTversion> <name>SSH Maven Webappname> <url>http://maven.apache.orgurl> <properties> <project.build.sourceEncoding>UTF-8project.build.sourceEncoding> <spring.version>3.1.1.RELEASEspring.version> <hibernate.version>3.6.5.Finalhibernate.version> <struts2.version>2.3.1struts2.version> properties> <dependencies> <dependency> <groupId>junitgroupId> <artifactId>junitartifactId> <version>3.8.1version> <scope>testscope> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-coreartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-contextartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-jdbcartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-beansartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-webartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-expressionartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-ormartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>cglibgroupId> <artifactId>cglib-nodepartifactId> <version>2.1_3version> dependency> <dependency> <groupId>org.hibernategroupId> <artifactId>hibernate-coreartifactId> <version>${hibernate.version}version> dependency> <dependency> <groupId>org.apache.strutsgroupId> <artifactId>struts2-coreartifactId> <version>${struts2.version}version> dependency> <dependency> <groupId>org.apache.strutsgroupId> <artifactId>struts2-junit-pluginartifactId> <version>${struts2.version}version> <scope>testscope> dependency> <dependency> <groupId>org.apache.strutsgroupId> <artifactId>struts2-spring-pluginartifactId> <version>${struts2.version}version> dependency> <dependency> <groupId>mysqlgroupId> <artifactId>mysql-connector-javaartifactId> <version>5.0.5version> dependency> <dependency> <groupId>c3p0groupId> <artifactId>c3p0artifactId> <version>0.9.1.2version> dependency> dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-pluginartifactId> <version>2.3.2version> <configuration> <source>1.8source> <target>1.8target> configuration> plugin> <plugin> <artifactId>maven-war-pluginartifactId> <version>2.2version> <configuration> <failOnMissingWebXml>falsefailOnMissingWebXml> configuration> plugin> plugins> <finalName>SSHfinalName> build> project>
2.建立存储java文件的package
3.构建Spring的配置文件applicationContext.xml,db.properties数据库连接配置文件
(1)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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <context:property-placeholder location="classpath:db.properties" /> <context:component-scan base-package="com.ssh.action"/> <context:component-scan base-package="com.ssh.serviceImpl"/> <context:component-scan base-package="com.ssh.daoImpl" /> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${database.driverClassName}">property> <property name="jdbcUrl" value="${database.url}">property> <property name="user" value="${database.username}">property> <property name="password" value="${database.password}">property> <property name="maxPoolSize"> <value>50value> property> <property name="minPoolSize"> <value>5value> property> <property name="initialPoolSize"> <value>5value> property> <property name="maxIdleTime"> <value>20value> property> <property name="maxStatements"> <value>50value> property> <property name="acquireIncrement"> <value>20value> property> bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialectprop> <prop key="hibernate.autoReconnect">trueprop> <prop key="hibernate.hbm2ddl.auto">updateprop> <prop key="hibernate.show_sql">trueprop> <prop key="hibernate.format_sql">trueprop> <prop key="hibernate.enable_lazy_load_no_trans">trueprop> <prop key="current_session_context_class">threadprop> <prop key="hibernate.max_fetch_depth">3prop> <prop key="hibernate.connection.url">jdbc:mysql://localhost:3306/sshprop> <prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driverprop> props> property> <property name="packagesToScan"> <list> <value>com.ssh.entityvalue> list> property> bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> bean> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/> beans>(2)db.properties
database.database=mysql database.driverClassName=com.mysql.jdbc.Driver database.username=root database.password=1234 database.show_sql=true database.url=jdbc:mysql://localhost:3306/ssh?useUnicode=true&characterEncoding=UTF-8
注意:由于struts中的action和service等交给spring这里扫描的是实现类。
4.Hibernate实体类的生成
(1)首先数据表这个大家随便建。
(2)IDE建立数据库连接(我这已经建立好了)没有的可以点击"+"(点击Database)
(3) 然后点击左下角的Persintence,右键选择最后一项,生成Entity
5.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"> <listener> <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class> listener> <context-param> <param-name>contextConfigLocationparam-name> <param-value>classpath:applicationContext.xmlparam-value> context-param> <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>6.struts2.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> <constant name="struts.devMode" value="false"/> <constant name="struts.objectFactory" value="spring" /> <package name="user" extends="struts-default"> <action name="user_*" class="useraction" method="{1}"> <result name="success">/success.jspresult> <result name="error">/error.jspresult> action> package> struts>