Spring+SpringMVC+Mybatis框架搭建
项目建设完成之后的结构:
数据库的表结构如下:
环境建设:搭建Maven环境、Tomcat环境、需要MySql 数据库支持,使用的编程工具Eclipse (这些是前期准备);
开始创建工程:
1.创建一个Maven工程:
选择webapp
随便填写了:
创建完成之后工程会报错:
接下来怎么解决的?
选中项目点击右键,选择“Properties” 进入下面界面:
选中project facets ,然后将java版本改成 你的jdk版本,或者1.6以上版本。
之后选择 java build path
修改lib里面的java版本:
选中上面的jre System Library 然后选择 edit
保存之后再看看工程。上面的操作主要是让目录结构显示正常。(上面的不一定可以解决问题,工程还是会报错的)
在pom.xml文件中添加依赖:
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
这个立竿见影;不报错了。
之后再pom文件中添加依赖包,最终pom.xml文件如下:
<pre style="font-size: 13px; padding: 9.5px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; color: rgb(51, 51, 51); border-radius: 4px; margin-top: 0px; margin-bottom: 10px; line-height: 20px; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; background-color: rgb(245, 245, 245); border: 1px solid rgba(0, 0, 0, 0.14902);"><span style="color: rgb(232, 191, 106);"><project </span><span style="color: rgb(186, 186, 186);">xmlns</span><span style="color: rgb(106, 135, 89);">="http://maven.apache.org/POM/4.0.0" </span><span style="color: rgb(186, 186, 186);">xmlns:</span><span style="color: rgb(152, 118, 170);">xsi</span><span style="color: rgb(106, 135, 89);">="http://www.w3.org/2001/XMLSchema-instance"
</span><span style="color: rgb(106, 135, 89);"> </span><span style="color: rgb(152, 118, 170);">xsi</span><span style="color: rgb(186, 186, 186);">:schemaLocation</span><span style="color: rgb(106, 135, 89);">="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"</span><span style="color: rgb(232, 191, 106);">>
</span><span style="color: rgb(232, 191, 106);"> <modelVersion></span>4.0.0<span style="color: rgb(232, 191, 106);"></modelVersion>
</span><span style="color: rgb(232, 191, 106);"> <groupId></span>com.eliteams<span style="color: rgb(232, 191, 106);"></groupId>
</span><span style="color: rgb(232, 191, 106);"> <artifactId></span>quick4j<span style="color: rgb(232, 191, 106);"></artifactId>
</span><span style="color: rgb(232, 191, 106);"> <packaging></span>war<span style="color: rgb(232, 191, 106);"></packaging>
</span><span style="color: rgb(232, 191, 106);"> <version></span>1.0.0<span style="color: rgb(232, 191, 106);"></version>
</span><span style="color: rgb(232, 191, 106);"> <name></span>quick4j App<span style="color: rgb(232, 191, 106);"></name>
</span><span style="color: rgb(232, 191, 106);"> <url></span>https://github.com/starzou/quick4j<span style="color: rgb(232, 191, 106);"></url>
</span>
<span style="color: rgb(232, 191, 106);"> <build>
</span><span style="color: rgb(232, 191, 106);"> <finalName></span>quick4j<span style="color: rgb(232, 191, 106);"></finalName>
</span><span style="color: rgb(232, 191, 106);"> <plugins>
</span><span style="color: rgb(232, 191, 106);"> </span><span style="color: rgb(128, 128, 128);"><!-- Mybatis generator代码生成插件 配置 -->
</span><span style="color: rgb(128, 128, 128);"> </span><span style="color: rgb(232, 191, 106);"><plugin>
</span><span style="color: rgb(232, 191, 106);"> <groupId></span>org.mybatis.generator<span style="color: rgb(232, 191, 106);"></groupId>
</span><span style="color: rgb(232, 191, 106);"> <artifactId></span>mybatis-generator-maven-plugin<span style="color: rgb(232, 191, 106);"></artifactId>
</span><span style="color: rgb(232, 191, 106);"> <version></span>${plugin.mybatis.generator}<span style="color: rgb(232, 191, 106);"></version>
</span><span style="color: rgb(232, 191, 106);"> <configuration>
</span><span style="color: rgb(232, 191, 106);"> <configurationFile></span>${mybatis.generator.generatorConfig.xml}<span style="color: rgb(232, 191, 106);"></configurationFile>
</span><span style="color: rgb(232, 191, 106);"> <overwrite></span>true<span style="color: rgb(232, 191, 106);"></overwrite>
</span><span style="color: rgb(232, 191, 106);"> <verbose></span>true<span style="color: rgb(232, 191, 106);"></verbose>
</span><span style="color: rgb(232, 191, 106);"> </configuration>
</span><span style="color: rgb(232, 191, 106);"> </plugin>
</span>
<span style="color: rgb(232, 191, 106);"> </span><span style="color: rgb(128, 128, 128);"><!-- Maven插件 配置 -->
</span><span style="color: rgb(128, 128, 128);"> </span><span style="color: rgb(232, 191, 106);"><plugin>
</span><span style="color: rgb(232, 191, 106);"> <groupId></span>org.apache.maven.plugins<span style="color: rgb(232, 191, 106);"></groupId>
</span><span style="color: rgb(232, 191, 106);"> <artifactId></span>maven-compiler-plugin<span style="color: rgb(232, 191, 106);"></artifactId>
</span><span style="color: rgb(232, 191, 106);"> <version></span>${plugin.maven-compiler}<span style="color: rgb(232, 191, 106);"></version>
</span><span style="color: rgb(232, 191, 106);"> <configuration>
</span><span style="color: rgb(232, 191, 106);"> <source></span>${project.build.jdk}<span style="color: rgb(232, 191, 106);"></source>
</span><span style="color: rgb(232, 191, 106);"> <target></span>${project.build.jdk}<span style="color: rgb(232, 191, 106);"></target>
</span><span style="color: rgb(232, 191, 106);"> <encoding></span>${project.build.sourceEncoding}<span style="color: rgb(232, 191, 106);"></encoding>
</span><span style="color: rgb(232, 191, 106);"> </configuration>
</span><span style="color: rgb(232, 191, 106);"> </plugin>
</span>
<span style="color: rgb(232, 191, 106);"> <plugin>
</span><span style="color: rgb(232, 191, 106);"> <groupId></span>org.apache.maven.plugins<span style="color: rgb(232, 191, 106);"></groupId>
</span><span style="color: rgb(232, 191, 106);"> <artifactId></span>maven-surefire-plugin<span style="color: rgb(232, 191, 106);"></artifactId>
</span><span style="color: rgb(232, 191, 106);"> <version></span>${plugin.maven-surefire}<span style="color: rgb(232, 191, 106);"></version>
</span><span style="color: rgb(232, 191, 106);"> <configuration>
</span><span style="color: rgb(232, 191, 106);"> <skipTests></span>${skipTests}<span style="color: rgb(232, 191, 106);"></skipTests>
</span><span style="color: rgb(232, 191, 106);"> </configuration>
</span><span style="color: rgb(232, 191, 106);"> </plugin>
</span><span style="color: rgb(232, 191, 106);"> </plugins>
</span>
<span style="color: rgb(232, 191, 106);"> </span><span style="color: rgb(128, 128, 128);"><!--配置Maven 对resource文件 过滤 -->
</span><span style="color: rgb(128, 128, 128);"> </span><span style="color: rgb(232, 191, 106);"><resources>
</span><span style="color: rgb(232, 191, 106);"> <resource>
</span><span style="color: rgb(232, 191, 106);"> <directory></span>src/main/resources<span style="color: rgb(232, 191, 106);"></directory>
</span><span style="color: rgb(232, 191, 106);"> <includes>
</span><span style="color: rgb(232, 191, 106);"> <include></span>**/*.properties<span style="color: rgb(232, 191, 106);"></include>
</span><span style="color: rgb(232, 191, 106);"> <include></span>**/*.xml<span style="color: rgb(232, 191, 106);"></include>
</span><span style="color: rgb(232, 191, 106);"> </includes>
</span><span style="color: rgb(232, 191, 106);"> <</span><span style="background-color: rgb(52, 65, 52); color: rgb(232, 191, 106);">filtering</span><span style="color: rgb(232, 191, 106);">></span>true<span style="color: rgb(232, 191, 106);"></</span><span style="background-color: rgb(52, 65, 52); color: rgb(232, 191, 106);">filtering</span><span style="color: rgb(232, 191, 106);">>
</span><span style="color: rgb(232, 191, 106);"> </resource>
</span><span style="color: rgb(232, 191, 106);"> <resource>
</span><span style="color: rgb(232, 191, 106);"> <directory></span>src/main/java<span style="color: rgb(232, 191, 106);"></directory>
</span><span style="color: rgb(232, 191, 106);"> <includes>
</span><span style="color: rgb(232, 191, 106);"> <include></span>**/*.properties<span style="color: rgb(232, 191, 106);"></include>
</span><span style="color: rgb(232, 191, 106);"> <include></span>**/*.xml<span style="color: rgb(232, 191, 106);"></include>
</span><span style="color: rgb(232, 191, 106);"> </includes>
</span><span style="color: rgb(232, 191, 106);"> <</span><span style="background-color: rgb(52, 65, 52); color: rgb(232, 191, 106);">filtering</span><span style="color: rgb(232, 191, 106);">></span>true<span style="color: rgb(232, 191, 106);"></</span><span style="background-color: rgb(52, 65, 52); color: rgb(232, 191, 106);">filtering</span><span style="color: rgb(232, 191, 106);">>
</span><span style="color: rgb(232, 191, 106);"> </resource>
</span><span style="color: rgb(232, 191, 106);"> </resources>
</span><span style="color: rgb(232, 191, 106);"> </build>
</span>
<span style="color: rgb(232, 191, 106);"> <properties>
</span><span style="color: rgb(232, 191, 106);"> </span><span style="color: rgb(128, 128, 128);"><!-- base setting -->
</span><span style="color: rgb(128, 128, 128);"> </span><span style="color: rgb(232, 191, 106);"><project.build.sourceEncoding></span>UTF-8<span style="color: rgb(232, 191, 106);"></project.build.sourceEncoding>
</span><span style="color: rgb(232, 191, 106);"> <project.build.locales></span>zh_CN<span style="color: rgb(232, 191, 106);"></project.build.locales>
</span><span style="color: rgb(232, 191, 106);"> <project.build.jdk></span>1.7<span style="color: rgb(232, 191, 106);"></project.build.jdk>
</span>
<span style="color: rgb(232, 191, 106);"> </span><span style="color: rgb(128, 128, 128);"><!-- plugin setting -->
</span><span style="color: rgb(128, 128, 128);"> </span><span style="color: rgb(232, 191, 106);"><mybatis.generator.generatorConfig.xml></span>${basedir}/src/test/resources/generatorConfig.xml<span style="color: rgb(232, 191, 106);"></mybatis.generator.generatorConfig.xml>
</span><span style="color: rgb(232, 191, 106);"> <mybatis.generator.generatorConfig.properties></span>file:///${basedir}/src/test/resources/generatorConfig.properties<span style="color: rgb(232, 191, 106);"></mybatis.generator.generatorConfig.properties>
</span>
<span style="color: rgb(232, 191, 106);"> </span><span style="color: rgb(128, 128, 128);"><!-- plugin versions -->
</span><span style="color: rgb(128, 128, 128);"> </span><span style="color: rgb(232, 191, 106);"><plugin.mybatis.generator></span>1.3.1<span style="color: rgb(232, 191, 106);"></plugin.mybatis.generator>
</span><span style="color: rgb(232, 191, 106);"> <plugin.maven-compiler></span>3.1<span style="color: rgb(232, 191, 106);"></plugin.maven-compiler>
</span><span style="color: rgb(232, 191, 106);"> <plugin.maven-surefire></span>2.18.1<span style="color: rgb(232, 191, 106);"></plugin.maven-surefire>
</span><span style="color: rgb(232, 191, 106);"> <skipTests></span>true<span style="color: rgb(232, 191, 106);"></skipTests>
</span>
<span style="color: rgb(232, 191, 106);"> </span><span style="color: rgb(128, 128, 128);"><!-- lib versions -->
</span><span style="color: rgb(128, 128, 128);"> </span><span style="color: rgb(232, 191, 106);"><junit.version></span>4.11<span style="color: rgb(232, 191, 106);"></junit.version>
</span><span style="color: rgb(232, 191, 106);"> <spring.version></span>4.0.2.RELEASE<span style="color: rgb(232, 191, 106);"></spring.version>
</span><span style="color: rgb(232, 191, 106);"> <mybatis.version></span>3.2.2<span style="color: rgb(232, 191, 106);"></mybatis.version>
</span><span style="color: rgb(232, 191, 106);"> <mybatis.spring.version></span>1.2.2<span style="color: rgb(232, 191, 106);"></mybatis.spring.version>
</span><span style="color: rgb(232, 191, 106);"> <mysql.connector.version></span>5.1.30<span style="color: rgb(232, 191, 106);"></mysql.connector.version>
</span><span style="color: rgb(232, 191, 106);"> <postgresql.version></span>9.1-901.jdbc4<span style="color: rgb(232, 191, 106);"></postgresql.version>
</span><span style="color: rgb(232, 191, 106);"> <slf4j.version></span>1.6.6<span style="color: rgb(232, 191, 106);"></slf4j.version>
</span><span style="color: rgb(232, 191, 106);"> <log4j.version></span>1.2.12<span style="color: rgb(232, 191, 106);"></log4j.version>
</span><span style="color: rgb(232, 191, 106);"> <httpclient.version></span>4.1.2<span style="color: rgb(232, 191, 106);"></httpclient.version>
</span><span style="color: rgb(232, 191, 106);"> <jackson.version></span>1.9.13<span style="color: rgb(232, 191, 106);"></jackson.version>
</span><span style="color: rgb(232, 191, 106);"> <c3p0.version></span>0.9.1.2<span style="color: rgb(232, 191, 106);"></c3p0.version>
</span><span style="color: rgb(232, 191, 106);"> <druid.version></span>1.0.5<span style="color: rgb(232, 191, 106);"></druid.version>
</span><span style="color: rgb(232, 191, 106);"> <tomcat.jdbc.version></span>7.0.53<span style="color: rgb(232, 191, 106);"></tomcat.jdbc.version>
</span><span style="color: rgb(232, 191, 106);"> <jstl.version></span>1.2<span style="color: rgb(232, 191, 106);"></jstl.version>
</span><span style="color: rgb(232, 191, 106);"> <google.collections.version></span>1.0<span style="color: rgb(232, 191, 106);"></google.collections.version>
</span><span style="color: rgb(232, 191, 106);"> <cglib.version></span>3.1<span style="color: rgb(232, 191, 106);"></cglib.version>
</span><span style="color: rgb(232, 191, 106);"> <shiro.version></span>1.2.3<span style="color: rgb(232, 191, 106);"></shiro.version>
</span><span style="color: rgb(232, 191, 106);"> <commons.fileupload.version></span>1.3.1<span style="color: rgb(232, 191, 106);"></commons.fileupload.version>
</span><span style="color: rgb(232, 191, 106);"> <commons.codec.version></span>1.9<span style="color: rgb(232, 191, 106);"></commons.codec.version>
</span><span style="color: rgb(232, 191, 106);"> <commons.net.version></span>3.3<span style="color: rgb(232, 191, 106);"></commons.net.version>
</span><span style="color: rgb(232, 191, 106);"> <aspectj.version></span>1.6.12<span style="color: rgb(232, 191, 106);"></aspectj.version>
</span><span style="color: rgb(232, 191, 106);"> <netty.version></span>4.0.18.Final<span style="color: rgb(232, 191, 106);"></netty.version>
</span><span style="color: rgb(232, 191, 106);"> <hibernate.validator.version></span>5.1.1.Final<span style="color: rgb(232, 191, 106);"></hibernate.validator.version>
</span><span style="color: rgb(232, 191, 106);"> </properties></span>
<!-- 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>
之后再看lib里面是否都导入了jar包:
接着需要配置resource下面的四个配置文件,目录结构如下:
jdbc.properties 、log4j.properties、 spring-mvc.xml 、 spring-mybatis.xml 这些文件可能都需要自己创建。
需要自己创建一个数据库表testu,包括四个字段 id username password age
jdbc.properties 配置如下:
- jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/testu?useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=root
#\u5B9A\u4E49\u521D\u59CB\u8FDE\u63A5\u6570
jdbc.initialSize=0
#\u5B9A\u4E49\u6700\u5927\u8FDE\u63A5\u6570
jdbc.maxActive=20
#\u5B9A\u4E49\u6700\u5927\u7A7A\u95F2
jdbc.maxIdle=20
#\u5B9A\u4E49\u6700\u5C0F\u7A7A\u95F2
jdbc.minIdle=1
#\u5B9A\u4E49\u6700\u957F\u7B49\u5F85\u65F6\u95F4
jdbc.maxWait=60000
log4j.properties配置如下:
#定义LOG输出级别
log4j.rootLogger=INFO,Console,File
#定义日志输出目的地为控制台
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
#可以灵活地指定日志输出格式,下面一行是指定具体的格式
log4j.appender.Console.layout = org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n
#文件大小到达指定尺寸的时候产生一个新的文件
log4j.appender.File = org.apache.log4j.RollingFileAppender
#指定输出目录
log4j.appender.File.File = logs/ssm.log
#定义文件最大大小
log4j.appender.File.MaxFileSize = 10MB
# 输出所以日志,如果换成DEBUG表示输出DEBUG以上级别日志
log4j.appender.File.Threshold = ALL
log4j.appender.File.layout = org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n
spring-mvc.xml 配置如下:
<?xml version=”1.0” encoding=”UTF-8”?>
<context:component-scan base-package="com.cn.uuu.controller" />
<!--避免IE执行AJAX时,返回JSON出现下载文件 -->
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" /> <!-- JSON转换器 -->
</list>
</property>
</bean>
<!-- 定义跳转的文件的前后缀 ,视图模式配置-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 默认编码 -->
<property name="defaultEncoding" value="utf-8" />
<!-- 文件大小最大值 -->
<property name="maxUploadSize" value="10485760000" />
<!-- 内存中的最大值 -->
<property name="maxInMemorySize" value="40960" />
</bean>
</beans>
spring-mybatis.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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-4.0.xsd">
<!-- 自动扫描 -->
<context:component-scan base-package="com.cn.uuu" />
<!-- 引入配置文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties" />
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 初始化连接大小 -->
<property name="initialSize" value="${jdbc.initialSize}"></property>
<!-- 连接池最大数量 -->
<property name="maxActive" value="${jdbc.maxActive}"></property>
<!-- 连接池最大空闲 -->
<property name="maxIdle" value="${jdbc.maxIdle}"></property>
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${jdbc.minIdle}"></property>
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="${jdbc.maxWait}"></property>
</bean>
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:com/cn/uuu/mapping/*.xml"></property>
</bean>
<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.cn.uuu.IDao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
到这里配置文件基本配置完毕
接下来怎么创建一个简单的实例运行:
可以参考这篇文章 http://blog.csdn.net/cool\_easy/article/details/42778239 使用代码生成。生成
这三个包,还有几个文件需要自己创建。
代码结构如下
最后的代码详见:http://download.csdn.net/detail/naploen8/8381755
原文链接: http://blog.csdn.net/cool_easy/article/details/42963573
还没有评论,来说两句吧...