创建User表(使用SQLyog)
创建Maven项目(Webapp)添加依赖
配置Web.xml
数据库的配置文件db.properties
Mybatis 配置文件
spring-dao层配置文件
spring-service配置文件
spring-web配置文件
创建User实体
创建UserDao
创建 UserService
创建UserController
简单的JSP
Dao层测试
CREATE TABLE `user` (
`id` int(12) NOT NULL AUTO_INCREMENT,
`username` varchar(32) DEFAULT NULL,
`birthday` date DEFAULT NULL,
`gender` varchar(2) DEFAULT NULL,
`address` varchar(128) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=25 DEFAULT CHARSET=utf8
4.0.0
com.springmvc
day03-springmvc
1.0-SNAPSHOT
war
day03-springmvc Maven Webapp
http://www.example.com
org.springframework
5.0.1.RELEASE
3.5.2
1.3.2
1.7.7
1.2.17
junit
junit
4.12
test
${spring.groupId}
spring-core
${spring.version}
${spring.groupId}
spring-beans
${spring.version}
${spring.groupId}
spring-web
${spring.version}
${spring.groupId}
spring-oxm
${spring.version}
${spring.groupId}
spring-tx
${spring.version}
${spring.groupId}
spring-jdbc
${spring.version}
${spring.groupId}
spring-webmvc
${spring.version}
${spring.groupId}
spring-aop
${spring.version}
${spring.groupId}
spring-context
${spring.version}
${spring.groupId}
spring-test
${spring.version}
org.mybatis
mybatis
${mybatis.version}
org.mybatis
mybatis-spring
${mybatis.spring.version}
mysql
mysql-connector-java
5.1.37
c3p0
c3p0
0.9.1.2
commons-dbcp
commons-dbcp
1.2.2
com.alibaba
druid
1.1.12
redis.clients
jedis
3.0.1
jstl
jstl
1.2
log4j
log4j
${log4j.version}
ch.qos.logback
logback-classic
1.2.3
org.codehaus.jackson
jackson-mapper-asl
1.9.13
commons-fileupload
commons-fileupload
1.3.3
commons-beanutils
commons-beanutils
1.9.3
commons-io
commons-io
2.4
commons-codec
commons-codec
1.9
com.fasterxml.jackson.core
jackson-databind
2.9.5
com.fasterxml.jackson.core
jackson-core
2.9.5
com.fasterxml.jackson.core
jackson-annotations
2.9.5
commons-collections
commons-collections
3.2
javax.servlet
javax.servlet-api
3.1.0
provided
javax.servlet.jsp
javax.servlet.jsp-api
2.3.1
provided
dom4j
dom4j
1.1
jaxen
jaxen
1.1.6
org.aspectj
aspectjrt
1.8.5
org.aspectj
aspectjweaver
1.8.5
net.coobird
thumbnailator
0.4.8
com.github.penggle
kaptcha
2.3.2
day03-springmvc
maven-clean-plugin
3.1.0
maven-resources-plugin
3.0.2
maven-compiler-plugin
3.8.0
maven-surefire-plugin
2.22.1
maven-war-plugin
3.2.2
maven-install-plugin
2.5.2
maven-deploy-plugin
2.8.2
在创建好Maven项目我们要在右边 刷新一下,让Maven管理文件夹,才可以创建
Archetype Created Web Application
spring-dispatcher
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring/spring-*.xml
1
spring-dispatcher
/
# 连接数据库的参数
db.driver=com.mysql.jdbc.Driver
db.username=root
db.password=123456
db.url=jdbc:mysql://localhost:3306/spring
这里我使用了三层架构这样的配置,在web.xml 中,就加载了三层配置文件
package com.kangna.domain;
import java.util.Date;
/********************************
* @Author: kangna
* @Date: 2019/10/10 18:30
* @Version: 1.0
* @Desc: User entity
********************************/
public class User {
private Integer id;
private String username;
private Date birthday;
private String gender;
private String address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", birthday=" + birthday +
", gender='" + gender + '\'' +
", address='" + address + '\'' +
'}';
}
}
package com.kangna.dao;
import com.kangna.domain.User;
import java.util.List;
public interface UserDao {
List findAll();
}
package com.kangna.service;
import com.kangna.dao.UserDao;
import com.kangna.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/********************************
* @Author: kangna
* @Date: 2019/10/10 19:39
* @Version: 1.0
* @Desc:
********************************/
@Service
public class UserService {
@Autowired
private UserDao userDao;
public List findAll() {
return userDao.findAll();
}
}
package com.kangna.web.controller;
import com.kangna.domain.User;
import com.kangna.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
/********************************
* @Author: kangna
* @Date: 2019/10/10 19:19
* @Version: 1.0
* @Desc:
********************************/
@Controller
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("list")
public String list(Model model){
List list = userService.findAll();
model.addAttribute("list", list);
return "list";
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--导入jstl标签库--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
用户列表jsp页面
用户列表:
用户Id
用户名称
用户地址
${user.id}
${user.username}
${user.address}
import com.kangna.domain.User;
import com.kangna.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.List;
/********************************
* @Author: kangna
* @Date: 2019/10/11 21:30
* @Version: 1.0
* @Desc:
********************************/
@RunWith(SpringJUnit4ClassRunner.class)
// 告诉junit Spring 配置文件的位置
@ContextConfiguration({ "classpath:spring/spring-dao.xml" , "classpath:spring/spring-service.xml"})
public class UserDaoTest {
@Autowired
private UserService userService;
@Test
public void testFindAll() {
System.out.print(userService.findAll());
}
}
启动Tomcat进行 Controller的测试
至此我们就搭建了一个简单的SSM框架,并且测试可以运行。