首页 > 代码库 > (一)springmvc+spring+mybatis+maven框架搭建

(一)springmvc+spring+mybatis+maven框架搭建

(一)springmvc+spring+mybatis+maven框架搭建

 

1.说明

工作之余,为了学习点东西。先搭建个框架。

以后要往里面加东西,比如rabbitMQ、redis、shiro等。

也是为了自己学习做一个demo吧。

2.项目文件

介绍:

application.properties文件:一些数据库连接参数,以后redis、rabbitMQ参数也加在里面。

applicationContet.xml文件: spring配置文件

log4j.properties:日志文件

message_en.properties和messages_zh_CN.properties文件:国际化文件

mybatis-config.xml文件:mybatis配置文件

spring-mvc.xml文件:mvc配置文件

技术分享

3.maven的pom.xml文件

文件里引入了很多jar包,大家可以根据自己需要加自己用到的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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.ln</groupId>
  <artifactId>ssm_project</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  
   <build>
        <finalName>ssm_project</finalName>
        <plugins>
            <!-- Mybatis generator代码生成插件 配置 -->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>${plugin.mybatis.generator}</version>
                <configuration>
                    <configurationFile>${mybatis.generator.generatorConfig.xml}</configurationFile>
                    <overwrite>true</overwrite>
                    <verbose>true</verbose>
                </configuration>
            </plugin>

            <!-- Maven插件 配置 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${plugin.maven-compiler}</version>
                <configuration>
                    <source>${project.build.jdk}</source>
                    <target>${project.build.jdk}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${plugin.maven-surefire}</version>
                <configuration>
                	<forkMode>once</forkMode>  
                    <argLine>-Dfile.encoding=UTF-8</argLine>
                    <skipTests>${skipTests}</skipTests>
                </configuration>
            </plugin>
            
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>tomcat-maven-plugin</artifactId>
                <version>1.1</version>
                <configuration>
                    <path>/</path>
                    <port>8080</port>
                    <uriEncoding>UTF-8</uriEncoding>
                    <url>http://localhost:8080/</url>
                    <server>tomcat</server>
                </configuration>
            </plugin>
        </plugins>

        <!--配置Maven 对resource文件 过滤 -->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
            <resource>
                <directory>src/main/java</directory>
            </resource>
        </resources>
    </build>

    <properties>
        <!-- base setting -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.build.locales>zh_CN</project.build.locales>
        <project.build.jdk>1.7</project.build.jdk>

        <!-- plugin setting -->
        <mybatis.generator.generatorConfig.xml>${basedir}/src/test/resources/generatorConfig.xml</mybatis.generator.generatorConfig.xml>
        <mybatis.generator.generatorConfig.properties>file:///${basedir}/src/test/resources/generatorConfig.properties</mybatis.generator.generatorConfig.properties>

        <!-- plugin versions -->
        <plugin.mybatis.generator>1.3.1</plugin.mybatis.generator>
        <plugin.maven-compiler>3.1</plugin.maven-compiler>
        <plugin.maven-surefire>2.18.1</plugin.maven-surefire>
        <skipTests>true</skipTests>

        <!-- lib versions -->
        <junit.version>4.12</junit.version>
        <spring.version>4.3.8.RELEASE</spring.version>
        <mybatis.version>3.2.2</mybatis.version>
        <mybatis.spring.version>1.2.2</mybatis.spring.version>
        <mysql.connector.version>5.1.30</mysql.connector.version>
        <postgresql.version>9.1-901.jdbc4</postgresql.version>
        <slf4j.version>1.6.6</slf4j.version>
        <log4j.version>1.2.12</log4j.version>
        <httpclient.version>4.3.2</httpclient.version>
        <jackson.version>1.9.13</jackson.version>
        <c3p0.version>0.9.1.2</c3p0.version>
        <druid.version>1.0.5</druid.version>
        <tomcat.jdbc.version>7.0.53</tomcat.jdbc.version>
        <jstl.version>1.2</jstl.version>
        <google.collections.version>1.0</google.collections.version>
        <cglib.version>3.1</cglib.version>
        <shiro.version>1.2.3</shiro.version>
        <commons.fileupload.version>1.3.1</commons.fileupload.version>
        <commons.codec.version>1.9</commons.codec.version>
        <commons.net.version>3.3</commons.net.version>
        <aspectj.version>1.6.12</aspectj.version>
        <netty.version>4.0.18.Final</netty.version>
        <hibernate.validator.version>5.1.1.Final</hibernate.validator.version>
    </properties>

    <dependencies>
        <!-- junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
        </dependency>

        <!-- springframe start -->
        <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>
        

        <!-- springframe end -->

        <!-- mybatis start-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>${mybatis.version}</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>${mybatis.spring.version}</version>
        </dependency>
        <!--mybatis end-->

        <!-- mysql-connector -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.connector.version}</version>
        </dependency>

        <!-- DruidDataSource -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>${druid.version}</version>
        </dependency>

        <!-- jackson -->
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>${jackson.version}</version>
        </dependency>

        <!-- log start -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</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>
        <!-- log end -->

        <!-- servlet api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>

        <!-- jstl -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>${jstl.version}</version>
        </dependency>

        <!-- start apache -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>${commons.fileupload.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>${httpclient.version}</version>
        </dependency>

        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>${commons.codec.version}</version>
        </dependency>

        <dependency>
            <groupId>commons-net</groupId>
            <artifactId>commons-net</artifactId>
            <version>${commons.net.version}</version>
        </dependency>

        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.3</version>
        </dependency>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.2.1</version>
        </dependency>

        <!-- end apache -->

        <!-- google -->
        <dependency>
            <groupId>com.google.collections</groupId>
            <artifactId>google-collections</artifactId>
            <version>${google.collections.version}</version>
        </dependency>

        <!-- cglib -->
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib-nodep</artifactId>
            <version>${cglib.version}</version>
        </dependency>


        <!-- shiro -->
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-spring</artifactId>
            <version>${shiro.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-ehcache</artifactId>
            <version>${shiro.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-core</artifactId>
            <version>${shiro.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-web</artifactId>
            <version>${shiro.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-quartz</artifactId>
            <version>${shiro.version}</version>
        </dependency>

        <!-- aspectjweaver -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${aspectj.version}</version>
        </dependency>

        <!-- hibernate-validator -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>${hibernate.validator.version}</version>
        </dependency>

        <!-- netty -->
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>${netty.version}</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.2</version>
            <type>jar</type>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.6.2</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        
        <!-- controller的单元测试需要此库  -->
        <dependency>
		    <groupId>javax.el</groupId>
		    <artifactId>javax.el-api</artifactId>
		    <version>3.0.0</version>
		</dependency>
		
		<!-- 操作excel的库 -->
        <dependency>
		    <groupId>org.apache.poi</groupId>
		    <artifactId>poi</artifactId>
		    <version>3.15</version>
		</dependency>
		
		<!-- 读取各版本excel的兼容库 -->
		<dependency>
		    <groupId>org.apache.poi</groupId>
		    <artifactId>poi-ooxml</artifactId>
		    <version>3.15</version>
		</dependency>
		
		<!--rabbitmq依赖 -->
		<dependency>
			<groupId>org.springframework.amqp</groupId>
			<artifactId>spring-rabbit</artifactId>
			<version>1.7.1.RELEASE</version>
		</dependency>
		<!-- 如果没有这段,上面也会将其拉进来 -->
		<dependency>
			<groupId>org.springframework.amqp</groupId>
			<artifactId>spring-amqp</artifactId>
			<version>1.7.1.RELEASE</version>
		</dependency>
		<!-- 这个必要的 -->
		<dependency>
			<groupId>com.rabbitmq</groupId>
			<artifactId>amqp-client</artifactId>
			<version>3.6.6</version>
		</dependency>

    </dependencies>
    </project>


3.application.propertes配置文件

 

 

##本地数据库
jdbc.driver=com.mysql.jdbc.Driver
 #根据需要改为自己数据库
jdbc.url=jdbc:mysql://127.0.0.1:3306/ssm?useUnicode=true&characterEncoding=utf-8 
#根据需要改为自己数据库用户名
jdbc.username=root 
#改为自己数据库密码
jdbc.password=1234 ##DataSource Global Setting#配置初始化大小、最小、最大ds.initialSize=1ds.minIdle=1ds.maxActive=20#配置获取连接等待超时的时间 ds.maxWait=60000#配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒ds.timeBetweenEvictionRunsMillis=60000#配置一个连接在池中最小生存的时间,单位是毫秒ds.minEvictableIdleTimeMillis=300000


4. applicationContext.xml文件

 

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
       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:p="http://www.springframework.org/schema/p"
       xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xmlns:cache="http://www.springframework.org/schema/cache"
  	 xsi:schemaLocation="http://www.springframework.org/schema/beans  
        http://www.springframework.org/schema/beans/spring-beans.xsd  
        http://www.springframework.org/schema/util   
        http://www.springframework.org/schema/util/spring-util.xsd  
        http://www.springframework.org/schema/context  
        http://www.springframework.org/schema/context/spring-context.xsd  
        http://www.springframework.org/schema/aop   
        http://www.springframework.org/schema/aop/spring-aop.xsd  
        http://www.springframework.org/schema/tx  
        http://www.springframework.org/schema/tx/spring-tx.xsd">  


<!-- 自动扫描spring注解,排除springmvc已扫描的Controller注解 -->
<context:component-scan base-package="com.ln.web"> 
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> 
</context:component-scan> 

<!-- mybatis与spring整合 --> 
   <!-- 引入配置文件 -->
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath*:application.properties</value>
            </list>
        </property>	
    </bean>
  <!-- dataSource 配置 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <!-- 基本属性 url、user、password -->
        <property name="url" value="http://www.mamicode.com/${jdbc.url}"/>
        <property name="username" value="http://www.mamicode.com/${jdbc.username}"/>
        <property name="password" value="http://www.mamicode.com/${jdbc.password}"/>

        <!-- 配置初始化大小、最小、最大 -->
        <property name="initialSize" value="http://www.mamicode.com/${ds.initialSize}"/>
        <property name="minIdle" value="http://www.mamicode.com/${ds.minIdle}"/>
        <property name="maxActive" value="http://www.mamicode.com/${ds.maxActive}"/>

        <!-- 配置获取连接等待超时的时间 -->
        <property name="maxWait" value="http://www.mamicode.com/${ds.maxWait}"/>

        <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="http://www.mamicode.com/${ds.timeBetweenEvictionRunsMillis}"/>

        <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
        <property name="minEvictableIdleTimeMillis" value="http://www.mamicode.com/${ds.minEvictableIdleTimeMillis}"/>

        <property name="validationQuery" value="http://www.mamicode.com/SELECT ‘x‘"/>
        <property name="testWhileIdle" value="http://www.mamicode.com/true"/>
        <property name="testOnBorrow" value="http://www.mamicode.com/false"/>
        <property name="testOnReturn" value="http://www.mamicode.com/false"/>
    </bean>
<!-- 配置sqlSessionFactory --> 
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
    <property name="configLocation" value="http://www.mamicode.com/classpath:mybatis-config.xml" /> 
    <property name="dataSource" ref="dataSource" /> 
</bean> 
<!-- 扫描mapper接口   com.ln.web.dao 为自己项目dao的路径    --> 
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 
    <property name="basePackage" value="http://www.mamicode.com/com.ln.web.dao"></property> 
    <property name="sqlSessionFactoryBeanName" value="http://www.mamicode.com/sqlSessionFactory"></property> 
</bean> 
</beans>

5.springmvc.xml配置

 

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
       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:p="http://www.springframework.org/schema/p"
       xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xmlns:cache="http://www.springframework.org/schema/cache" xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans  
        http://www.springframework.org/schema/beans/spring-beans.xsd  
        http://www.springframework.org/schema/util   
        http://www.springframework.org/schema/util/spring-util.xsd  
        http://www.springframework.org/schema/context  
        http://www.springframework.org/schema/context/spring-context.xsd  
        http://www.springframework.org/schema/aop   
        http://www.springframework.org/schema/aop/spring-aop.xsd  
        http://www.springframework.org/schema/tx  
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        ">  
<!-- 自动扫描注解,只扫描的Controller注解,其它注解的扫描交给spring去处理 --> 
<context:component-scan base-package="com.ln.web.controller"/> 


 <mvc:annotation-driven></mvc:annotation-driven>
 
 
<!-- 配置springmvc的视图解析器 --> 
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"> 
</bean> 
</beans>


6.mybatis-config.xml文件

 

 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "classpath:schema/mybatis-3-config.dtd">
<configuration>

 <typeAliases>
        <package name="com.ln.web.model"/>
        <package name="com.ln.web.enums"/>
    </typeAliases>

</configuration>



7.编写测试类,测试数据库连接。

 

新建test

技术分享

TestBase接口定义,以后所有的测试类都实现此接口即可。

 

package com.ln.web.controller;

import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


@RunWith(SpringJUnit4ClassRunner.class)  
@ContextConfiguration(locations = { "classpath*:applicationContext.xml"})
public abstract class TestBase extends AbstractJUnit4SpringContextTests{
	

}

UserTes测试类。

 

 

package com.ln.web.controller;

import static org.junit.Assert.assertNotNull;

import java.sql.Connection;
import java.util.List;

import javax.annotation.Resource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import com.ln.web.model.User;
import com.ln.web.service.UserService;

public class UserTest extends TestBase {
	@Resource
	private UserService userService;
	@Autowired
	private SqlSessionFactory factory;

	/**
	 * 测试数据库连接
	 */
	@Test
	public void testConn() {
		Connection con = factory.openSession().getConnection();
		assertNotNull("数据库连接失败!!!", con);
	}

	/**
	 * 测试
	 */
	@Test
	public void listTest() {
		List<User> list = userService.getList();
		System.out.println(list.size());
	}

}


运行结果:

 

 

技术分享

 

有问题自己在检查配置。

至于listTest()测试,我新建了userService、userServiceImpl.这里就不贴代码了,就是一个简单调用。

user实体类、userMapper和userMapper.xml文件都是通过Mybatic自动生成的。在pom.xml文件中加入了这个插件。

只要在配置两个文件:generatorConfig.properties和generatorConfig.xml

 

generatorConfig.properties文件 :

链接的数据库,用户名、密码根据自己项目改下。

包路径配置根据自己项目的包路径配置下。

 

# 数据库驱动jar 路径
drive.class.path=C\:\\Users\\Administrator\\.m2\\repository\\mysql\\mysql-connector-java\\5.1.30\\mysql-connector-java-5.1.30.jar

# 数据库连接参数
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf-8
jdbc.username=simple
jdbc.password=1234

# 包路径配置
model.package=com.ln.web.model
dao.package=com.ln.web.dao
xml.mapper.package=com.ln.web.dao

target.project=src/main/java

 

 

generatorConfig.xml文件:

 需要生成的表名,根据自己项目改下就可以了:

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<!-- 配置Run As Maven build : Goals 参数 : mybatis-generator:generate -Dmybatis.generator.overwrite=true -->
<!-- 配置 tableName,使用 Run As Maven build 生成 dao model 层 -->
<generatorConfiguration>
    <!-- 配置文件路径 -->
    <properties url="${mybatis.generator.generatorConfig.properties}"/>

    <!--数据库驱动包路径 -->
    <classPathEntry location="${drive.class.path}"/>

    <context id="DB2Tables" targetRuntime="MyBatis3">
        <!--关闭注释 -->
        <commentGenerator>
            <property name="suppressAllComments" value="http://www.mamicode.com/true"/>
        </commentGenerator>

        <!--数据库连接信息 -->
        <jdbcConnection driverClass="${jdbc.driver}" connectionURL="${jdbc.url}" userId="${jdbc.username}"
                        password="${jdbc.password}">
        </jdbcConnection>

        <!--生成的model 包路径 -->
        <javaModelGenerator targetPackage="${model.package}" targetProject="${target.project}">
            <property name="enableSubPackages" value="http://www.mamicode.com/ture"/>
            <property name="trimStrings" value="http://www.mamicode.com/true"/>
        </javaModelGenerator>

        <!--生成xml mapper文件 路径 -->
        <sqlMapGenerator targetPackage="${xml.mapper.package}" targetProject="${target.project}">
            <property name="enableSubPackages" value="http://www.mamicode.com/ture"/>
        </sqlMapGenerator>

        <!-- 生成的Dao接口 的包路径 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="${dao.package}" targetProject="${target.project}">
            <property name="enableSubPackages" value="http://www.mamicode.com/ture"/>
        </javaClientGenerator>

        <!--对应数据库表名 -->
        <table tableName="user_table"></table> 
    </context>
</generatorConfiguration>

8.web.xml文件。

 

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">
	
	<!-- 加载spring容器 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value> classpath*:applicationContext.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>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<!-- 日志 -->
	<context-param>
		<param-name>log4jConfigLocation</param-name>
		<param-value>classpath:log4j.properties</param-value>
	</context-param>
	<context-param>
		<param-name>log4jRefreshInterval</param-name>
		<param-value>60000</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
	</listener>

	<!-- 配置springmvc的dispatchServlet的处理类 -->
	<servlet>
		<servlet-name>dispatcher</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath*:spring-mvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>dispatcher</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	<welcome-file-list>
		<welcome-file>/index.jsp</welcome-file>
	</welcome-file-list>
</web-app>


注:

 

我的项目创建的时候不是web项目,要把项目转成web项目生成web.xml文件:

可以参考:http://www.cnblogs.com/kaige123/p/5866446.html

 

9.测试、启动项目,访问:

index.jsp:

 

<%@ page contentType="text/html; charset=utf-8"  
    pageEncoding="utf-8"%>  
<!DOCTYPE html>  
<html>  
<head>  
<meta charset="utf-8">  
<title>用户登录界面</title>  
</head>  
<body>  
    <form action="user/login.action"  method="post" style="text-align: center; margin: 80px auto; width: 500px;">  
        <p>用户名:<input type="text" name="username" /></p>  
        <p>密码:<input type="password" name="password" /></p>  
        <input type="submit" value="http://www.mamicode.com/登录"/>  
    </form>  
      
</body>  
</html>  


userController:

 

 

package com.ln.web.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.ln.web.model.User;
import com.ln.web.service.UserService;

@Controller
@RequestMapping(value="http://www.mamicode.com/user")
public class UserController {
	@Resource	
	private UserService userService;
	@RequestMapping(value="http://www.mamicode.com/login")
	public ModelAndView  login(User user){
		ModelAndView mav=new ModelAndView();  
		mav.setViewName("success");
		return 	mav;
	}
}

 

运行结果:

技术分享

技术分享

 

 

 

 

 

 

 

 

 





 

 

 



 

 





(一)springmvc+spring+mybatis+maven框架搭建