zoukankan      html  css  js  c++  java
  • 使用maven搭建环境ssm+oracle简单项目

    2.Eclipse下创建Maven项目

     
    这里创建一个Maven project,上面的Maven Module是maven模块化开发用到的,可以理解为子工程。
    接着Next
     
     
     
    这里选择maven-archetype-webapp,创建一个maven-webapp样例工程的意思。
     
     
    此处groupid和artifactId被统称为“坐标”是为了保证项目唯一性而提出的.。org一般指一些组织机构,非盈利组织。我个人的项目一般喜欢用org。zch是我名字的缩写。artifactId写项目名称就可以了。
     
     
     
    工程创建完毕,这就是项目的结构,此处有个错误是因缺少javax.servlet-api依赖导致的。pom文件引入下列内容即可。
     
    <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>3.1.0</version>
        </dependency>
     
    现在需要改一下项目的结构。因为maven项目是需要下列Source Folder文件夹的。
    src/main/resources
    src/main/java
    src/test/resources
    src/test/java
     
     
     
    这里提示没有找到这几个文件夹。先把它们remove掉。手动创建
     
     
     
    OK,这就是maven项目的结构了,现在已经没有报错提示。提醒:此处创建的是Source Folder文件夹。不要创建成Folder。
    下面再改一下输出目录,把下面的allow output folders for source folders勾上。
     
     
    按上述步骤把那四个source folder改下输出目录。编译的class就是输出到对应的目录下。
    src/main/resources和src/main/java选择classes
    src/test/resources和src/test/java选择test-classes
     
    到此maven项目的一些基本结构已经建立完毕。下面开始搭建SSM框架开发环境。
    提示:一些maven,jdk,tomcat记得要引入配置好。这里我就不说了。
    //以下可以直接使用打的jar,因为整合的是oracle注意要引入oracle所需要的jar包,这里maven不提供oracle包需要自己下载。
     https://files.cnblogs.com/files/cyz110/portal.rar
    3.ssm框架整合
     
    首先引入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.0</modelVersion>
      <groupId>org.zch</groupId>
      <artifactId>portal</artifactId>
      <packaging>war</packaging>
      <version>0.0.1-SNAPSHOT</version>
      <name>portal Maven Webapp</name>
      <url>http://maven.apache.org</url>
      <dependencies>
            <!-- 单元测试 -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.11</version>
                <scope>test</scope>
            </dependency>


            <!-- 1.日志 -->
            <!-- 实现slf4j接口并整合 -->
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
                <version>1.1.1</version>
            </dependency>
            
            <dependency>
                <groupId>c3p0</groupId>
                <artifactId>c3p0</artifactId>
                <version>0.9.1.2</version>
            </dependency>


            <!-- DAO: MyBatis -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.3.0</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>1.2.3</version>
            </dependency>


            <!-- 3.Servlet web -->
            <dependency>
                <groupId>taglibs</groupId>
                <artifactId>standard</artifactId>
                <version>1.1.2</version>
            </dependency>
            <dependency>
                <groupId>jstl</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.5.4</version>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>3.1.0</version>
            </dependency>


            <!-- 4.Spring -->
            <!-- 1)Spring核心 -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>4.1.7.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
                <version>4.1.7.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>4.1.7.RELEASE</version>
            </dependency>
            <!-- 2)Spring DAO层 -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>4.1.7.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-tx</artifactId>
                <version>4.1.7.RELEASE</version>
            </dependency>
            <!-- 3)Spring web -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>4.1.7.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>4.1.7.RELEASE</version>
            </dependency>
            <!-- 4)Spring test -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>4.1.7.RELEASE</version>
            </dependency>


            <!-- redis客户端:Jedis -->
            <dependency>
                <groupId>redis.clients</groupId>
                <artifactId>jedis</artifactId>
                <version>2.7.3</version>
            </dependency>
            <dependency>
                <groupId>com.dyuproject.protostuff</groupId>
                <artifactId>protostuff-core</artifactId>
                <version>1.0.8</version>
            </dependency>
            <dependency>
                <groupId>com.dyuproject.protostuff</groupId>
                <artifactId>protostuff-runtime</artifactId>
                <version>1.0.8</version>
            </dependency>


            <!-- Map工具类 -->
            <dependency>
                <groupId>commons-collections</groupId>
                <artifactId>commons-collections</artifactId>
                <version>3.2</version>
            </dependency>
            
            <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.7.4</version>
    </dependency>
            
       </dependencies>
      
    <build>
    <resources>
                <resource>
                  <directory>src/main/java</directory>
                   <includes>
                        <include>**/*.properties</include>
                        <include>**/*.xml</include>
                    </includes>
                    <filtering>false</filtering>
                </resource>
            </resources>
    </build>
    </project>
     
     
    applicationContext-dao.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"
        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">
        <!-- 配置整合mybatis过程 -->
        <!-- 1.配置数据库相关参数properties的属性:${url} -->
        <context:property-placeholder location="classpath:jdbc.properties" />


        <!-- 2.数据库连接池 -->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <!-- 配置连接池属性 -->
            <property name="driverClass" value="${jdbc.driver}" />
            <property name="jdbcUrl" value="${jdbc.url}" />
            <property name="user" value="${jdbc.username}" />
            <property name="password" value="${jdbc.password}" />


            <!-- c3p0连接池的私有属性 -->
            <property name="maxPoolSize" value="30" />
            <property name="minPoolSize" value="10" />
            <!-- 关闭连接后不自动commit -->
            <property name="autoCommitOnClose" value="false" />
            <!-- 获取连接超时时间 -->
            <property name="checkoutTimeout" value="10000" />
            <!-- 当获取连接失败重试次数 -->
            <property name="acquireRetryAttempts" value="2" />
        </bean>


       <!-- mybatis config -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation" value="classpath:mybatis-config.xml" />
    </bean>
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="org.portal.dao" />
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    </bean>
     
    </beans>
     
    读入数据库连接相关参数,配置数据连接池,以及sqlSessionFactory
    可以看到数据库的一些连接信息是从其他文件取值的。所以现在需要一个jdbc.properties配置数据库地址等信息。
     
     
    这里我就打个码了。也提醒各位分享技术贴图贴代码的同时,不要暴露自己的一些隐私信息。
    因为我的数据库是装在阿里云上面的。所以我写的是我服务器的地址。如果是本地的话localhost就行了。
     
    mapper映射
     
     
     
    resources下新建一个mapper文件夹。用以存放跟Dao类相映射的XML文件
     
     
    resources下新建一个mybatis-config.xml:
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE configuration
    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
    <settings>
    <!-- changes from the defaults -->
    <setting name="lazyLoadingEnabled" value="false" />
    <!-- JdbcType enumeration. Most common are: NULL, VARCHAR and OTHER -->
    <setting name="jdbcTypeForNull" value="NULL" />
    </settings>
    <typeAliases>
    <typeAlias alias="User" type="org.portal.entity.User" />
    </typeAliases>

    <mappers>
    <mapper resource="mapper/UserDao.xml"/>
    </mappers>
    </configuration>
     
    让实体bean映射xml
     
    applicationContext-service.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:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
    http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">
    <context:component-scan base-package="org.portal.service"></context:component-scan>
    </beans>
     
    此处目前整合阶段暂时没有多少东西。就一个扫描service包
     
    applicationContext-trans.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:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
    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
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">
    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
    <!-- 配置数据源 -->
    <property name="dataSource" ref="dataSource" ></property>
    </bean>
    <!-- 配置事务 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
    <tx:method name="save*" propagation="REQUIRED" />
    <tx:method name="insert*" propagation="REQUIRED" />
    <tx:method name="add*" propagation="REQUIRED" />
    <tx:method name="create*" propagation="REQUIRED" />
    <tx:method name="delete*" propagation="REQUIRED" />
    <tx:method name="update*" propagation="REQUIRED" />
    <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
    <tx:method name="select*" propagation="SUPPORTS" read-only="true" />
    <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
    </tx:attributes>
    </tx:advice>
    <!-- 切面 -->
    <aop:config>
    <aop:advisor advice-ref="txAdvice" pointcut="execution(* org.portal.service.impl.*.*(..))" />


    </aop:config>
    </beans>
     
    这是配置spring对事务的处理
     
    springmvc.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:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
    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/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">
    <context:component-scan base-package="org.portal.controller"/>
    <!-- 配置处理器映射器  适配器 -->
    <mvc:annotation-driven />


    <!-- 配置视图解释器 jsp -->
    <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/view/"/>
    <property name="suffix" value=".jsp"/>
    </bean>
    </beans>
     
    这是配置springmvc。prefix是你的页面文件夹
    suffix是后缀名。
     
    web.xml配置
     
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0" 
    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_3_0.xsd">
    <display-name>portal</display-name>
    <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <!-- 加载spring容器 -->
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/applicationContext-*.xml</param-value>
    </context-param>
    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- 解决post乱码 -->
    <filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
    <param-name>encoding</param-name>
    <param-value>utf-8</param-value>
    </init-param>
    <!-- <init-param>
    <param-name>forceEncoding</param-name>
    <param-value>true</param-value>
    </init-param> -->
    </filter>
    <filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!-- springmvc的前端控制器 -->
    <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation, springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" -->
    <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/springmvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
    </servlet-mapping>
      
    </web-app>
    这是一个初始化方法,访问项目可以直接到首页,虽然web.xml也可以配置,但我更倾向于这样写。因为可以在这方法里做一些初始化加载数据等操作。
     
    show.jsp文件内容

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%--
    Created by IntelliJ IDEA.
    user: 13926
    Date: 2017/7/18
    Time: 23:07
    To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
    <title>Title</title>
    </head>
    <body>
    <table border="1">
    <tr>
    <td>序号</td>
    <td>姓名</td>
    <td>年龄</td>
    </tr>
    <c:choose>
    <c:when test="${not empty userList}">
    <c:forEach items="${userList}" var="user" varStatus="vs">
    <tr>
    <td>${user.id}</td>
    <td>${user.name}</td>
    <td>${user.age}</td>
    </tr>
    </c:forEach>
    </c:when>
    <c:otherwise>
    <tr>
    <td colspan="2">无数据!</td>
    </tr>
    </c:otherwise>
    </c:choose>
    </table>
    </body>
    </html>

     
    现在开始测试
     
     
  • 相关阅读:
    项目质量管理
    项目成本管理
    项目进度管理
    项目范围管理
    项目整体管理
    项目立项管理
    信息系统项目管理基础
    信息化和信息系统
    linux(3)
    Patorjk
  • 原文地址:https://www.cnblogs.com/cyz110/p/8677498.html
Copyright © 2011-2022 走看看