SSM(Spring+Springmvc+Mybatis)增删改查的创建及整合步骤

1、创建web工程

SSM(Spring+Springmvc+Mybatis)增删改查的创建及整合步骤_第1张图片

2、导入相关jar包

在WebContext/WEB-INF/lib中导入jar包

SSM(Spring+Springmvc+Mybatis)增删改查的创建及整合步骤_第2张图片SSM(Spring+Springmvc+Mybatis)增删改查的创建及整合步骤_第3张图片

3、创建User类

创建Getter和setter方法

public class User {
	
	private Integer userId;
	private String userName;
	private String userPwd;
	private Integer userAge;
	private String userHobby;
	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 getUserPwd() {
		return userPwd;
	}
	public void setUserPwd(String userPwd) {
		this.userPwd = userPwd;
	}
	public Integer getUserAge() {
		return userAge;
	}
	public void setUserAge(Integer userAge) {
		this.userAge = userAge;
	}
	public String getUserHobby() {
		return userHobby;
	}
	public void setUserHobby(String userHobby) {
		this.userHobby = userHobby;
	}

}

4、编写Mapper接口

public interface UserMapper {
	
	/**
	 * 注册用户
	 * @param user
	 * @return
	 */
	public int reg(User user);
	
	/**
	 * 删除用户
	 * @param user
	 * @return
	 */
	public int delUser(User user);
	
	/**
	 * 修改用户
	 * @param user
	 * @return
	 */
	public int updateUser(User user);
	
	/**
	 * 获取所有用户
	 * @return
	 */
	public List getAllUser();
	
	/**
	 * 根据id查询用户
	 * @param user
	 * @return
	 */
	public User getUserById(User user);
	
	/**
	 * 登录
	 * @param user
	 * @return
	 */
	public User login(User user);

}

5、编写Mapper.xml文件



  

    
     
         sql语句
     
     
    
     
        sql语句
     
     
    
     
     	sql语句
     
     
    
     
     
    
     
     
    
     
  

6、编写控制层Service层

public interface UserService {
	
	/**
	 * 增加用户
	 * @param user
	 * @return
	 */
	public int reg(User user);
	
	/**
	 * 删除用户
	 * @param user
	 * @return
	 */
	public int delUser(User user);
	
	/**
	 * 修改用户
	 * @param user
	 * @return
	 */
	public int updateUser(User user);
	
	/**
	 * 获取所有用户
	 * @return
	 */
	public List getAllUser();
	
	/**
	 * 根据id查询用户
	 * @param user
	 * @return
	 */
	public User getUserById(User user);
	
	/**
	 * 登录
	 * @param user
	 * @return
	 */
	public User login(User user);

}

7、编写Service的实现类ServiceImp,继承Service接口

@Autowired注解代表注入。

public class UserServiceImp implements UserService {

    //在实现类ServiceImp中注入Mapper接口
	@Autowired
	private UserMapper userMapper;
	
	public int reg(User user) {
		System.out.println("reg");
		return userMapper.reg(user);
	}

	public int delUser(User user) {
		System.out.println("delUser");
		return userMapper.delUser(user);
	}

	public int updateUser(User user) {
		System.out.println("updateUser");
		return userMapper.updateUser(user);
	}

	public List getAllUser() {
		System.out.println("getAllUser");
		return userMapper.getAllUser();
	}

	public User getUserById(User user) {
		System.out.println("getUserById");
		return userMapper.getUserById(user);
	}

	public User login(User user) {
		System.out.println("login");
		return userMapper.login(user);
	}

}

 

8、编写控制层Controller

1、@Controller注解用于指示该类是一个控制器

2、@RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。

@Controller
@RequestMapping("user")
public class UserController {
	
    //注入UserService
	@Autowired
	private UserService userServiceImp;
	
