web项目配置,使用maven,spring,spring mvc,mybatis,mysql

创建maven项目

创建后项目目录

 

 

创建java目录,如果创建不了到build-path删掉missing

配置jdk,ecplise默认是1.8没装

tomcat启动

项目目录

各文件修改

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/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>com.complay.xx</groupId>

  <artifactId>xx-web</artifactId>

  <packaging>war</packaging>

  <version>0.0.1-SNAPSHOT</version>

  <name>web Maven Webapp</name>

  <url>http://maven.apache.org</url>

      <properties>

        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        <junit.version>4.9</junit.version>

        <spring.version>4.3.10.RELEASE</spring.version>

        <mybatis.version>3.4.4</mybatis.version>

        <mybatis.spring.version>1.3.1</mybatis.spring.version>

        <commons-dbcp.version>1.4</commons-dbcp.version>

        <jstl.version>1.2</jstl.version>

        <log4j.version>1.2.17</log4j.version>

        <fastjson.version>1.2.35</fastjson.version>

        <slf4j.version>1.7.25</slf4j.version>

        <jackson.version>1.9.9</jackson.version>

        <commons-fileupload.version>1.3.3</commons-fileupload.version>

        <commons-io.version>2.5</commons-io.version>

        <commons-codec.version>1.10</commons-codec.version>

        <aspectjweaver.version>1.8.10</aspectjweaver.version>

    </properties>

 

 

  <dependencies>

     <!-- Junit测试 -->

            <dependency>

                <groupId>junit</groupId>

                <artifactId>junit</artifactId>

                <version>${junit.version}</version>

                <scope>test</scope>

            </dependency>

 

            <!-- spring coreweboxmtxjdbcwebmvcaopcontexttest -->

            <dependency>

                <groupId>org.springframework</groupId>

                <artifactId>spring-core</artifactId>

                <version>${spring.version}</version>

            </dependency>

            <dependency>

                <groupId>org.springframework</groupId>

                <artifactId>spring-web</artifactId>

                <version>${spring.version}</version>

            </dependency>

            <dependency>

                <groupId>org.springframework</groupId>

                <artifactId>spring-oxm</artifactId>

                <version>${spring.version}</version>

            </dependency>

            <dependency>

                <groupId>org.springframework</groupId>

                <artifactId>spring-tx</artifactId>

                <version>${spring.version}</version>

            </dependency>

            <dependency>

                <groupId>org.springframework</groupId>

                <artifactId>spring-jdbc</artifactId>

                <version>${spring.version}</version>

            </dependency>

            <dependency>

                <groupId>org.springframework</groupId>

                <artifactId>spring-webmvc</artifactId>

                <version>${spring.version}</version>

            </dependency>

            <dependency>

                <groupId>org.springframework</groupId>

                <artifactId>spring-aop</artifactId>

                <version>${spring.version}</version>

            </dependency>

 

            <dependency>

                <groupId>org.springframework</groupId>

                <artifactId>spring-context-support</artifactId>

                <version>${spring.version}</version>

            </dependency>

            <dependency>

                <groupId>org.springframework</groupId>

                <artifactId>spring-test</artifactId>

                <version>${spring.version}</version>

            </dependency>

            <!-- mybatis核心包 -->

            <dependency>

                <groupId>org.mybatis</groupId>

                <artifactId>mybatis</artifactId>

                <version>${mybatis.version}</version>

            </dependency>

            <!-- mybatis/spring -->

            <dependency>

                <groupId>org.mybatis</groupId>

                <artifactId>mybatis-spring</artifactId>

                <version>${mybatis.spring.version}</version>

            </dependency>

 

            <!-- 导入Mysql数据库链接jar -->

            <dependency>

                <groupId>mysql</groupId>

                <artifactId>mysql-connector-java</artifactId>

                <version>6.0.6</version>

            </dependency>

 

            <!-- 导入dbcpjar包,用来在applicationContext.xml中配置数据库 -->

            <dependency>

                <groupId>commons-dbcp</groupId>

                <artifactId>commons-dbcp</artifactId>

                <version>${commons-dbcp.version}</version>

            </dependency>

            <!-- JSTL标签类 -->

            <dependency>

                <groupId>jstl</groupId>

                <artifactId>jstl</artifactId>

                <version>${jstl.version}</version>

            </dependency>

            <!-- 日志文件管理包 -->

            <dependency>

                <groupId>log4j</groupId>

                <artifactId>log4j</artifactId>

                <version>${log4j.version}</version>

            </dependency>

 

            <!-- 格式化对象,方便输出日志 -->

            <dependency>

                <groupId>com.alibaba</groupId>

                <artifactId>fastjson</artifactId>

                <version>${fastjson.version}</version>

            </dependency>

    <dependency>

        <groupId>mysql</groupId>

        <artifactId>mysql-connector-java</artifactId>

        <version>5.1.15</version>

    </dependency>

 

            <dependency>

                <groupId>org.slf4j</groupId>

                <artifactId>slf4j-api</artifactId>

                <version>${slf4j.version}</version>

            </dependency>

 

            <dependency>

                <groupId>org.slf4j</groupId>

                <artifactId>slf4j-log4j12</artifactId>

                <version>${slf4j.version}</version>

            </dependency>

            <!-- jsonbean之间相互转换 -->

            <dependency>

                <groupId>org.codehaus.jackson</groupId>

                <artifactId>jackson-core-asl</artifactId>

                <version>${jackson.version}</version>

            </dependency>

            <dependency>

                <groupId>org.codehaus.jackson</groupId>

                <artifactId>jackson-mapper-asl</artifactId>

                <version>${jackson.version}</version>

            </dependency>

      

            <!-- 上传组件包 -->

            <dependency>

                <groupId>commons-fileupload</groupId>

                <artifactId>commons-fileupload</artifactId>

                <version>${commons-fileupload.version}</version>

            </dependency>

            <dependency>

                <groupId>commons-io</groupId>

                <artifactId>commons-io</artifactId>

                <version>${commons-io.version}</version>

            </dependency>

            <dependency>

                <groupId>commons-codec</groupId>

                <artifactId>commons-codec</artifactId>

                <version>${commons-codec.version}</version>

            </dependency>

 

 

            <dependency>

                <groupId>org.aspectj</groupId>

                <artifactId>aspectjweaver</artifactId>

                <version>${aspectjweaver.version}</version>

            </dependency>

 

            <dependency>

                <groupId>javax.servlet</groupId>

                <artifactId>javax.servlet-api</artifactId>

                <version>4.0.0-b07</version>

                <scope>provided</scope>

            </dependency>

  </dependencies>

  <build>

  <finalName>web</finalName>

    <plugins>

           <plugin>

              <groupId>org.codehaus.mojo</groupId>

              <artifactId>tomcat-maven-plugin</artifactId>

              <configuration>

                  <port>6088</port>

                  <path>/web</path>

                  <uriEncoding>UTF-8</uriEncoding>

                  <!--<contextReloadable>true</contextReloadable> -->

              </configuration>

           </plugin>

            <plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-compiler-plugin</artifactId>

                <version>2.3.2</version>

                <configuration>

                <source>1.8</source>

                <target>1.8</target>

                </configuration>

            </plugin>

        </plugins>

  </build>

