JavaEE-SSM整合

简介: JavaEE-SSM整合

请求状态提示


根据 状态码 判断

“StatusCode”: 100,

表示数据不为空

“StatusDescription”: “请求成功。”,

提示“请求成功。”

  "DataStatus": {
    "RequestParameter": "keywords=烽火&pagesize=10&pagenumber=1&type=&searchtype=all",
    "StatusCode": 100,
    "StatusDescription": "请求成功。",
    "ResponseDateTime": "2021-06-10 09:25:05",
    "DataTotalCount": 244
  },


Update


根据id更改


Add


id自增,不用写

返回的“1”为改变的行数,新增了一行即改变了一行。


Delete


根据id删除


FindAll


查询全部


FindById


根据id查询


CustomerDao.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.dao.CustomerDao">
    <!--根据id查询-->
    <select id="findCustomerById" parameterType="Integer" resultType="Customer">
        select * from t_customer where id =#{id}
    </select>
<!--    查询所有用户-->
    <select id="findAllCustomer" resultType="Customer">
        select * from t_customer
    </select>
<!--    删-->
    <delete id="deleteCustomer" parameterType="Integer">
        delete from t_customer where id=#{id}
    </delete>
<!--    增-->
    <insert id="addCustomer" parameterType="Customer">
        insert into t_customer(username,jobs,phone) values (#{username},#{jobs},#{phone})
    </insert>
<!--    改-->
    <update id="updateCustomer" parameterType="Customer">
        update t_customer
        <set>
            <if test="username !=null and username !=''">
                username =#{username},
            </if>
            <if test="jobs !=null and jobs !=''">
                jobs =#{jobs},
            </if>
            <if test="phone !=null and phone !=''">
                phone =#{phone},
            </if>
        </set>
        where id=#{id}
    </update>
</mapper>

还有个CustomerDao.java接口与CustomerService.java差不多


CustomerService


package com.service;
import com.po.Customer;

import java.util.List;

public interface CustomerService {
    public Customer findCustomerById(Integer id);
    public List<Customer> findAllCustomer();
    public int deleteCustomer(Integer id);
    public int addCustomer(Customer customer);
    public int updateCustomer(Customer customer);
}


CustomerServiceImpl


package com.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.dao.CustomerDao;
import com.po.Customer;
import com.service.CustomerService;

import java.util.List;

@Service
//注解@Transactional作用:对数据的增、修、改进行事务管理
@Transactional
public class CustomerServiceImpl implements CustomerService {
    //注解注入CustomerDao
    @Autowired
    private CustomerDao customerDao;
    //查询客户
    public Customer findCustomerById(Integer id) {
        return this.customerDao.findCustomerById(id);
    }

    @Override
    public List<Customer> findAllCustomer() {
        return this.customerDao.findAllCustomer();
    }

    @Override
    public int deleteCustomer(Integer id) {
        return this.customerDao.deleteCustomer(id);
    }

    @Override
    public int addCustomer(Customer customer) {
       return this.customerDao.addCustomer(customer);
    }

    @Override
    public int updateCustomer(Customer customer) {
        return this.customerDao.updateCustomer(customer);
    }
}


CustomerController


package com.controller;

import com.po.Customer;
import com.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@CrossOrigin
@Controller
public class CustomerController {
    @Autowired
    private CustomerService customerService;
    @ResponseBody
    @RequestMapping("/findCustomerById")
    public Customer findCustomerById(Integer id) {
        Customer customer = customerService.findCustomerById(id);
        return customer;
    }
//    查询所有
    @ResponseBody
    @RequestMapping("/findAllCustomer")
    public List<Customer> findAllCustomer(){
        List<Customer> customers = customerService.findAllCustomer();
        return customers;
    }
    //删
    @ResponseBody
    @RequestMapping("/deleteCustomer")
    public int deleteCustomer(Integer id) {
        return customerService.deleteCustomer(id);//返回改变的(删除的)行数
    }
    //增
    @ResponseBody
    @RequestMapping("/addCustomer")
    public int addCustomer(@RequestBody Customer customer) {
        return customerService.addCustomer(customer);
    }
    //改
    @ResponseBody
    @RequestMapping("/updateCustomer")
    public int updateCustomer(@RequestBody Customer customer) {
        return customerService.updateCustomer(customer);
    }
}
相关文章
|
7月前
|
前端开发 Java 应用服务中间件
SSM相关
SSM相关
|
Java 关系型数据库 MySQL
基于SSM的摊位管理系统。Javaee项目。
基于SSM的摊位管理系统。Javaee项目。
|
前端开发 Java 数据库连接
SSM整合~
SSM整合~
|
2月前
|
JSON 前端开发 Java
SSM:SpringMVC
本文介绍了SpringMVC的依赖配置、请求参数处理、注解开发、JSON处理、拦截器、文件上传下载以及相关注意事项。首先,需要在`pom.xml`中添加必要的依赖,包括Servlet、JSTL、Spring Web MVC等。接着,在`web.xml`中配置DispatcherServlet,并设置Spring MVC的相关配置,如组件扫描、默认Servlet处理器等。然后,通过`@RequestMapping`等注解处理请求参数,使用`@ResponseBody`返回JSON数据。此外,还介绍了如何创建和配置拦截器、文件上传下载的功能,并强调了JSP文件的放置位置,避免404错误。
|
6月前
|
前端开发 JavaScript Java
SSM
【6月更文挑战第6天】
31 1
|
7月前
|
Java 关系型数据库 MySQL
SpringBoot整合JUnit、MyBatis、SSM
SpringBoot整合JUnit、MyBatis、SSM
47 4
|
6月前
|
Java 数据库连接 Maven
ssm整合
ssm整合
20 0
|
7月前
|
XML Java 关系型数据库
springboot整合ssm详细讲解
springboot整合ssm详细讲解
216 1
|
7月前
|
Java 数据库连接 Spring
SSM整合
SSM整合
|
XML 存储 JSON
SSM 之 SpringMVC(下)
SSM 之 SpringMVC(下)
71 0
SSM 之 SpringMVC(下)
下一篇
DataWorks