可参考源码:Spring4 SpringMVC4 Mybatis3整合源码
访问我的个人博客 秋码个人博客
首先打开Mysql的图形界面工具进行建库建表,当然也可以使用cmd黑窗口命令形式进行建库建表。
create database kaoshi;
use kaoshi;
create table k_dept(
id int(11) auto_increment primary key,
name varchar(30),
age int(11),
birdate date);
INSERT INTO `k_dept` VALUES ('1', '张三', '56', '2017-08-07');
INSERT INTO `k_dept` VALUES ('2', '李四', '47', '2017-09-04');
INSERT INTO `k_dept` VALUES ('3', '王五', '36', '2017-09-05');
打开IDEA,创建Maven项目。
pom.xml 文件如下。
4.0.0
com.zhenqi.kaoshi
kaoshi-ssm
war
1.0-SNAPSHOT
kaoshi-ssm Maven Webapp
http://maven.apache.org
UTF-8
4.3.8.RELEASE
3.2.8
org.springframework
spring-core
${spring.version}
org.springframework
spring-context
${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.aspectj
aspectjrt
1.8.6
org.aspectj
aspectjweaver
1.8.6
org.mybatis
mybatis
${mybatis.version}
org.mybatis
mybatis-spring
1.2.3
mysql
mysql-connector-java
5.1.38
com.mchange
c3p0
0.9.5.1
jstl
jstl
1.2
log4j
log4j
1.2.17
org.slf4j
slf4j-log4j12
1.7.21
javax.servlet
javax.servlet-api
3.1.0
provided
javax.servlet.jsp
jsp-api
2.1
provided
junit
junit
4.12
test
kaoshi-ssm
先把配置文件整理好。 db.properties。位置在resources目录下。
driver=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/kaoshi?useUnicode=true&characterEncoding=UTF-8
user=root
password=root
idleConnectionTestPeriod=60
maxIdleTime=240
initialPoolSize=100
acquireIncrement=5
maxPoolSize=500
minPoolSize=10
日志记录配置文件,log4j.properties。
# DEBUG,INFO,WARN,ERROR,FATAL
LOG_LEVEL=INFO
#C-console, I-INFO, E-ERROR
log4j.rootLogger=${LOG_LEVEL},C,I,E
log4j.appender.C=org.apache.log4j.ConsoleAppender
log4j.appender.C.Threshold = INFO
log4j.appender.C.Encoding=utf-8
log4j.appender.C.layout=org.apache.log4j.PatternLayout
log4j.appender.C.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss} (%F:%L):%m%n
log4j.appender.I=org.apache.log4j.DailyRollingFileAppender
log4j.appender.I.File=${catalina.base}/logs/eidserver/info/log_daily
#log4j.appender.I.File=c://eidserver/info/log_daily
log4j.appender.I.Threshold = INFO
log4j.appender.I.Append = true
log4j.appender.I.Encoding=utf-8
log4j.appender.I.DatePattern='_'yyyy-MM-dd'.log'
log4j.appender.I.layout=org.apache.log4j.PatternLayout
log4j.appender.I.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH\:mm\:ss} (%F\:%L)\:%m%n
log4j.appender.I.MaxFileSize = 50MB
log4j.appender.I.MaxBackupIndex = 7
log4j.appender.E=org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File=${catalina.base}/logs/eidserver/error/log_error
#log4j.appender.E.File=c://eidserver/error/log_error
log4j.appender.E.Append = TRUE
log4j.appender.E.Threshold = ERROR
log4j.appender.E.Encoding=utf-8
log4j.appender.E.DatePattern='_'yyyy-MM-dd'.log'
log4j.appender.E.layout=org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH\:mm\:ss} (%F\:%L)\:%m%n
log4j.appender.E.MaxFileSize = 50MB
spring-mvc.xml 。位置在resources目录下。
而后是 spring-mybatis.xml。位置同上。
classpath*:com/zhenqi/dao/*.xml
然后在 web.xml 文件配置。
Archetype Created Web Application
index.jsp
contextConfigLocation
classpath:spring-mybatis.xml
org.springframework.web.context.ContextLoaderListener
springmvc-dispatcherservlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
1
springmvc-dispatcherservlet
/
配置文件整理好了,接下来就可以编写Java代码了。
1、Entity实体对象 Dept.java
package com.zhenqi.entity;
import java.util.Date;
/**
* Created by wuming on 2017/9/8.
*/
public class Dept {
private Integer id;
private String name;
private Integer age;
private Date birDate;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getBirDate() {
return birDate;
}
public void setBirDate(Date birDate) {
this.birDate = birDate;
}
}
2、Dao层
而后是Dao接口与Mapper xml文件。
DeptDao.java
package com.zhenqi.dao;
import com.zhenqi.entity.Dept;
import java.util.List;
/**
* Created by wuming on 2017/9/8.
*/
public interface DeptDao {
//查找所有内容
public List findAll();
}
Dept.xml 其位置 com/zhenqi/dao目录下。
3.Service层,也就是处理业务相关的。
DeptService.java接口文件如下:
package com.zhenqi.service;
import com.zhenqi.entity.Dept;
import java.util.List;
/**
* Created by wuming on 2017/9/8.
*/
public interface DeptService {
//查找所有内容
public List findall();
}
实现类 DeptServiceImpl.java 如下:
package com.zhenqi.service.impl;
import com.zhenqi.dao.DeptDao;
import com.zhenqi.entity.Dept;
import com.zhenqi.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* Created by wuming on 2017/9/8.
*/
@Service("deptService")
public class DeptServiceImpl implements DeptService {
//注入dao对象
@Autowired
private DeptDao deptDao;
public List findall() {
return deptDao.findAll();
}
}
4、Controller层,也就是所谓的控制层。
DeptController.java文件如下:
package com.zhenqi.controller;
import com.zhenqi.entity.Dept;
import com.zhenqi.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
/**
* Created by wuming on 2017/9/8.
*/
@Controller
@RequestMapping("/dept")
public class DeptController {
@Autowired
private DeptService deptService;
@RequestMapping("/find")
public String find(ModelMap model, HttpServletRequest request){
List deptList=deptService.findall();
//将获取到的数据存入ModelMap中,以便页面取出
model.addAttribute("depts", deptList);
return "dept";
}
}
5、view层,就是jsp或html。
dept.jsp文件如下,其位置在 WEB-INF/views/ 目录下。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page isELIgnored="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
Dept
ID
姓名
年龄
生日
启动Tomcat容器,如果不知怎么在IDEA中部署maven项目到Tomcat上的朋友,可参考文章:IDEA运行Maven项目部署tomcat上
访问浏览器,http://localhost:8080/kaoshi-ssm/dept/find
至此,maven搭建ssm框架就先告一段落了。
可参考源码:Spring4 SpringMVC4 Mybatis3整合源码