ssm项目实例——增删查改
1.ssm简介
SSM(Spring+SpringMVC+MyBatis)框架集由Spring、MyBatis两个开源框架整合而成(SpringMVC是Spring中的部分内容)。常作为数据源较简单的web项目的框架。
- Spring
Spring就像是整个项目中装配bean的大工厂,在配置文件中可以指定使用特定的参数去调用实体类的构造方法来实例化对象。。
Spring的核心思想是IoC(控制反转),即不再需要程序员去显式地new
一个对象,而是让Spring框架帮你来完成这一切。 - pringMVC
SpringMVC在项目中拦截用户请求,它的核心Servlet即DispatcherServlet承担中介或是前台这样的职责,将用户请求通过HandlerMapping去匹配Controller,Controller就是具体对应请求所执行的操作。 - mybatis
mybatis是对jdbc的封装,它让数据库底层操作变的透明。mybatis的操作都是围绕一个sqlSessionFactory实例展开的。mybatis通过配置文件关联到各实体类的Mapper文件,Mapper文件中配置了每个类对数据库所需进行的sql语句映射。在每次与数据库交互时,通过sqlSessionFactory拿到一个sqlSession,再执行sql命令。
页面发送请求给控制器,控制器调用业务层处理逻辑,逻辑层向持久层发送请求,持久层与数据库交互,后将结果返回给业务层,业务层将处理逻辑发送给控制器,控制器再调用视图展现数据。
2.创建maven项目
3.项目结构
导入webapp配置文件与资源文件
4.编码实现
bean.User
package com.zhongruan.bean;
public class User {
private int id;
private String username;
private String password;
public User(){
}
public User(String username,String password){
this.username = username;
this.password = password;
}
public User(int id, String username, String password){
this.id = id;
this.username = username;
this.password = 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 + '\'' +
'}';
}
}
controller.UserController
package com.zhongruan.controller;
import com.zhongruan.bean.User;
import com.zhongruan.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/login.do")
public ModelAndView login(User user){
int id = userService.login(user.getUsername(),user.getPassword());
ModelAndView mv = new ModelAndView();
if (id!=-1){
mv.setViewName("main");
}else {
mv.setViewName("../failer");
}
return mv;
}
@RequestMapping("/findAll.do")
public ModelAndView findAll(){
List<User> users = userService.findAll();
ModelAndView mv = new ModelAndView();
mv.addObject("users",users);
mv.setViewName("user-list");
return mv;
}
@RequestMapping("/add.do")
public String add(User user){
userService.add(user);
return "redirect:findAll.do";
}
@RequestMapping("/deleteById.do")
public String delete(int id){
userService.deleteById(id);
return "redirect:findAll.do";
}
@RequestMapping("/toUpdate.do")
public ModelAndView toUpdate(int id){
User user = userService.selectUserById(id);
ModelAndView mv = new ModelAndView();
mv.setViewName("user-update");
mv.addObject("user",user);
return mv;
}
@RequestMapping("/update.do")
public String update(User user){
userService.update(user);
return "redirect:findAll.do";
}
}
dao.UserDao
package com.zhongruan.dao;
import com.zhongruan.bean.User;
import java.util.List;
public interface UserDao {
User findByUserName(String username);
List<User> findAll();
void add(User user);
void deleteById(int id);
User selectById(int id);
void update(User user);
}
service.impl.UserServiceImpl
package com.zhongruan.service.impl;
import com.zhongruan.bean.User;
import com.zhongruan.dao.UserDao;
import com.zhongruan.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public int login(String username, String password) {
User user = userDao.findByUserName(username);
if(user!=null&&user.getPassword().equals(password)){
return user.getId();
}
return -1;
}
@Override
public List<User> findAll() {
return userDao.findAll();
}
@Override
public void add(User user) {
userDao.add(user);
}
@Override
public void deleteById(int id){
userDao.deleteById(id);
}
@Override
public User selectUserById(int id) {
return userDao.selectById(id);
}
@Override
public void update(User user) {
userDao.update(user);
}
}
service.UserService
package com.zhongruan.service;
import com.zhongruan.bean.User;
import java.util.List;
public interface UserService {
int login(String username,String password);
List<User> findAll();
void add(User user);
void deleteById(int id);
User selectUserById(int id);
void update(User user);
}
资源文件
mapper.UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.zhongruan.dao.UserDao" >
<select id="findByUserName" parameterType="String" resultType="user">
select * from tb_user where username=#{username}
</select>
<select id="findAll" resultType="user">
select * from tb_user
</select>
<insert id="add" parameterType="user">
insert into tb_user (username,password) value (#{username},#{password})
</insert>
<delete id="deleteById" parameterType="int">
delete from tb_user where id = #{id}
</delete>
<select id="selectById" parameterType="int" resultType="user">
select * from tb_user where id=#{id}
</select>
<update id="update" parameterType="user">
update tb_user set username=#{username},password=#{password} where id=#{id}
</update>
</mapper>
5.功能测试
- 登录功能
登录成功
- 用户管理
- 新增用户
- 更新用户
- 删除用户