SSM(Spring +SpringMVC + Mybatis)框架的搭建
最近通过学习别人博客发表的SSM搭建Demo,尝试去搭建一个简单的SSMDemo---实现的功能是对用户增删改查的操作
参考博客链接
1、User实体类
package com.ssm.ljn.user.model;
import java.io.Serializable;
/**
*
*
* 用户实体类>
*
* @author Luojiening
*/
public class User implements Serializable {
private int id;
private String username;
private int age;
private String sex;
public User() {
super();
}
public User(int id, String username, int age, String sex) {
super();
this.id = id;
this.username = username;
this.age = age;
this.sex = sex;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
2、dao层
package com.ssm.ljn.user.dao;
import java.util.List;
import org.springframework.stereotype.Repository;
import com.ssm.ljn.user.model.User;
@Repository
public interface IUserDao {
void saveUser(User user);
boolean updateUser(User user);
boolean deleteUser(int id);
User queryUserById(int id);
List queryAllUser();
}
3、实现Dao接口(主要是SQL语句)
insert into User(user_id,user_name,user_age,user_sex) value(#{id},#{username},#{age},#{sex})
update User set user_name=#{username},user_age=#{age},user_sex=#{sex} where user_id=#{id}
delete from User where user_id = #{id}
4、spring-mybatis配置文件
5、mybatis配置文件
6、springmvc配置文件
7、jdbc.properties
#jdbc.properties
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://127.0.0.1:3306/ssm?useUnicode=true&characterEncoding=utf8&useSSL=false
jdbc.username = root
jdbc.password = 123456
8、log4j.xml
### set log levels ###
log4j.rootLogger = INFO , console , debug , error
### console ###
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 = %-d{yyyy-MM-dd HH\:mm\:ss} [%p]-[%c] %m%n
### log file ###
log4j.appender.debug = org.apache.log4j.DailyRollingFileAppender
log4j.appender.debug.File = ../logs/springmvc-demo.log
log4j.appender.debug.Append = true
log4j.appender.debug.Threshold = INFO
log4j.appender.debug.layout = org.apache.log4j.PatternLayout
log4j.appender.debug.layout.ConversionPattern = %-d{yyyy-MM-dd HH\:mm\:ss} [%p]-[%c] %m%n
### exception ###
log4j.appender.error = org.apache.log4j.DailyRollingFileAppender
log4j.appender.error.File = ../logs/springmvc-demo_error.log
log4j.appender.error.Append = true
log4j.appender.error.Threshold = ERROR
log4j.appender.error.layout = org.apache.log4j.PatternLayout
log4j.appender.error.layout.ConversionPattern = %-d{yyyy-MM-dd HH\:mm\:ss} [%p]-[%c] %m%n
###log4j.error.key not found
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ISO8601} %l %c%n%p: %m%n
### druid sql ###
log4j.logger.druid.sql=warn,stdout
log4j.logger.druid.sql.DataSource=warn,stdout
log4j.logger.druid.sql.Connection=warn,stdout
log4j.logger.druid.sql.Statement=warn,stdout
log4j.logger.druid.sql.ResultSet=warn,stdout
8、web.xml
SSMDemo
contextConfigLocation
classpath:cfg/spring-mybatis.xml
org.springframework.web.context.ContextLoaderListener
dispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:cfg/springmvc.xml
1
dispatcherServlet
/
CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
utf-8
CharacterEncodingFilter
/*
index.jsp
10、service层及其实现类
package com.ssm.ljn.user.service;
import java.util.List;
import org.springframework.stereotype.Service;
import com.ssm.ljn.user.model.User;
public interface IUserService {
void saveUser(User user);
boolean updateUser(User user);
boolean deleteUser(int id);
User queryUserById(int id);
List queryAllUser();
}
package com.ssm.ljn.user.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ssm.ljn.user.dao.IUserDao;
import com.ssm.ljn.user.model.User;
import com.ssm.ljn.user.service.IUserService;
@Service("userService")
@Transactional
public class UserService implements IUserService{
@Autowired
private IUserDao iUserDao;
@Override
public void saveUser(User user) {
iUserDao.saveUser(user);
}
@Override
public boolean updateUser(User user) {
return iUserDao.updateUser(user);
}
@Override
public boolean deleteUser(int id) {
return iUserDao.deleteUser(id);
}
@Override
public User queryUserById(int id) {
return iUserDao.queryUserById(id);
}
@Override
public List queryAllUser() {
return iUserDao.queryAllUser();
}
}
11、建立controllor
package com.ssm.ljn.user.action;
import java.util.List;
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 org.springframework.web.bind.annotation.RequestParam;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.ljn.user.model.User;
import com.ssm.ljn.user.service.IUserService;;
@Controller
@RequestMapping("/user")
public class UserAction {
@Autowired
private IUserService iUserService;
/**
*跳转到添加用户界面
*/
@RequestMapping("toAddUser")
public String toAddUser(){
return "addUser";
}
/**
* 添加用户并重定向
* @param model
* @param user
* @return
*/
@RequestMapping("addUser")
public String addUser(Model model,User user){
if(user != null){
iUserService.saveUser(user);
}
return "redirect:/user/userInfo";
}
/**
* 修改用户
* @param model
* @param request
* @param user
* @return
*/
@RequestMapping("updateUser")
public String UpdateUser(Model model,User user){
if(iUserService.updateUser(user)){
user = iUserService.queryUserById(user.getId());
model.addAttribute("user", user);
return "redirect:/user/userInfo";
}
return "/error";
}
/**
* 查询所有用户
* @param request
* @param model
* @return
*/
@RequestMapping("getAllUser")
public String getAllUser(Model model){
List user = iUserService.queryAllUser();
model.addAttribute("userList",user);
return "allUser";
}
/**
* 查询单个用户
* @param id
* @param request
* @param model
* @return
*/
@RequestMapping("/getUser")
public String getUser(int id,Model model){
model.addAttribute("user", iUserService.queryUserById(id));
return "editUser";
}
/**
* 根据id删除用户
* @param id
* @param request
* @param response
*/
@RequestMapping("/delUser")
public String deleteUser(int id,Model model){
model.addAttribute("user", iUserService.deleteUser(id));
return "redirect:/user/userInfo";
}
@RequestMapping("userInfo")
public String getUsers(@RequestParam(value="pn",defaultValue="1")Integer pn,Model model){
//从第一条开始 每页查询五条数据
PageHelper.startPage(pn, 5);
List users = iUserService.queryAllUser();
//将用户信息放入PageInfo对象里
PageInfo page = new PageInfo(users,5);
model.addAttribute("pageInfo", page);
return "allUser";
}
}
index界面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
pageContext.setAttribute("path", request.getContextPath());
%>
首页
进入用户管理页
jsp页面主要有显示所有用户、新增用户以及修改用户三部分组成
allUser.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
pageContext.setAttribute("path", request.getContextPath());
%>
用户列表
editUser.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
编辑用户
添加用户
addUser.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
添加用户
添加用户