Github地址
https://github.com/menglanyingfei/SSM-Demo
使用IDEA和Maven创建Java Web项目
下面演示直接截图:
创建Maven项目时, 这里需要加入一个参数, 否则后面会创建失败:
Properties:
archetypeCatalog=internal
创建成功:
然后配置Tomcat服务器, 注意创建的时候选择本地的.
运行成功:
Github与IntelliJ IDEA整合
参考: http://www.cnblogs.com/MrJun/p/3351478.html
配置Maven依赖
在pom.xml中加入:
4.0.0
com.wtu
SSM-Demo
war
1.0-SNAPSHOT
SSM-Demo Maven Webapp
http://maven.apache.org
4.0.2.RELEASE
3.2.6
1.7.7
1.2.17
junit
junit
4.11
test
org.springframework
spring-core
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-oxm
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-aop
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-test
${spring.version}
aspectj
aspectjweaver
1.5.4
org.mybatis
mybatis
${mybatis.version}
org.mybatis
mybatis-spring
1.2.2
javax
javaee-api
7.0
mysql
mysql-connector-java
5.1.30
com.mchange
c3p0
0.9.5-pre8
com.mchange
mchange-commons-java
0.2.12
jstl
jstl
1.2
log4j
log4j
${log4j.version}
com.alibaba
fastjson
1.1.41
org.slf4j
slf4j-api
${slf4j.version}
org.slf4j
slf4j-log4j12
${slf4j.version}
org.codehaus.jackson
jackson-mapper-asl
1.9.13
commons-fileupload
commons-fileupload
1.3.1
commons-io
commons-io
2.4
commons-codec
commons-codec
1.9
SSM-Demo
Spring各模块配置
首先配置Mybatis的全局配置文件 sqlMapConfig.xml:
spring-mapper.xml:
数据库连接配置的属性文件 jdbc.properties:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql:///ssm-demo
user=root
password=lxy
Spring配置service层
spring-service.xml
然后配置log4j:
直接在类路径下添加log4j.properties:
#定义LOG输出级别
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-Demo.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-dd HH\:mm\:ss}][%c]%m%n
建表的SQL语句
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_name` varchar(40) NOT NULL,
`password` varchar(255) NOT NULL,
`age` int(4) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
/* Data for the table `user` */
insert into `user`(`id`,`user_name`,`password`,`age`) values (1,'小阳','666',21);
SSM框架——使用MyBatis Generator自动创建代码
参考: http://blog.csdn.net/zhshulin/article/details/23912615
若没有积分, 下载地址:
https://github.com/menglanyingfei/SSM-Demo/tree/master/MyBatis%20Generator%E8%87%AA%E5%8A%A8%E5%88%9B%E5%BB%BA%E4%BB%A3%E7%A0%81%E5%B7%A5%E5%85%B7
最后配置springmvc.xml和web.xml
springmvc.xml:
web.xml:
Archetype Created Web Application
contextConfigLocation
classpath:spring/spring-*.xml
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
true
encoding
UTF-8
encodingFilter
/*
org.springframework.web.context.ContextLoaderListener
org.springframework.web.util.IntrospectorCleanupListener
SpringMVC
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring/springmvc.xml
1
true
SpringMVC
/
/index.jsp
Controller中的方法:
/**
* User类的控制器, 视图显示
*
* menglanyingfei
* on 2017.11.02 17:34
*/
@Controller
@RequestMapping("/user")
public class UserController {
@Resource
private UserService userService;
@RequestMapping("/showUser")
public String toIndex(HttpServletRequest request, Model model) {
// 应该这样输入/user/showUser?id=1
int userId = Integer.parseInt(request.getParameter("id"));
User user = userService.getUserById(userId);
model.addAttribute("user", user);
return "/showUser";
// return "forward:/jsp/showUser.jsp";
}
@RequestMapping("/showUser1")
public ModelAndView home(HttpServletRequest request, Model model) {
// 应该这样输入/user/showUser1?id=1
int userId = Integer.parseInt(request.getParameter("id"));
User user = userService.getUserById(userId);
model.addAttribute("user", user);
ModelAndView mav = new ModelAndView();
mav.setViewName("/showUser");
return mav;
}
}
然后在浏览器输入
http://localhost:8080/项目名/user/showUser1?id=1
最终结果: