闲来无事复习了一下SSM搭建,给刚入坑的小白提供一个空白框架学习,话不多说 上代码 代码中有详细注解 介绍
一、创建SSM框架需要的配置文件 applicationContext.xml spring-mvc.xml spring-server.xml
applicationContext.xml 用于spring 集成 mybatis等
classpath:config.properties
spring-mvc.xml
text/html;charset=UTF-8
application/json
WriteMapNullValue
QuoteFieldNames
spring-server.xml
config.properties 数据库连接
#JDBC
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/sso?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 日志
### 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
因为用了maven 来管理 用到了 mybatis-generator generatorConfig.xml 如下
web.xml
Archetype Created Web Application
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
pom.xml 在pom文件中 集成了 tomcat7 和 generator
4.0.0
com.ssm.yannis
yannisssm
1.0-SNAPSHOT
war
yannis-ssm Maven Webapp
http://www.example.com
UTF-8
1.7
1.7
4.2.6.RELEASE
3.4.0
1.7.7
1.2.17
2.7.1
1.2
2.5
2.0
jstl
jstl
${jstl.version}
javax.servlet
servlet-api
${servlet-api.version}
provided
javax.servlet
jsp-api
${jsp-api.version}
provided
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
org.mybatis.generator
mybatis-generator-core
1.3.5
mysql
mysql-connector-java
5.1.30
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
src/main/resources
**/*.properties
**/*.xml
true
src/main/java
**/*.properties
**/*.xml
true
yannisssm
maven-clean-plugin
3.1.0
maven-resources-plugin
3.0.2
maven-compiler-plugin
3.8.1
maven-surefire-plugin
2.12.4
maven-war-plugin
2.2
maven-install-plugin
2.5.2
maven-deploy-plugin
2.8.2
org.apache.tomcat.maven
tomcat7-maven-plugin
2.2
8080
/${project.artifactId}
${project.build.sourceEncoding}
org.mybatis.generator
mybatis-generator-maven-plugin
1.3.2
src/main/resources/generatorConfig.xml
true
true
Generate MyBatis Artifacts
generate
指令
项目结构
用generator生成后台代码 利用注解加载配置文件 并测试
import com.yannis.dao.UserMapper;
import com.yannis.entity.User;
import com.yannis.server.UserServer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @Author: yannis
* @Date: 2020/5/29 16:20
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml","classpath:spring-mvc.xml","classpath:spring-server.xml"})
public class ssmTest {
@Autowired
UserMapper userMapper;
@Autowired
UserServer userServer;
@Test
public void test(){
User user = userServer.selectByPrimaryKey(2);
System.out.println(user.toString());
}
}