二、jar包导入
链接:https://pan.baidu.com/s/1cmem-MaO0bYeu3GlWDmLvw 密码:fbb9
开始编写程序
数据库的创建我就不写了 大家可以根据自己的喜好创建数据库
一、创建User实体类
package com.ssm.model;
public class User {
private int id;
private String username;
private String password;
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 String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password="
+ password + "]";
}
}
二、创建和实现mapper接口
UserMapper.xml
INSERT INTO Demo4(id,username,password) VALUES(#{id},#{username},#{password})
UPDATE Demo4 SET username=#{username},password=#{password} where id=#{id}
DELETE FROM Demo4 WHERE id=#{id}
UserMapper.java
package com.ssm.mapper;
import java.util.List;
import com.ssm.model.User;
public interface UserMapper {
void save(User user);
int update(User user);
boolean delete(int id);
User findById(int id);
List findAll();
}
三、mybatis和spring的整合
spring-common.xml
四、mybatis的配置文件
mybatis-config.xml
五、配置log4j.properties
log4j.rootLogger=DEBUG, stdout, R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# Pattern to output the caller's file name and line number.
#log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
# Print the date in ISO 8601 format
log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=example.log
log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
# Print only messages of level WARN or above in the package com.foo.
log4j.logger.com.foo=WARN
六、整合springmvc
spring-mvc.xml
七、web.xml的配置
ssm001
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:spring-common.xml,classpath:spring-mvc.xml
springMVC
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-common.xml,classpath:spring-mvc.xml
1
springMVC
/
org.springframework.web.util.IntrospectorCleanupListener
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
encodingFilter
/*
index.jsp
八、srevice的 创建和 实现
UserService.java
package com.ssm.service;
import java.util.List;
import com.ssm.model.User;
public interface UserService {
void save(User user);
int update(User user);
boolean delete(int id);
User findById(int id);
List findAll();
}
UserServiceImpl.java
package com.ssm.service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ssm.mapper.UserMapper;
import com.ssm.model.User;
import com.ssm.service.UserService;
@Service
@Transactional
public class UserServiceImpl implements UserService{
@Resource
private UserMapper usermapper;
@Override
public void save(User user) {
// TODO Auto-generated method stub
usermapper.save(user);
}
@Override
public boolean update(User user) {
// TODO Auto-generated method stub
return usermapper.update(user);
}
@Override
public boolean delete(int id) {
// TODO Auto-generated method stub
return usermapper.delete(id);
}
@Override
public User findById(int id) {
// TODO Auto-generated method stub
User user=usermapper.findById(id);
return user;
}
@Override
public List findAll() {
// TODO Auto-generated method stub
List findAllList=usermapper.findAll();
return findAllList;
}
}
九、控制层Controller
UserController.java
package com.ssm.controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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 com.ssm.model.User;
import com.ssm.service.UserService;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
/**
* 获取所有用户列表
* @param request
* @param model
* @return
*/
@RequestMapping("/getAllUser")
public String getAllUser(HttpServletRequest request,Model model){
List user=userService.findAll();
model.addAttribute("userList",user);
request.setAttribute("userList",user);
return "/allUser";
}
/**
* 跳转到添加用户界面
* @param request
* @return
*/
@RequestMapping("/toAddUser")
public String toAddUser()
{
return "/addUser";
}
/**
* 添加用户并重定向
* @param user
* @param request
* @return
*/
@RequestMapping("/addUser")
public String addUser(User user,Model model){
userService.save(user);
return "redirect:/user/getAllUser";
}
/**
*编辑用户
* @param user
* @param request
* @return
*/
@RequestMapping("/updateUser")
public String updateUser(User user,HttpServletRequest request,Model model){
if(userService.update(user)!=0)
{
user=userService.findById(user.getId());
request.setAttribute("user",user);
model.addAttribute("user",user);
return "redirect:/user/getAllUser";
}
else {
return "/error";
}
}
/**
* 根据id查询单个用户
* @param id
* @param request
* @return
*/
@RequestMapping("/getUser")
public String getUser(int id,HttpServletRequest request,Model model){
request.setAttribute("user",userService.findById(id));
model.addAttribute("user",userService.findById(id));
return "/editUser";
}
/**
* 删除用户
* @param id
* @param request
* @param response
*/
@RequestMapping("/delUser")
public void delUser(int id,HttpServletRequest request,HttpServletResponse response){
String result="{\"result\":\"error\"}";
if(userService.delete(id))
{
result="{\"result\":\"success\"}";
}else {
result="{\"result\":\"error\"}";
}
response.setContentType("application/json");
try {
PrintWriter out=response.getWriter();
out.write(result);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
十、jap页面
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
用户列表
进入用户管理页
ddUser.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'addUser.jsp' starting page
<%=path%>添加用户<%=basePath%>
allUser.jsp
<%@ page language="java" import="java.util.*" 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"%>
My JSP 'allUser.jsp' starting page
添加用户
id
账号
密码
操作
${user.id}
${user.username}
${user.password}
编辑
删除
editUser.jsp
s<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'editUser.jsp' starting page
修改用户
原文:https://www.cnblogs.com/wangyan12-9/p/8276173.html