经过这么多天的各种框架学习(摸鱼)之旅,终于开始整合啦~好激动好激动~~
首先,SSM框架的整合除了需要各大框架本身的jar包之外,
首先来看整个项目的结构:
jsp文件夹:由于我们编写的jsp文件里面可能包含业务逻辑,因此为了保证安全性,将其放置在客户端无法访问的WEB-INF目录下
resource静态资源文件:由于静态资源一般都是些css,html或者JavaScript文件,客户端访问网站的时候回自动下载到客户机上,因此并没有安全性可言,所以我们不必将resource文件夹放置在WEB-INF目录下,只需将其放置在项目的根目录下即可
contextConfigLocation
classpath:com/config/Spring/Spring.xml
org.springframework.web.context.ContextLoaderListener
springMVC
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:com/config/SpringMVC/springMVC-servlet.xml
1
springMVC
/
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
characterEncodingFilter
/*
CREATE TABLE `s_user` (
`userid` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
`email` varchar(255) DEFAULT NULL,
`phone` varchar(255) DEFAULT NULL,
`status` varchar(255) NOT NULL DEFAULT '0',
`code` varchar(255) DEFAULT NULL,
PRIMARY KEY (`userid`)
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8;
package com.pojo;
public class User {
private Integer userid;
private String username;
private String password;
private String email;
private String phone;
private Integer status;
private String code;
public Integer getUserid() {
return userid;
}
public void setUserid(Integer userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
@Override
public String toString() {
return "User [userid=" + userid + ", username=" + username + ", password=" + password + ", email=" + email
+ ", phone=" + phone + ", status=" + status + ", code=" + code + "]";
}
}
INSERT INTO s_user(username,password,email,phone)
VALUES(#{username},#{password},#{email},#{phone})
UPDATE s_user SET username = #{username},
password = #{password},
email = #{email},
phone = #{phone}
WHERE
userid = #{userid}
DELETE FROM s_user WHERE userid = #{userid}
package com.mapper;
import com.pojo.User;
import java.util.*;
public interface UserMapper {
public List getAllUsers();
public User getUserById(int userid);
public boolean deleteUserById(int userid);
public boolean insertUser(User user);
public boolean updateUserById(User user);
}
package com.service;
import com.pojo.User;
import java.util.*;
public interface UserService {
public List getAllUsers();
public User getUserById(int userid);
public boolean deleteUserById(int userid);
public boolean insertUser(User user);
public boolean updateUserById(User user);
}
package com.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mapper.UserMapper;
import com.pojo.User;
import com.service.UserService;
//使用@Service注解标记该类,表明该类属于业务逻辑层,并由Spring IOC容器接手其生命周期
@Service
public class UserServiceImpl implements UserService {
//使用@Autowired注解标记该对象后,在之后使用该对象则会有IOC分配,无需手动new出来;
@Autowired
UserMapper userMapper;
@Override
public List getAllUsers() {
// TODO Auto-generated method stub
return userMapper.getAllUsers();
}
@Override
public User getUserById(int userid) {
// TODO Auto-generated method stub
return userMapper.getUserById(userid);
}
@Override
public boolean deleteUserById(int userid) {
// TODO Auto-generated method stub
return userMapper.deleteUserById(userid);
}
@Override
public boolean insertUser(User user) {
// TODO Auto-generated method stub
return userMapper.insertUser(user);
}
@Override
public boolean updateUserById(User user) {
// TODO Auto-generated method stub
return userMapper.updateUserById(user);
}
}
package com.controller;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
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.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.pojo.User;
import com.service.UserService;
import com.service.impl.UserServiceImpl;
//使用@Controller注解标注该类,表明此类为控制器类;
@Controller
public class UserController {
@Autowired
UserService userService;
@RequestMapping(value="/user")
public String user(Map
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
User
${requestScope.user}
得到以下结果: