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

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

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")
}
});
}
					
										


					

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/588036.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-12
下一篇 2022-04-12

发表评论

登录后才能评论

评论列表(0条)

保存