查看
getMyData()
{
axios.get('http://localhost:8081/test').then(
(response)=>
{
console.log(response.data)
},
(error)=>
{
console.log("error")
}
)
},
mapper:
@Select("select * from test")
List<Test> findAll();
controller:
@Resource
TestMapper TestMapper;
@GetMapping
@RequestMapping("/test")
public List<Test>getT()
{
return TestMapper.findAll();
}
增加(*)=>需要将请求头格式设置为application/json;charset=utf-8
axios.defaults.headers.post['Content-Type'] = 'application/json;charset=utf-8';
addMyData()
{
let user= {
id:'123',
name:'七七七',
sex:'男',
age:19,
school:'攀大'
};
axios({
url:'http://localhost:8081/addUser',
method:'post',
data:JSON.stringify(user),
}).then(
(response)=>
{
console.log(response.data)
},
(error)=>
{
console.log(error.message)
}
)
},
mapper:传过来的对象t中必须要有values后面的属性名和属性值
@Insert("insert into test(id,name,sex,age,school) values(#{id},#{name},#{sex},#{age},#{school})")
void addUser(@RequestBody Test t);
controller:@RequestBody代表传入的是一个对象
@GetMapping
@RequestMapping(path = "/addUser")
public String addUser(@RequestBody Test t)
{
TestMapper.addUser(t);
return "增加成功";
}
删除:前端需要用params{}将参数囊括进去
deleteMyData(id)
{
axios({
url:'http://localhost:8081/deleteById',
method:'post',
params:
{
id:id
}
}).then(
(response)=>
{
console.log(response.data)
},
(error)=>
{
console.log(error.message)
}
)
}
mapper:
@Delete("delete from test where id=#{id}")
void deleteById(String id);
controller:(controller需要用@RequestParam获取查询参数)
@GetMapping
@RequestMapping("/deleteById")
public String deleteById(@RequestParam String id)
{
TestMapper.deleteById(id);
return "删除成功";
}
修改
updateMyData(id)
{
let user2 =
{
id:id,
name:'哈哈',
}
axios({
url:'http://localhost:8081/updateUser',
method:'post',
data:JSON.stringify(user2),
}).then(
(res)=>
{
console.log(res.data)
})
}
mapper:
@Update("update test set name=#{name} where id=#{id}")
void updateById(@RequestBody Test t);
controller:
@GetMapping
@RequestMapping(path= "/updateUser")
public String updateUser(@RequestBody Test t)
{
System.out.println(t.toString());
TestMapper.updateById(t);
return "修改成功";
}