</project>

 

 

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

      <context-param>

        <param-name>contextConfigLocation</param-name>

        <param-value>classpath*:springContext.xml</param-value>

    </context-param>

    <listener>

        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

    </listener>

   

    <!-- 字符集过滤器 --> 

    <filter>

        <filter-name>encodingFilter</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>

    </filter>

    <filter-mapping>

        <filter-name>encodingFilter</filter-name>

        <url-pattern>/*</url-pattern>

    </filter-mapping>

   

    <!-- spring mvc 配置 -->

    <!-- 配置DispatcherServlet表示,该工程将采用springmvc的方式。启动时也会默认在/WEB-INF目录下查找XXX-servlet.xml作为配置文件,

        XXX就是DispatcherServlet的名字,该文件中将配置两项重要的mvc特性:HandlerMapping,负责为DispatcherServlet这个前端控制器的请求查找Controller

        ViewResolver,负责为DispatcherServlet查找ModelAndView的视图解析器。

        此处使用指定的配置文件spring-mvc.xml -->

    <servlet>

        <servlet-name>contacts</servlet-name>

        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <init-param> 

            <param-name>contextConfigLocation</param-name> 

            <!--<param-value>/WEB-INF/classes/spring-mvc-servlet.xml</param-value>-->

            <param-value>classpath*:/spring-mvc.xml</param-value>

        </init-param> 

        <load-on-startup>1</load-on-startup> 

    </servlet>

    <servlet-mapping>

        <servlet-name>contacts</servlet-name>

        <url-pattern>*.do</url-pattern>

    </servlet-mapping>

  <display-name>Archetype Created Web Application</display-name>

</web-app>

 

Jdbc.properties

jdbc.driverClassName=com.mysql.jdbc.Driver

 

jdbc.url=jdbc:mysql://localhost:3306/cmdb_user?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&failOverReadOnly=false

jdbc.username=root

jdbc.password=root

 

 

spring-mvc.xml

 

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:mvc="http://www.springframework.org/schema/mvc" 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-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/mvc

       http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"

       xmlns:util="http://www.springframework.org/schema/util

        http://www.springframework.org/schema/util

    http://www.springframework.org/schema/util/spring-util-3.0.xsd">

<context:annotation-config />

    <!-- 自动扫描且只扫描@Controller -->

    <context:component-scan base-package="com.pp" use-default-filters="false">

       <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />

    </context:component-scan>

   

<mvc:annotation-driven />

 

    <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->

    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">

    </bean>

 

   

</beans>

 

 

Springcontext.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:aop="http://www.springframework.org/schema/aop"

    xmlns:tx="http://www.springframework.org/schema/tx"

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

 

    <!-- bean definitions here -->

    <context:component-scan base-package="com.pp">

       <context:exclude-filter type="annotation"

           expression="org.springframework.stereotype.Controller" />

    </context:component-scan>

   

    <!-- 配置读取外部配置文件 -->

    <context:property-placeholder location="classpath:jdbc.properties" />

  

    <context:annotation-config /> <!-- 此行语句使得resource autowired 等四个注解可以使用 -->

    <bean id="default" class="org.apache.commons.dbcp.BasicDataSource">

       <property name="driverClassName"><value>${jdbc.driverClassName}</value></property>

       <property name="url"><value>${jdbc.url}</value></property>

       <property name="username"><value>${jdbc.username}</value></property>

       <property name="password"><value>${jdbc.password}</value></property>

       <!-- 配置初始化大小、最小、最大 -->

       <property name="initialSize" value="1" />

       <property name="maxActive" value="20" />

       <property name="minIdle" value="3" />

       <!-- 配置获取连接等待超时的时间 -->

       <property name="maxWait" value="60000" />

        <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->

       <property name="timeBetweenEvictionRunsMillis" value="60000" />

       <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->

       <property name="minEvictableIdleTimeMillis" value="300000" />

       <property name="testWhileIdle" value="true" />

        <!-- 这里建议配置为TRUE,防止取到的连接不可用 -->

       <property name="testOnBorrow" value="true" />

       <property name="testOnReturn" value="false" />

       <!-- 验证连接有效与否的SQL,不同的数据配置不同 -->

       <property name="validationQuery" value="select 1 " />

    </bean>

     <bean id="SessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

       <property name="dataSource" ref="default" />

       <property name="mapperLocations" value="classpath:mapping/*.xml"/>

    </bean>

        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

        <property name="basePackage" value="com.pp.xx" />

        <property name="sqlSessionFactoryBeanName" value="SessionFactory"/>

    </bean> 

    <!-- 开启事务注解驱动 -->

    <tx:annotation-driven />

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

        <property name="dataSource" ref="default" />

    </bean>

   

 

</beans>

 

Log4j.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">

 

<log4j:configuration>

 

    <!-- 控制台Appender -->

    <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">

       <layout class="org.apache.log4j.PatternLayout">

           <param name="ConversionPattern" value="%d - %c -%-4r [%t] %-5p %x - %m%n" />

       </layout>

    </appender>

 

    <!-- 异常文本日志 -->

    <appender name="Exception_File" class="org.apache.log4j.DailyRollingFileAppender">

       <!--设置通道名称是:file,输出方式DailyRollingFileAppender -->

       <param name="File" value="./logs/exception.txt" />

       <!--日志文件路径和文件名称 -->

       <param name="Append" value="true" />

       <param name="DatePattern" value="’.’yyyy-MM-dd" />

       <layout class="org.apache.log4j.PatternLayout">

           <param name="ConversionPattern"

              value="------------------- %d{yyyy-MM-dd HH\\:mm\\:ss,SSS} %-5r %-5p [%c] -----------------------%n%m%n" />

       </layout>

       <filter class="org.apache.log4j.varia.LevelRangeFilter">

           <param name="LevelMin" value="INFO" />

           <param name="LevelMax" value="ERROR" />

       </filter>

    </appender>

 

    <logger name="Exception_File_Logger">

       <level value="INFO" />

       <appender-ref ref="Exception_File" />

    </logger>

 

    <root>

       <level value="INFO"></level>

       <appender-ref ref="Exception_File" />

       <appender-ref ref="CONSOLE" />

    </root>

 

</log4j:configuration>

 

Action.java

package com.pp.xx;

 

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import com.pp.xx.User;

import com.pp.xx.UserMapper;

 

@Controller

public class action {

    @Autowired

    private UserMapper userMapper;

    @RequestMapping(value = "/downloadAgent.do",method = RequestMethod.GET , produces = "application/json;charset=UTF-8")

    @ResponseBody

    public  String downloadAgent(String downurl,HttpServletRequest request,HttpServletResponse response){

       User user=userMapper.selectByPrimaryKey("59c10d7d-9ef2-432d-930a-945e4d19560b");

       return  JSON.toJSONString(user);      

    }

}

 

 

数据库连接的UserMapper.xml,User.java,UserMapper.java使用mybatis-generator生成

有些不影响的报错,都是版本对不上,在.settingjdk版本改


虚像:

评论 阅读