• SSM 前后端分离 这里controll层的返回值和之前那个不一样


    1.先创建实体类:

    2.创建mapper层

    package cn.kgc.mapper;

    import cn.kgc.Account;
    import org.apache.ibatis.annotations.Delete;
    import org.apache.ibatis.annotations.Insert;
    import org.apache.ibatis.annotations.Select;
    import org.apache.ibatis.annotations.Update;

    import java.util.List;

    /**
    * Created by 86182 on 2019/7/1.
    */
    public interface AccountMapper {
    //查询所有
    @Select("select * from account")
    List<Account> selectAccount();
    //查询单条
    @Select("select * from account where id=#{id}")
    Account selectByid(Integer id);
    //添加
    @Insert("insert into account(id,number,pwd,money,status,createtime) values(#{id},#{number},#{pwd},#{money},#{status},now())")
    Integer insertAccount(Account account);
    //修改
    @Update("update account set number =#{number},pwd=#{pwd},money=#{money},status=#{status} WHERE id =#{id}")
    Integer updateAccount(Account account);
    //删除
    @Delete("delete from account where id=#{id}")
    Integer deleteAccount(Integer id);
    }
    3.创建service层
    3.1接口层:
    package cn.kgc.service;

    import cn.kgc.Account;

    import java.util.List;

    /**
    * Created by 86182 on 2019/7/1.
    */
    public interface AccountService {
    //页面展示
    List<Account> showData();
    //查询单条
    Account findByData(Integer id);
    //添加
    Integer add(Account account);
    //修改
    Integer edit(Account account);
    //删除
    Integer del(Integer id);
    }
    3.2实现类
    package cn.kgc.service;

    import cn.kgc.mapper.AccountMapper;
    import cn.kgc.Account;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

    import java.util.List;

    /**
    * Created by 86182 on 2019/7/1.
    */
    @Service
    @Transactional
    public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountMapper accountMapper;
    @Override
    public List<Account> showData() {
    return accountMapper.selectAccount();
    }

    @Override
    public Account findByData(Integer id) {
    return accountMapper.selectByid(id);
    }

    @Override
    public Integer add(Account account) {
    return accountMapper.insertAccount(account);
    }

    @Override
    public Integer edit(Account account) {
    return accountMapper.updateAccount(account);
    }

    @Override
    public Integer del(Integer id) {
    return accountMapper.deleteAccount(id);
    }
    }
    4.controller层
    package cn.kgc.controller;

    import cn.kgc.service.AccountService;
    import cn.kgc.Account;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;

    import java.util.List;

    /**
    * Created by 86182 on 2019/7/1.
    */
    @RestController
    public class AccountController {
    @Autowired
    private AccountService accountService;

    //查询全部
    @RequestMapping("/init.do")
    public List<Account> showData(){
    return accountService.showData();
    }
    //添加
    @RequestMapping("/add.do")
    public int addData(Account account){
    return accountService.add(account);
    }
    //查询单条
    @RequestMapping("/info.do")
    public Account getInfo(Integer id){
    return accountService.findByData(id);
    }
    //修改
    @RequestMapping("/edit.do")
    public int editData(Account account){
    return accountService.edit(account);
    }
    //删除
    @RequestMapping("/del.do")
    public int deleteData(Integer id){
    return accountService.del(id);
    }
    }
    前台页面html

    js页面

    $(function () {
    initData();
    })
    function initData() {
    $.ajax({
    url:"init.do",
    type:"post", //post的方式提交
    dataType:"json", //数据类型是json格式
    data:{},
    async:true,
    success:function (obj) {
    // alert("success");
    console.log(obj);
    var str="";
    $.each(obj,function(i) {
    str+="<tr>";
    str+=" <td>"+obj[i].id+"</td>";
    str+=" <td>"+obj[i].number+"</td>";
    str+=" <td>"+obj[i].money+"</td>";
    str+=" <td>"+obj[i].status+"</td>";
    str+=" <td>"+obj[i].createtime+"</td>";
    str+=" <td>" +
    "<a href='edit.html?id="+obj[i].id+"'>修改</a>" +
    "|<a href='javascript:void(0);' onclick='delFun("+obj[i].id+")'>删除</a>" +
    "</td>";
    str+="</tr>";
    });
    $("table").append(str);
    },
    error:function () {
    alert("error")
    }
    });
    }
    function delFun(id) {
    $.ajax({
    url:"del.do",
    type:"post",
    dataType:"json",
    data:{"id":id},
    async:true,
    success:function (obj) {
    location.href="main.html";
    },
    error:function () {
    alert("del error")
    }
    });
    }
    
    
  • 相关阅读:
    今天查看了java文档中的sort方法
    记录下git简单使用(以码云为例)
    今天是leetcode300
    今天了解了一下摩尔投票法
    # 今天学习了一下java8的lambda表达式
    make命令的-j -f参数说明
    shell中单引号和双引号区别
    判断字符串相似度
    shell计算时间差
    hive cli转hive beeline的几个例子
  • 原文地址:https://www.cnblogs.com/shxkey/p/11273661.html
Copyright © 2020-2023  润新知