本文章仅作为个人笔记
项目搭建参考于博客:http://blog.csdn.net/zhshulin/article/details/37921705
-
创建空壳web项目:
于pom.xml添加项目依赖及插件:
jar包分为spring核心包/mybatis核心包/mybatis-spring包/javaee包/mysql连接包/dbcp包/JSTL包/日志包/JSON转换包/上传组件包
插件包分为Mybatis-generator插件/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"> <parent> <artifactId>SSSMByMavenModules</artifactId> <groupId>com.mayousheng.www</groupId> <version>1.0</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>web</artifactId> <packaging>war</packaging> <name>web Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <!-- JSTL标签类 --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>com.mayousheng.www</groupId> <artifactId>service</artifactId> <version>1.0</version> </dependency> </dependencies> <build> <finalName>web</finalName> <plugins> <plugin> <!--Mybatis-generator插件,用于自动生成Mapper和POJO--> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <!--配置文件的位置--> <configurationFile>src/main/resources/generator/generator-config.xml</configurationFile> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> <executions> <execution> <id>Generate MyBatis Artifacts</id> <goals> <goal>generate</goal> </goals> </execution> </executions> <dependencies> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.2</version> </dependency> </dependencies> </plugin> <!-- 配置Tomcat插件 --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <configuration> <!-- 指定端口 --> <port>8080</port> <!-- 请求路径 --> <path>/</path> </configuration> </plugin> </plugins> </build> </project>
-
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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <!-- 引入配置文件 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:properties/jdbc.properties"/> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> <!-- 初始化连接大小 --> <property name="initialSize" value="${initialSize}"/> <!-- 连接池最大数量 --> <property name="maxActive" value="${maxActive}"/> <!-- 连接池最大空闲 --> <property name="maxIdle" value="${maxIdle}"/> <!-- 连接池最小空闲 --> <property name="minIdle" value="${minIdle}"/> <!-- 获取连接最大等待时间 --> <property name="maxWait" value="${maxWait}"/> </bean> <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- 自动扫描mapping.xml文件 --> <property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"/> </bean> <!-- DAO接口所在包名,Spring会自动查找其下的类 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.mayousheng.www.mapper"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> </beans>
-
spring-mvc.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:annotation-config/> <!-- 开启mvc的注解驱动 --> <mvc:annotation-driven/> <!-- 自动扫描controller包下的所有类,使其认为spring mvc的控制器 --> <context:component-scan base-package="com.mayousheng.www"/> <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/> <!--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> </beans>
-
jdbc.properties文件内容如下:
driver=com.mysql.jdbc.Driver url=jdbc:mysql://127.0.0.1:3306/test username=root password=root #定义初始连接数 initialSize=1 #定义最大连接数 maxActive=2000 #定义最大空闲 maxIdle=200 #定义最小空闲 minIdle=20 #定义最长等待时间 maxWait=60000 * log4j.properties文件内容如下: 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-ddHH\:mm\:ss}][%c]%m%n * generator.properties配置文件如下: jdbc.driverLocation=C:\\Users\\marking\\.m2\\repository\\mysql\\mysql-connector-java\\5.1.30\\mysql-connector-java-5.1.30.jar jdbc.driverClass=com.mysql.jdbc.Driver jdbc.connectionURL=jdbc:mysql://127.0.0.1:3306/test jdbc.userId=root jdbc.password=root
-
generator-config.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"> <generatorConfiguration> <!--数据库驱动包路径 --> <classPathEntry location="${driverLocation}"/> <context id="MySQLTables" targetRuntime="MyBatis3"> <!--关闭注释 --> <commentGenerator> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--数据库连接信息 --> <jdbcConnection driverClass="${driverClass}" connectionURL="${connectionURL}" userId="${userId}" password="${password}"> </jdbcConnection> <!-- 生成model路径 --> <javaModelGenerator targetPackage="${modelPackage}" targetProject="${src_main_java}"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- 生成mapper路径 --> <sqlMapGenerator targetPackage="${sqlMapperPackage}" targetProject="${src_main_resources}"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!-- 生成的Dao接口 的包路径 --> <javaClientGenerator type="XMLMAPPER" targetPackage="${mapperPackage}" targetProject="${src_main_java}"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!-- sql占位符,表示所有的表 --> <table tableName="%" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> <generatedKey column="epa_id" sqlStatement="Mysql" identity="true"/> </table> </context> </generatorConfiguration>
-
配置文件结构如图:
配置各文件:
配置web.xml添加spring-mybatis.xml配置文件包含,配置编码过滤防止乱码,配置Spring监听,配置防止Spring内存溢出监听,配置SpringMVC配置文件包含,配置项目拦截路径,配置根访问。
具体内容可于源码查看
运行及技巧:
从git下载好代码导入后需根据个人mysql情况更改jdbc.properties,另外如果想直接跑这个项目需要运行sql目录下的test.sql文件以创建数据库及生成测试数据,跑起来后于浏览器访问http://localhost:8080/user/showUser?id=1即可验证是否完成整个项目的初始化
-
idea下Tomcat配置运行:
-
generator生成代码:
-
直接下载并打开git: