(在网上看到的一些解释:groupId:定义了项目属于哪个组,一般来说这个通常和公司或组织关联,比如说,你所在的公司为wk.那就应该定义为com.wk.wktest,wktest为项目名称
artifactId:定 义了当前Maven项目在组中的唯一id,例如wktest这个项目,我则把他定义为test1,还例如spring项目,他的 groupId为org.springframework,对于content项目来说artifactId则为spring-content,web项 目则为spring-web)
点击next
在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文件 ,在
4.2.6.RELEASE
3.4.0
1.7.7
1.2.17
2.7.1
添加pom文件里的依赖添加到
junit
junit
4.11
test
jstl
jstl
1.2
com.alibaba
fastjson
1.2.7
commons-dbcp
commons-dbcp
1.4
commons-pool
commons-pool
1.6
org.springframework
spring-test
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-core
${spring.version}
org.springframework
spring-aop
${spring.version}
org.springframework
spring-jms
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-context-support
${spring.version}
jar
compile
org.aspectj
aspectjweaver
1.7.4
aspectj
aspectjrt
1.5.4
org.codehaus.jackson
jackson-mapper-asl
1.9.11
org.mybatis
mybatis
3.4.1
org.mybatis
mybatis-spring
1.3.0
mysql
mysql-connector-java
5.1.21
com.oracle
ojdbc14
10.2.0.1.0
c3p0
c3p0
0.9.1.2
jar
org.slf4j
slf4j-api
1.7.5
org.slf4j
slf4j-log4j12
1.7.5
log4j
log4j
1.2.17
jstl
jstl
1.2
com.fasterxml.jackson.core
jackson-core
2.7.3
com.fasterxml.jackson.core
jackson-databind
2.7.3
com.fasterxml.jackson.core
jackson-annotations
2.7.3
修改web.xml文件内容
contextConfigLocation
classpath:applicationContext.xml
log4jConfigLocation
classpath:log4j.properties
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
encodingFilter
/*
org.springframework.web.context.ContextLoaderListener
org.springframework.web.util.IntrospectorCleanupListener
SpringMVC
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
1
SpringMVC
/
/index.jsp
30
配置application.xml内容,注意,base-package的路径下所有的文件spring都会在启动的时候扫描一遍
classpath:config.properties
配置spring-mvc.xml
text/html;charset=UTF-8
application/json
WriteMapNullValue
QuoteFieldNames
配置spring-mybatis内容
配置log4j.properties文件
### set log levels ###
log4j.rootLogger = info , stdout , D , E
### \u8F93\u51FA\u5230\u63A7\u5236\u53F0 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.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.DailyRollingFileAppender
log4j.appender.D.File = C://logs/ssm.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG
## \u8F93\u51FADEBUG\u7EA7\u522B\u4EE5\u4E0A\u7684\u65E5\u5FD7
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.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.DailyRollingFileAppender
log4j.appender.E.File = C://logs/error.log
## \u5F02\u5E38\u65E5\u5FD7\u6587\u4EF6\u540D
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR
## \u53EA\u8F93\u51FAERROR\u7EA7\u522B\u4EE5\u4E0A\u7684\u65E5\u5FD7!!!
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
配置config.properties文件
#JDBC
jdbc.driver=oracle.jdbc.OracleDriver
jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:test
jdbc.username=123
jdbc.password="123"
#\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
找到自己写sql的xml,我的是在resource-》mapper-》test.xml
添加内容namespace绑定自己的dao的类,这个不用多说
屏幕上方点击尖头--》edit configurations
选择本地tomcat
OK,可以先跑起来,如果能正常运行并且弹出页面表示配置正确
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 2018/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)
@ResponseBody
public 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 2018/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 2018/5/23.
*/
@Service("testService")
public class TestServiceImpl implements TestService {
@Autowired(required = false)
private TestDao testDao;
@Override
public int testAction() {
int i = testDao.selectCount();
return i;
}
}
dao
package wk.dao;
/**
* Created by infodba on 2018/5/23.
*/
public interface TestDao {
int selectCount();
}
test.xml
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
111
Hello World!
点---abc---击
在webapp下新建home.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
222
第二个页面