先新建一个maven的项目
1.file->new->project->next
2.maven->勾选creat from archetype->webapp,同时选择java版本->next
3.输入GroupID,ArtifacID
(在网上看到的一些解释:groupId:定义了项目属于哪个组,一般来说这个通常和公司或组织关联,比如说,你所在的公司为wk.那就应该定义为com.wk.wktest,wktest为项目名称
artifactId:定 义了当前Maven项目在组中的唯一id,例如wktest这个项目,我则把他定义为test1,还例如spring项目,他的 groupId为org.springframework,对于content项目来说artifactId则为spring-content,web项 目则为spring-web)
点击next
4.选择idea自带的maven或者本地安装好的maven,next
6.默认,不需要改 next
7.点击finish,然后项目构建结束后,点击import changes
8.检测是否成功,控制台打印Build success 说明成功
搭建ssm环境。
1结构搭建
在src下新建test文件夹,和main同级,
选中test,右键-》mark directory as-》test sources root,意思是把test设置为测试类的根目录
在main下新建文件夹resource 和web,右键-》mark directory as
将resource设置为 resources root(资源文件根目录)
将web设置为sources root(文件根目录)
在resource下新建xml文件,用来写sql
在web下新建wk,在wk下新建controller,service,serviceImpl,dao文件夹,再分别建立对应的java文件
在resource下添加配置文件applicationContext.xml,spring-mvc.xml,spring-mybatis.xml,config.properties,log4j.properties
配置pom文件 ,在<properties>节点里添加
<spring.version>4.2.6.RELEASE</spring.version><!-- mybatis版本�?--><mybatis.version>3.4.0</mybatis.version><!-- log4j日志文件管理包版�?--><slf4j.version>1.7.7</slf4j.version><log4j.version>1.2.17</log4j.version><!--jackson版本�?--><jackson.version>2.7.1</jackson.version>
添加pom文件里的依赖添加到<dependencies>节点中间
<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency><!-- 添加jstl依赖 --><dependency><groupId>jstl</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency><!--阿里json包--><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.7</version></dependency><!--jdbc连接池--><dependency><groupId>commons-dbcp</groupId><artifactId>commons-dbcp</artifactId><version>1.4</version></dependency><dependency><groupId>commons-pool</groupId><artifactId>commons-pool</artifactId><version>1.6</version></dependency><!-- spring核心�?--><dependency><groupId>org.springframework</groupId><artifactId>spring-test</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-core</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-jms</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-tx</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>${spring.version}</version><type>jar</type><scope>compile</scope></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.7.4</version></dependency><dependency><groupId>aspectj</groupId><artifactId>aspectjrt</artifactId><version>1.5.4</version></dependency><dependency><groupId>org.codehaus.jackson</groupId><artifactId>jackson-mapper-asl</artifactId><version>1.9.11</version></dependency><!--mybatis核心�?--><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.4.1</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>1.3.0</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.21</version></dependency><dependency><groupId>com.oracle</groupId><artifactId>ojdbc14</artifactId><version>10.2.0.1.0</version></dependency><dependency><groupId>c3p0</groupId><artifactId>c3p0</artifactId><version>0.9.1.2</version><type>jar</type></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>1.7.5</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>1.7.5</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency><dependency><groupId>jstl</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency><!--json所需要的依赖--><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-core</artifactId><version>2.7.3</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.7.3</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-annotations</artifactId><version>2.7.3</version></dependency>
修改web.xml文件内容
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="/xml/ns/javaee" version="2.5"><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><context-param><param-name>log4jConfigLocation</param-name><param-value>classpath:log4j.properties</param-value></context-param><!-- 编码过滤器 --><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监听器 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 防止spring内存溢出监听器,比如quartz --><listener><listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class></listener><!-- spring mvc servlet--><servlet><servlet-name>SpringMVC</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>SpringMVC</servlet-name><!-- 此处也可以配置成 *.do 形式 --><url-pattern>/</url-pattern></servlet-mapping><welcome-file-list><welcome-file>/index.jsp</welcome-file></welcome-file-list><!-- session配置 --><session-config><session-timeout>30</session-timeout></session-config></web-app>
配置application.xml内容,注意,base-package的路径下所有的文件spring都会在启动的时候扫描一遍
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="/schema/beans"xmlns:xsi="/2001/XMLSchema-instance"xmlns:context="/schema/context"xsi:schemaLocation="/schema/beans /schema/beans/spring-beans-4.2.xsd/schema/context /schema/context/spring-context-4.2.xsd"><bean id="configurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><list><value>classpath:config.properties</value></list></property><property name="fileEncoding" value="UTF-8"></property></bean><!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 --><context:component-scan base-package="wk"><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan><import resource="spring-mybatis.xml"/></beans>
配置spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="/schema/beans"xmlns:xsi="/2001/XMLSchema-instance"xmlns:aop="/schema/aop"xmlns:mvc="/schema/mvc"xmlns:context="/schema/context"xsi:schemaLocation="/schema/beans /schema/beans/spring-beans-4.2.xsd/schema/context /schema/context/spring-context-4.2.xsd/schema/mvc /schema/mvc/spring-mvc-4.2.xsd/schema/aop /schema/aop/spring-aop-4.2.xsd"><mvc:annotation-driven><mvc:message-converters><bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/><bean class="org.springframework.http.converter.FormHttpMessageConverter"/><bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/><bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"/></mvc:message-converters></mvc:annotation-driven><mvc:default-servlet-handler/><!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 --><context:component-scan base-package="wk.controller"></context:component-scan><aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy><!-- 避免IE执行AJAX时,返回JSON出现下载文件 --><!-- 支持JSON数据格式 --><beanclass="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"><property name="messageConverters"><list><bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/><!-- 解析导出文件byte流 --><ref bean="fastJsonHttpMessageConverter"/><!--<ref bean="mappingJacksonHttpMessageConverter" />--></list></property></bean><!-- 使用fastJson来支持JSON数据格式 --><bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"><property name="supportedMediaTypes"><list><value>text/html;charset=UTF-8</value><value>application/json</value></list></property><property name="features"><list><value>WriteMapNullValue</value><value>QuoteFieldNames</value></list></property></bean><!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/"/><property name="suffix" value=".jsp"/><!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑 --></bean></beans>
配置spring-mybatis内容
<?xml version="1.0" encoding="UTF-8" ?><beans xmlns="/schema/beans"xmlns:xsi="/2001/XMLSchema-instance"xmlns:tx="/schema/tx"xsi:schemaLocation="/schema/beans/schema/beans/spring-beans-4.2.xsd/schema/tx/schema/tx/spring-tx.xsd"><!-- 连接池 --><bean id = "dataSource" class = "mons.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 name="maxActive" value="${jdbc.maxActive}"/><!-- 连接池最大空闲 --><property name="maxIdle" value="${jdbc.maxIdle}" /><!-- 连接池最小空闲 --><property name="minIdle" value="${jdbc.minIdle}" /><!-- 获取连接最大等待时间 --><property name="maxWait" value="${jdbc.maxWait}" /></bean><!-- 结合Spring和Mybatis --><bean id = "sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource" /><!-- 自动扫描mapping.xml文件 --><property name = "mapperLocations" value="classpath:mapper/*.xml" /></bean><!-- DAO接口所在包名,Spring会自动查找其下的类 --><bean class = "org.mybatis.spring.mapper.MapperScannerConfigurer"><property name = "basePackage" value="wk.dao" /><property name = "sqlSessionFactoryBeanName" value = "sqlSessionFactory" /></bean><!-- 定义事务 --><bean id = "transactionManager"class = "org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name = "dataSource" ref = "dataSource" /></bean><!-- 使用注解定义事务 --><tx:annotation-driven transaction-manager = "transactionManager" /></beans>
配置log4j.properties文件
### set log levels ###log4j.rootLogger = info , stdout , D , E### \u8F93\u51FA\u5230\u63A7\u5236\u53F0 ###log4j.appender.stdout = org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.Target = System.outlog4j.appender.stdout.layout = org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} %-5p [%t] %c %x - %m%n#log4j.appender.stdout.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{ 1 }:%L - %m%n### \u8F93\u51FA\u5230\u65E5\u5FD7\u6587\u4EF6 ###log4j.appender.D = org.apache.log4j.DailyRollingFileAppenderlog4j.appender.D.File = C://logs/ssm.loglog4j.appender.D.Append = truelog4j.appender.D.Threshold = DEBUG ## \u8F93\u51FADEBUG\u7EA7\u522B\u4EE5\u4E0A\u7684\u65E5\u5FD7log4j.appender.D.layout = org.apache.log4j.PatternLayoutlog4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n### \u4FDD\u5B58\u5F02\u5E38\u4FE1\u606F\u5230\u5355\u72EC\u6587\u4EF6 ###log4j.appender.E = org.apache.log4j.DailyRollingFileAppenderlog4j.appender.E.File = C://logs/error.log## \u5F02\u5E38\u65E5\u5FD7\u6587\u4EF6\u540Dlog4j.appender.E.Append = truelog4j.appender.E.Threshold = ERROR ## \u53EA\u8F93\u51FAERROR\u7EA7\u522B\u4EE5\u4E0A\u7684\u65E5\u5FD7!!!log4j.appender.E.layout = org.apache.log4j.PatternLayoutlog4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
配置config.properties文件
#JDBCjdbc.driver=oracle.jdbc.OracleDriverjdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:testjdbc.username=123jdbc.password="123"#\u5B9A\u4E49\u521D\u59CB\u8FDE\u63A5\u6570jdbc.initialSize=0#\u5B9A\u4E49\u6700\u5927\u8FDE\u63A5\u6570jdbc.maxActive=20#\u5B9A\u4E49\u6700\u5927\u7A7A\u95F2jdbc.maxIdle=20#\u5B9A\u4E49\u6700\u5C0F\u7A7A\u95F2jdbc.minIdle=1#\u5B9A\u4E49\u6700\u957F\u7B49\u5F85\u65F6\u95F4jdbc.maxWait=60000
找到自己写sql的xml,我的是在resource-》mapper-》test.xml
添加内容namespace绑定自己的dao的类,这个不用多说
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper PUBLIC "-////DTD Mapper 3.0//EN" "/dtd/mybatis-3-mapper.dtd" ><mapper namespace="wk.dao.TestDao"></mapper>
tomcat配置
屏幕上方点击尖头--》edit configurations
选择本地tomcat
OK,可以先跑起来,如果能正常运行并且弹出页面表示配置正确
给controller,service,serviceImpl,dao添加注解,方法,并且测试
ontroller
package wk.controller;import com.alibaba.fastjson.JSONObject;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;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 wk.service.TestService;/*** Created by infodba on /5/23.*/@Controller@RequestMapping("/index")public class TestController {private Logger logger = LoggerFactory.getLogger(this.getClass());@Autowired@Qualifier("testService")private TestService testService;/** 两个方法* 第一个不跳转页面,只会在页面显示出返回值* 第二个跳转页面* * */@RequestMapping(value = "/testAction", method = RequestMethod.GET)@ResponseBodypublic JSONObject indexTest(String data) {int i = testService.testAction();JSONObject jsonObject = new JSONObject();jsonObject.put("test", i);return jsonObject;}@RequestMapping(value = "/testActionDemo2", method = RequestMethod.GET)public String indexTest2() {return "home";}}
service
package wk.service;/*** Created by infodba on /5/23.*/public interface TestService {int testAction();}
serviceImpl
package wk.serviceImpl;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import wk.dao.TestDao;import wk.service.TestService;/*** Created by infodba on /5/23.*/@Service("testService")public class TestServiceImpl implements TestService {@Autowired(required = false)private TestDao testDao;@Overridepublic int testAction() {int i = testDao.selectCount();return i;}}
dao
package wk.dao;/*** Created by infodba on /5/23.*/public interface TestDao {int selectCount();}
test.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper PUBLIC "-////DTD Mapper 3.0//EN" "/dtd/mybatis-3-mapper.dtd" ><mapper namespace="wk.dao.TestDao"><select id="selectCount" resultType="Integer">SELECT "COUNT"(id) FROM T_PLAN</select></mapper>
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head><meta charset="utf-8"><meta name="renderer" content="webkit|ie-comp|ie-stand"><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"><meta name="viewport"content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/><meta http-equiv="Cache-Control" content="no-siteapp"/><title>111</title></head><body><h2>Hello World!</h2><h2><a href="/testmaven/index/testAction">点---abc---击</a></h2></body></html>
在webapp下新建home.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head><meta charset="utf-8"><meta name="renderer" content="webkit|ie-comp|ie-stand"><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"><meta name="viewport"content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/><meta http-equiv="Cache-Control" content="no-siteapp"/><title>222</title></head><body><h2>第二个页面</h2></body>
ok,完毕,在运行之前,将jdbc连接信息改为自己本地的数据库,同时test.xml里的sql也要改,改为自己的数据库的表
改完之后启动tomcat。