原生SSM框架整合(没有使用Maven)

经过这么多天的各种框架学习(摸鱼)之旅,终于开始整合啦~好激动好激动~~

首先,SSM框架的整合除了需要各大框架本身的jar包之外,

还需要一个mybatis-spring的jar包,不然无法完成整合操作

首先来看整个项目的结构:

原生SSM框架整合(没有使用Maven)_第1张图片

jsp文件夹:由于我们编写的jsp文件里面可能包含业务逻辑,因此为了保证安全性,将其放置在客户端无法访问的WEB-INF目录下

resource静态资源文件:由于静态资源一般都是些css,html或者JavaScript文件,客户端访问网站的时候回自动下载到客户机上,因此并没有安全性可言,所以我们不必将resource文件夹放置在WEB-INF目录下,只需将其放置在项目的根目录下即可


 

Web.xml配置文件




	
	
	
		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
		/*
	

Spring配置文件



	
	
 	
 	
    
	
	
	
	
		
		
		
		
	
	
	
	
		
		
		
		
		
		
	
	
	
	
	
		
	



SpringMVC配置文件



	
	
	
	
	
	
	
	
	
	
		
		
	



数据表脚本代码:

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;

实体类(即POJO)User:

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 + "]";
	}
}

UserMapper.xml配置文件:




	
	
	
		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}
	

    

UserMapper.java接口:

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);
}

 

注意:XXMapper.xml配置文件和XXMapper.java接口必须放置在同一包下,否则初始化Spring的时候可能会报错;


业务逻辑层

1.UserService接口:

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);
}

2.UserServiceImpl接口实现类:

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);
	}

	
	
}

Controller控制层

1.UserController控制器类

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 map) {
		
		List userList =  userService.getAllUsers();
		map.put("user", userList);
		return "user";
		
	}
	
	
}

user.jsp:

<%@ 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}


启动服务器并打开浏览器,在浏览器地址栏输入:

http://localhost:8080/Shop/user

得到以下结果:


至此,最基础的SSM框架搭建完成,后续学习了Maven可以再用Maven搭建一次SSM框架~~~

你可能感兴趣的:(SSM框架,SSM框架,框架整合)