SSM框架实现项目的增删改查

本文详细介绍了如何使用SSM框架进行项目的增删改查操作,包括程序的基本框架包结构,如UserInfo、UserController、IUserDao、IUserService等关键组件的设定,以及相关配置文件如ApplicationContext、db.properties等的配置。通过这些内容,展示了完整的SSM项目实现过程。
摘要由CSDN通过智能技术生成

SSM框架实现项目的增删改查

一、基本的程序框架包结构

上篇博客有查询部分,所以这次博客接着完成之后的
包结构如下:
在这里插入图片描述

二、相关代码

UserInfo.java,将user封装起来

package com.zhongruan.bean;

public class UserInfo {
    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 "UserInfo{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

UserController.java,控制器,接受请求并处理

package com.zhongruan.controller;

import com.zhongruan.bean.UserInfo;
import com.zhongruan.service.IUserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PatchMapping;
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 IUserService userService;

    @RequestMapping("/findAll.do")
    public ModelAndView findAll()
    {
        List<UserInfo> all =userService.findAll();
        ModelAndView mv=new ModelAndView();
        mv.addObject("userinfos",all);
        mv.setViewName("allUser");
        return mv;
    }
@RequestMapping("/delete.do")
    public ModelAndView delete(int id){
        userService.delete(id);
        ModelAndView mv=new ModelAndView();
       mv= findAll();
       return mv;
    }
    @RequestMapping("/toAddUser.do")
    public  ModelAndView addUser(){
        ModelAndView mv=new ModelAndView();
        mv.setViewName("addUser");
        return mv;
    }
    @RequestMapping("/save.do")
    public ModelAndView insert(@Param("userinfo")UserInfo userinfo){
        userService.insert(userinfo);
        List<UserInfo> all =userService.findAll();
        ModelAndView mv=new ModelAndView();
        mv.addObject("userinfos",all);
        mv.setViewName("allUser");
        return mv;
    }

    @RequestMapping("/toUpdate.do")
    public  ModelAndView toupdate(@Param("id")int id)
    {
        UserInfo userinfo=new UserInfo();
        userinfo=userService.find(id);
        ModelAndView mv=new ModelAndView();
        mv.addObject("userInfo",userinfo);
        mv.setViewName("updateUser");
        return mv;
    }
    @RequestMapping("/update.do")
    public ModelAndView update(@Param("userinfo")UserInfo userinfo)
    {
        userService.update(userinfo);
        ModelAndView mv=new ModelAndView();
        mv= findAll();
        return mv;
    }
    @RequestMapping("/login.do")
    public String login(String username,String password){
        UserInfo userInfo=userService.login(username);
        if(userInfo !=null){
            if(userInfo.getPassword().equals(password)){
                System.out.println("登陆成功");
                return "redirect:/success.jsp";
            }else {
                System.out.println("密码错误");
                return "redirect:/fail.jsp";
            }
        }else {
            System.out.println("用户名不存在");
            return "redirect:/fail.jsp";
        }
    }
}

IUserDao.java ,封装数据库操作

package com.zhongruan.dao;

import com.zhongruan.bean.UserInfo;

import javax.xml.registry.infomodel.User;
import java.util.List;

public interface IUserDao {
    List<UserInfo> findAll();
    void insert(UserInfo userinfo);
    void delete(int id);
    void update(UserInfo userinfo);
    UserInfo find(int id);
    UserInfo login(String username);
}

IUserService ,service类

package com.zhongruan.service;

import com.zhongruan.bean.UserInfo;

import java.util.List;

public interface IUserService {
    public List<UserInfo> findAll();
    void insert(UserInfo userinfo);
    void delete(int id);
    UserInfo find(int id);
    void update(UserInfo userinfo);
   UserInfo login(String username);
}

UserService.java ,实现Service类接口

package com.zhongruan.service.impl;

import com.zhongruan.bean.UserInfo;
import com.zhongruan.dao.IUserDao;
import com.zhongruan.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService implements IUserService {
   @Autowired
   private IUserDao userDao;

    @Override
    public List<UserInfo> findAll() {
        return userDao.findAll();
    }

    @Override
    public void insert(UserInfo userinfo) {
        userDao.insert(userinfo);
    }
    @Override
    public void delete(int id) {
         userDao.delete(id);
    }
    @Override
    public  UserInfo find(int id)
    {
       return  userDao.find(id);
    }

    @Override
    public void update(UserInfo userinfo) {
        userDao.update(userinfo);
    }

    @Override
    public UserInfo login(String username) {
        return userDao.login(username);
    }
}``

UserMapper.xml,实现Dao

<?xml version="1.0" encoding="UTF-8" ?>
<select id="findAll" resultType="com.zhongruan.bean.UserInfo">
select * from userinfo
<insert id="insert" parameterType="com.zhongruan.bean.UserInfo">
    insert into userinfo values (#{id},#{username},#{password})
</insert>

<delete id="delete" parameterType="com.zhongruan.bean.UserInfo">
    delete from userinfo
    where id = #{id}
</delete>

<select id="find" parameterType="int" resultType="com.zhongruan.bean.UserInfo">
    select * from userinfo where id=#{id}
</select>

<update id="update" parameterType="com.zhongruan.bean.UserInfo" >
    update userinfo set username=#{username},password=#{password} where id=#{id}
</update>

<select id="login" parameterType="string" resultType="com.zhongruan.bean.UserInfo">
select * from userinfo where username=#{username}
```

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-4.2.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
    <!-- 1.配置数据库相关参数properties的属性:${url} -->
    <context:property-placeholder location="classpath:db.properties"/>

    <!-- 2.配置数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="maxPoolSize" value="30"/>
        <property name="minPoolSize" value="2"/>
    </bean>

    <!-- 3.配置SqlSessionFactory对象 -->

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 注入数据库连接池 -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 扫描bean包 使用别名 -->
        <property name="typeAliasesPackage" value="com.zhongruan.bean"></property>

        <!--配置加载映射文件 UserMapper.xml-->
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>

    </bean>

    <!-- 自动生成dao,m
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值