	/**
	 * 增加用户
	 * @param user
	 * @return
	 */
	@RequestMapping("reg")
	public ModelAndView reg(User user) {
        //调用增加方法
		int i = userServiceImp.reg(user);
        //创建ModelAndView对象
		ModelAndView mav = new ModelAndView();
		if(i==0) {
            //设置跳转页面名称,交由图形解析器
			mav.setViewName("reg");
			return mav;
		}
        //跳转登录页面
		mav.setViewName("forward:/Login.jsp");
		return mav;
	}
	
	/**
	 * 用户删除
	 * @param user
	 * @return
	 */
	@RequestMapping("delUser")
	public ModelAndView delUser(User user) {
        //调用删除方法
		userServiceImp.delUser(user);
        //创建ModelAndView对象
		ModelAndView mav = new ModelAndView();
        //跳转至查询所有用户方法
		mav.setViewName("forward:/user/getAllUser.action");
		return mav;
	}
	
	/**
	 * 用户更新
	 * @param user
	 * @return
	 */
	@RequestMapping("updateUser")
	public ModelAndView updateUser(User user) {
        //调用修改方法
		userServiceImp.updateUser(user);
        //创建ModelAndView对象
		ModelAndView mav = new ModelAndView();
        //跳转至查询所有用户方法
		mav.setViewName("forward:/user/getAllUser.action");
		return mav;
	}

	/**
	 * 获取所有用户
	 * @param user
	 * @return
	 */
	@RequestMapping("getAllUser")
	public ModelAndView getAllUser() {
        //创建ModelAndView对象
		ModelAndView mav = new ModelAndView();
        //调用查询所有用户方法,用List接住
		List allUser = userServiceImp.getAllUser();
        放入ModelAndView中,标识为list
		mav.addObject("list", allUser);
        
		mav.setViewName("showAll");
		return mav;
	}

	/**
	 * 根据id查询用户
	 * @param user
	 * @return
	 */
	@RequestMapping("getUserById")
	public ModelAndView getUserById(User user) {
        //调用查询所有用户方法
		User userss = userServiceImp.getUserById(user);
        //创建ModelAndView对象
		ModelAndView mav = new ModelAndView();
        放入ModelAndView中,标识为list
		mav.addObject("user", userss);
        //设置跳转页面名称,交由图形解析器
		mav.setViewName("updatePage");
		return mav;
	}

	/**
	 * 登录
	 * @param user
	 * @return
	 */
	@RequestMapping("login")
	public ModelAndView login(User user) {
		ModelAndView mav = new ModelAndView();
		User users = userServiceImp.login(user);
		if(users==null) {
			mav.addObject("ma","账户或密码错误" );
			mav.setViewName("Login");
		}
		mav.addObject("user", users);
		mav.setViewName("forward:/user/getAllUser.action");
		return mav;
	}

}

9、编写springmvc.xml



	
	

	
	
		
		
		
		
	

10、编写mybatis-config.xml文件




	
	
		
	

11、编写applicationContext.xml文件

1、applicationContext-service.xml



        
        
        
 

 

2、applicationContext-dao.xml



	
	
		
		
		
		
	

	
	
		
		
		
		
		
		
	

	
	
		
	

12、配置web.xml文件



	ssm01
	
		index.html
		index.htm
		index.jsp
		default.html
		default.htm
		default.jsp
	

    
	
		ssm1
		org.springframework.web.servlet.DispatcherServlet
	    
	    contextConfigLocation
	    classpath:spring/springmvc.xml
	    
	    1
	

	
		ssm1
        //访问方法时需加上后缀
		*.action
	
	
	
	
		org.springframework.web.context.ContextLoaderListener
	
	
	
		contextConfigLocation
		classpath*:spring/applicationContext-*.xml
	

13、增加用户jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here


用户名:
密码:
确认密码:
年龄:
兴趣:

14、获取所有用户jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>



Insert title here



		
userId userName userAge userHobby
${user.userId} ${user.userName} ${user.userAge} ${user.userHobby} 删除修改

15、修改用户jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here


	
用户名:
年龄:
爱好:

 

你可能感兴趣的:(SSM(Spring+Springmvc+Mybatis)增删改查的创建及整合步骤)