标签:ubi vrp raw avs r文件 dev ssm die asi
一、介绍
使用IDEA搭建maven web项目,整合框架Spring+SpringMVC+mybatis
项目结构图:
二、搭建
1、新建maven项目
2、创建项目结构(如上图)
3、配置pom.xml
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 <groupId>SpringMVC</groupId> 5 <artifactId>SpringMVC_Demo</artifactId> 6 <packaging>war</packaging> 7 <version>1.0-SNAPSHOT</version> 8 <name>SSMDemo Maven Webapp</name> 9 <url>http://maven.apache.org</url> 10 11 <properties> 12 <!-- spring版本号 --> 13 <!--<spring.version>3.2.4.RELEASE</spring.version>--> 14 <spring.version>4.0.2.RELEASE</spring.version> 15 16 <!-- mybatis版本号 --> 17 <mybatis.version>3.2.4</mybatis.version> 18 <!-- log4j日志文件管理包版本 --> 19 <slf4j.version>1.6.6</slf4j.version> 20 <log4j.version>1.2.12</log4j.version> 21 </properties> 22 23 <dependencies> 24 <!-- spring核心包 --> 25 <!-- springframe start --> 26 <dependency> 27 <groupId>org.springframework</groupId> 28 <artifactId>spring-core</artifactId> 29 <version>${spring.version}</version> 30 </dependency> 31 32 <dependency> 33 <groupId>org.springframework</groupId> 34 <artifactId>spring-web</artifactId> 35 <version>${spring.version}</version> 36 </dependency> 37 38 <dependency> 39 <groupId>org.springframework</groupId> 40 <artifactId>spring-oxm</artifactId> 41 <version>${spring.version}</version> 42 </dependency> 43 44 <dependency> 45 <groupId>org.springframework</groupId> 46 <artifactId>spring-tx</artifactId> 47 <version>${spring.version}</version> 48 </dependency> 49 50 <dependency> 51 <groupId>org.springframework</groupId> 52 <artifactId>spring-jdbc</artifactId> 53 <version>${spring.version}</version> 54 </dependency> 55 56 <dependency> 57 <groupId>org.springframework</groupId> 58 <artifactId>spring-webmvc</artifactId> 59 <version>${spring.version}</version> 60 </dependency> 61 62 <dependency> 63 <groupId>org.springframework</groupId> 64 <artifactId>spring-aop</artifactId> 65 <version>${spring.version}</version> 66 </dependency> 67 68 <dependency> 69 <groupId>org.springframework</groupId> 70 <artifactId>spring-context-support</artifactId> 71 <version>${spring.version}</version> 72 </dependency> 73 74 <dependency> 75 <groupId>org.springframework</groupId> 76 <artifactId>spring-aop</artifactId> 77 <version>${spring.version}</version> 78 </dependency> 79 80 <dependency> 81 <groupId>org.springframework</groupId> 82 <artifactId>spring-test</artifactId> 83 <version>${spring.version}</version> 84 </dependency> 85 <!-- springframe end --> 86 87 <!-- mybatis核心包 --> 88 <dependency> 89 <groupId>org.mybatis</groupId> 90 <artifactId>mybatis</artifactId> 91 <version>${mybatis.version}</version> 92 </dependency> 93 <!-- mybatis/spring包 --> 94 <dependency> 95 <groupId>org.mybatis</groupId> 96 <artifactId>mybatis-spring</artifactId> 97 <version>1.2.2</version> 98 </dependency> 99 <!-- mysql驱动包 --> 100 <dependency> 101 <groupId>mysql</groupId> 102 <artifactId>mysql-connector-java</artifactId> 103 <version>5.1.29</version> 104 </dependency> 105 <!-- junit测试包 --> 106 <dependency> 107 <groupId>junit</groupId> 108 <artifactId>junit</artifactId> 109 <version>4.11</version> 110 <scope>test</scope> 111 </dependency> 112 <!-- 阿里巴巴数据源包 --> 113 <dependency> 114 <groupId>com.alibaba</groupId> 115 <artifactId>druid</artifactId> 116 <version>1.0.2</version> 117 </dependency> 118 119 <!-- json数据 --> 120 <dependency> 121 <groupId>org.codehaus.jackson</groupId> 122 <artifactId>jackson-mapper-asl</artifactId> 123 <version>1.9.13</version> 124 </dependency> 125 126 <!-- 日志文件管理包 --> 127 <!-- log start --> 128 <dependency> 129 <groupId>log4j</groupId> 130 <artifactId>log4j</artifactId> 131 <version>${log4j.version}</version> 132 </dependency> 133 <dependency> 134 <groupId>org.slf4j</groupId> 135 <artifactId>slf4j-api</artifactId> 136 <version>${slf4j.version}</version> 137 </dependency> 138 <dependency> 139 <groupId>org.slf4j</groupId> 140 <artifactId>slf4j-log4j12</artifactId> 141 <version>${slf4j.version}</version> 142 </dependency> 143 <!-- log end --> 144 </dependencies> 145 <build> 146 <finalName>SSMDemo</finalName> 147 </build> 148 </project>
4、添加配置文件
4.1 jdbc.properties 配置数据库属性
1 jdbc_driverClassName=com.mysql.jdbc.Driver 2 jdbc_url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8 3 jdbc_username=your usename 4 jdbc_password=your password
4.2 jdbc.properties 日志属性文件
1 # DEBUG,INFO,WARN,ERROR,FATAL 2 LOG_LEVEL=INFO 3 4 log4j.rootLogger=${LOG_LEVEL},CONSOLE,FILE 5 6 log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender 7 log4j.appender.CONSOLE.Encoding=utf-8 8 log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout 9 #log4j.appender.CONSOLE.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss} %C{8}@(%F:%L):%m%n 10 log4j.appender.CONSOLE.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss} %C{1}@(%F:%L):%m%n 11 12 log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender 13 log4j.appender.FILE.File=${catalina.base}/logs/SSHDemo.log 14 log4j.appender.FILE.Encoding=utf-8 15 log4j.appender.FILE.DatePattern=‘.‘yyyy-MM-dd 16 log4j.appender.FILE.layout=org.apache.log4j.PatternLayout 17 #log4j.appender.FILE.layout=org.apache.log4j.HTMLLayout 18 log4j.appender.FILE.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH\:mm\:ss} %C{8}@(%F\:%L)\:%m%n
4.3 mybatis-config.xml mybatis配置文件
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 <!-- 命名空间 --> 7 <typeAliases> 8 <typeAlias alias="User" type="com.ssm.demo.web.model.User"/> 9 </typeAliases> 10 11 <!-- 映射map --> 12 <mappers> 13 </mappers> 14 </configuration>
4.4 spring.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 7 http://www.springframework.org/schema/context 8 http://www.springframework.org/schema/context/spring-context.xsd"> 9 10 <!-- 引入jdbc配置文件 --> 11 <context:property-placeholder location="classpath:conf/jdbc.properties"/> 12 13 <!-- 扫描文件(自动将servicec层注入) --> 14 <context:component-scan base-package="com.ssm.demo.web.service"/> 15 <bean id = "userServiceImpl" class="com.ssm.demo.web.service.impl.UserServiceImpl"/> 16 17 </beans>
4.5 spring-mvc.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:p="http://www.springframework.org/schema/p" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xmlns:mvc="http://www.springframework.org/schema/mvc" 7 xsi:schemaLocation=" 8 http://www.springframework.org/schema/beans 9 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 10 http://www.springframework.org/schema/context 11 http://www.springframework.org/schema/context/spring-context-3.2.xsd 12 http://www.springframework.org/schema/mvc 13 http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> 14 15 16 17 <!-- 扫描controller(controller层注入) --> 18 <context:component-scan base-package="com.ssm.demo.web.controller"/> 19 20 <!-- 避免IE在ajax请求时,返回json出现下载 --> 21 <bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> 22 <property name="supportedMediaTypes"> 23 <list> 24 <value>text/html;charset=UTF-8</value> 25 </list> 26 </property> 27 </bean> 28 29 <!-- 对模型视图添加前后缀 --> 30 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" 31 p:prefix="/WEB-INF/view/" p:suffix=".jsp"/> 32 33 <!--进行静态资源的访问--> 34 <mvc:resources mapping="/resources/**" location="/resources/" /> 35 </beans>
4.6 spring-mybatis.xml spring整合mybatis
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:p="http://www.springframework.org/schema/p" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xmlns:aop="http://www.springframework.org/schema/aop" 7 xmlns:tx="http://www.springframework.org/schema/tx" 8 xmlns:util="http://www.springframework.org/schema/util" 9 xsi:schemaLocation="http://www.springframework.org/schema/beans 10 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 11 http://www.springframework.org/schema/context 12 http://www.springframework.org/schema/context/spring-context-3.2.xsd 13 http://www.springframework.org/schema/tx 14 http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 15 http://www.springframework.org/schema/aop 16 http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 17 http://www.springframework.org/schema/util 18 http://www.springframework.org/schema/util/spring-util-3.2.xsd"> 19 20 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" 21 destroy-method="close" > 22 <property name="driverClassName"> 23 <value>${jdbc_driverClassName}</value> 24 </property> 25 <property name="url"> 26 <value>${jdbc_url}</value> 27 </property> 28 <property name="username"> 29 <value>${jdbc_username}</value> 30 </property> 31 <property name="password"> 32 <value>${jdbc_password}</value> 33 </property> 34 <!-- 连接池最大使用连接数 --> 35 <property name="maxActive"> 36 <value>20</value> 37 </property> 38 <!-- 初始化连接大小 --> 39 <property name="initialSize"> 40 <value>1</value> 41 </property> 42 <!-- 获取连接最大等待时间 --> 43 <property name="maxWait"> 44 <value>60000</value> 45 </property> 46 <!-- 连接池最大空闲 --> 47 <property name="maxIdle"> 48 <value>20</value> 49 </property> 50 <!-- 连接池最小空闲 --> 51 <property name="minIdle"> 52 <value>3</value> 53 </property> 54 <!-- 自动清除无用连接 --> 55 <property name="removeAbandoned"> 56 <value>true</value> 57 </property> 58 <!-- 清除无用连接的等待时间 --> 59 <property name="removeAbandonedTimeout"> 60 <value>180</value> 61 </property> 62 <!-- 连接属性 --> 63 <property name="connectionProperties"> 64 <value>clientEncoding=UTF-8</value> 65 </property> 66 </bean> 67 68 <!-- mybatis文件配置,扫描所有mapper文件 --> 69 <bean id="sqlSessionFactory" 70 class="org.mybatis.spring.SqlSessionFactoryBean" 71 p:dataSource-ref="dataSource" 72 p:configLocation="classpath:conf/mybatis-config.xml" 73 p:mapperLocations="classpath:mapper/*.xml"/><!-- configLocation为mybatis属性 mapperLocations为所有mapper--> 74 75 <!-- spring与mybatis整合配置,扫描所有dao --> 76 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" 77 p:basePackage="com.ssm.demo.web.dao" 78 p:sqlSessionFactoryBeanName="sqlSessionFactory"/> 79 80 <!-- 对数据源进行事务管理 --> 81 <bean id="transactionManager" 82 class="org.springframework.jdbc.datasource.DataSourceTransactionManager" 83 p:dataSource-ref="dataSource"/> 84 85 <!-- 添加注解驱动 --> 86 <tx:annotation-driven transaction-manager="transactionManager"/> 87 88 </beans>
5、配置web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 5 id="WebApp_ID" version="2.5"> 6 <display-name>Archetype Created Web Application</display-name> 7 8 <!-- 读取spring配置文件 --> 9 <context-param> 10 <param-name>contextConfigLocation</param-name> 11 <param-value>classpath:conf/spring.xml; 12 classpath:conf/spring-mybatis.xml 13 </param-value> 14 </context-param> 15 <!-- 设计路径变量值 --> 16 <context-param> 17 <param-name>webAppRootKey</param-name> 18 <param-value>springmvc.root</param-value> 19 </context-param> 20 21 22 <!-- Spring字符集过滤器 --> 23 <filter> 24 <filter-name>SpringEncodingFilter</filter-name> 25 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 26 <init-param> 27 <param-name>encoding</param-name> 28 <param-value>UTF-8</param-value> 29 </init-param> 30 <init-param> 31 <param-name>forceEncoding</param-name> 32 <param-value>true</param-value> 33 </init-param> 34 </filter> 35 <filter-mapping> 36 <filter-name>SpringEncodingFilter</filter-name> 37 <url-pattern>/*</url-pattern> 38 </filter-mapping> 39 40 <!-- 日志记录 --> 41 <context-param> 42 <!-- 日志配置文件路径 --> 43 <param-name>log4jConfigLocation</param-name> 44 <param-value>classpath:conf/log4j.properties</param-value> 45 </context-param> 46 <context-param> 47 <!-- 日志页面的刷新间隔 --> 48 <param-name>log4jRefreshInterval</param-name> 49 <param-value>6000</param-value> 50 </context-param> 51 <listener> 52 <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> 53 </listener> 54 55 <listener> 56 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 57 </listener> 58 59 <!-- springMVC核心配置 --> 60 <servlet> 61 <servlet-name>spring</servlet-name> 62 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 63 <init-param> 64 <param-name>contextConfigLocation</param-name> 65 <param-value>classpath:conf/spring-mvc.xml</param-value> 66 </init-param> 67 <load-on-startup>2</load-on-startup> 68 </servlet> 69 <servlet-mapping> 70 <servlet-name>spring</servlet-name> 71 <url-pattern>*.do</url-pattern> 72 </servlet-mapping> 73 74 <welcome-file-list> 75 <welcome-file>index.jsp</welcome-file> 76 </welcome-file-list> 77 78 <!-- 错误跳转页面 --> 79 <error-page> 80 <!-- 路径不正确 --> 81 <error-code>404</error-code> 82 <location>/WEB-INF/errorpage/404.jsp</location> 83 </error-page> 84 <error-page> 85 <!-- 没有访问权限,访问被禁止 --> 86 <error-code>405</error-code> 87 <location>/WEB-INF/errorpage/405.jsp</location> 88 </error-page> 89 <error-page> 90 <!-- 内部错误 --> 91 <error-code>500</error-code> 92 <location>/WEB-INF/errorpage/500.jsp</location> 93 </error-page> 94 </web-app>
好了,一个简单的web项目就搭建成功了!不多说,该有的注释都有。
省略实体层、sevice层、controller层、view层。
下面下个测试类试一下
6、测试
1 package com.ssm.demo.test.service; 2 3 import com.ssm.demo.web.model.User; 4 import org.junit.Before; 5 import org.junit.Test; 6 import org.springframework.context.ApplicationContext; 7 import org.springframework.context.support.ClassPathXmlApplicationContext; 8 import com.ssm.demo.web.service.UserService; 9 10 /** 11 * user:ouym 12 * date: 2015/7/31 13 * time: 11:55 14 */ 15 public class UserTest { 16 17 // @Autowired 18 private UserService userService; 19 20 @Before 21 public void before(){ 22 @SuppressWarnings("resource") 23 ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"classpath:conf/spring.xml" 24 ,"classpath:conf/spring-mybatis.xml"}); 25 userService = (UserService) context.getBean("userServiceImpl"); 26 } 27 28 @Test 29 public void addUser(){ 30 User user = new User(); 31 user.setUsername("ouym"); 32 user.setPassword("1234567"); 33 34 System.out.println(userService.insertUser(user)); 35 } 36 }
测试通过,大功告成!
这里有一个小问题,注意到上面注释掉的@Autowired了吗?单元测试直接@Autowired是无效的,运行会报空指针错误。
至于为什么,看如下代码自己思考吧!
修改后的代码:
1 package com.ssm.demo.test; 2 3 import com.ssm.demo.web.model.User; 4 import org.junit.Before; 5 import org.junit.Test; 6 import org.junit.runner.RunWith; 7 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.context.ApplicationContext; 9 import org.springframework.context.support.ClassPathXmlApplicationContext; 10 import com.ssm.demo.web.service.UserService; 11 import org.springframework.stereotype.Component; 12 import org.springframework.test.context.ContextConfiguration; 13 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 14 15 /** 16 * user:ouym 17 * date: 2015/7/31 18 * time: 11:55 19 */ 20 @RunWith(SpringJUnit4ClassRunner.class) 21 @ContextConfiguration(locations = {"classpath:conf/spring.xml","classpath:conf/spring-mybatis.xml"}) 22 public class UserTest { 23 24 @Autowired 25 private UserService userService; 26 27 /*@Before 28 public void before(){ 29 @SuppressWarnings("resource") 30 ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"classpath:conf/spring.xml" 31 ,"classpath:conf/spring-mybatis.xml"}); 32 userService = (UserService) context.getBean("userServiceImpl"); 33 }*/ 34 35 @Test 36 public void addUser(){ 37 User user = new User(); 38 user.setUsername("ouym"); 39 user.setPassword("1234567"); 40 41 System.out.println(userService.insertUser(user)); 42 } 43 }
自己动手试试,可以将shiro和ehcache整合到该项目!
IDEA中maven搭建Spring+SpringMVC+mybatis项目
标签:ubi vrp raw avs r文件 dev ssm die asi
原文地址:http://www.cnblogs.com/ouym/p/6106690.html