岁数大了,搭个框架用了一天半,跌跌撞撞的,算基本走通,记录下流程和期间用到的问题。
一直懒得用Maven,怕麻烦,这次用是因为Spring官网改版了,必须要用Maven的方式下载,初步用了一下,挺好用,可以把依赖的包都下载下来,但麻烦的是,在eclipse建项目的时候,不是那么流畅,比如web.xml还需要手动创建。配置项目需要用到的包也是,你得找maven是否支持这种包,而且还得写清版本等,查包的网址是:
http://central.maven.org/maven2/oracle/toplink/essentials/
我用的是myeclipse10,没有遇到网上说的还得配置文件夹映射关系之类的问题,直接创建Maven Project项目:
以上两步即可,然后在src/main/webapp/web-inf下创建一个web.xml,然后随便整个jsp,将项目发布到tomcat下,直接就能用了。
-----------------------------------------------------------------------------------------------------------------------------------------
接下来就是整jar包的过程了,用了不少时间,找到项目根目录下的pom.xml文件,加入包,之后的几篇文章可能会增减,会在之后的文章中说明的
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.mice</groupId>
<artifactId>SayGoodbye001</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build/>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<!-- 与spring-data-jpa冲突
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
</dependency>
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.hateoas</groupId>
<artifactId>spring-hateoas</artifactId>
<version>0.7.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.4.2.RELEASE</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.1.6.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.1.6.Final</version>
</dependency>
</dependencies>
</project>
保存后,Maven便会去网站上去下载相关jar包以及依赖,这点是非常爽的,但是找版本号,颇费尽,熟悉了之后会好很多吧。
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/SpringMVC-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/applicationContext.xml</param-value>
</context-param>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
在src/main/java/config文件下创建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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<!-- 定义实体管理器工厂 -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="myJPA"/>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<!-- 启用 annotation事务-->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 配置Spring Data JPA扫描目录-->
<jpa:repositories base-package="org.mice.test"/>
</beans>
再创建一个SpringMVC-servlet.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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver"><!-- 指定策略解析 --><property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /><!-- 指定视图处理 --><property name="prefix" value="/jsp/" /><!--前缀--><property name="suffix" value=".jsp" /><!--后缀--> </bean> </beans><!-- 启用spring mvc 注解 --> <context:annotation-config /> <!-- 设置使用注解的类所在的jar包 --> <context:component-scan base-package="org.mice"/>
最后,在src/main/java/META-INF下创建persistence.xml文件,采用的hibernate的jpa实现,学习嘛,能通就行,重在整体,数据库直接用的oracle,由于maven没有oracle的驱动,我也没采用网上的手动添加到本地maven库的方式,直接通过eclipse配置到项目中。
<?xml version="1.0" encoding="UTF-8"?> <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> <persistence-unit name="myJPA" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <properties> <!--配置Hibernate方言 --> <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9Dialect" /> <!--配置数据库驱动 --> <property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver" /> <!--配置数据库用户名 --> <property name="hibernate.connection.username" value="chenph" /> <!--配置数据库密码 --> <property name="hibernate.connection.password" value="chenph" /> <!--配置数据库url --> <property name="hibernate.connection.url" value="jdbc:oracle:thin:@192.168.101.179:1521:orcl" /> <!--设置外连接抓取树的最大深度 --> <property name="hibernate.max_fetch_depth" value="3" /> <!--自动输出schema创建DDL语句 --> <property name="hibernate.hbm2ddl.auto" value="update" /> <property name="hibernate.show_sql" value="true" /> <property name="hibernate.format_sql" value="true" /> <property name="javax.persistence.validation.mode" value="none"/> </properties> </persistence-unit> </persistence>