package cn.sm1234.controller;
import cn.sm1234.pojo.Customer;
import cn.sm1234.service.CustomerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("/customer")
public class CustomerController {
//注入service对象
@Autowired
private CustomerService customerService ;
/**
*查询所有数据,给页面返回一个json格式数据
*/
@RequestMapping("/list")
@ResponseBody
public List list(){
//查询数据
List list = customerService.findAl();
return list ;
}
//设计Map聚合存储需要给页面的对象数据
private Map result = new HashMap();
/**
* 分页查询
*/
@RequestMapping("/listByPage")
@ResponseBody
public Map listByPage(Integer page,Integer rows){
//设置分页参数
PageHelper.startPage(page, rows);
//查询所有数据
List list = customerService.findAl();
//使用PageInfo封装查询结果
PageInfo pageInfo = new PageInfo(list);
//从PageInfo对象取出查询结果
//总记录数
long total = pageInfo.getTotal();
//当前页数据列表
List custList = pageInfo.getList();
result.put("total", total);
result.put("rows", custList);
return result;
}
/**
* 保存数据
* @return
*/
@RequestMapping("/save")
@ResponseBody
public Map save(Customer customer){
try {
customerService.save(customer) ;
result.put("success",true) ;
} catch (Exception e){
e.printStackTrace();
result.put("success",false) ;
result.put("msg",e.getMessage()) ;
}
return result ;
}
/**
* 根据id 查询对象
* @return
*/
@RequestMapping("/findById")
@ResponseBody
public Customer findById(Integer id){
Customer customer = customerService.findById(id);
return customer ;
}
/**
* 删除数据
* @param id
* @return
*/
@RequestMapping("/delete")
@ResponseBody
public Map delete(String[] id){
try {
customerService.delete(id);
result.put("success",true) ;
}catch (Exception e){
result.put("success",false) ;
}
return result ;
}
}
package cn.sm1234.dao;
import cn.sm1234.pojo.Customer;
import java.util.List;
public interface CustomerMapper {
/**
* 查询所有数据
* @return
*/
public List findAl() ;
/**
* 保存数据
* @param customer
*/
public void save(Customer customer);
/**
* 根据id 查询对象
* @param id
* @return
*/
public Customer findById(Integer id);
/**
* 修改对象数据
* @param customer
*/
public void update(Customer customer);
/**
* 删除数据
* @param id
*/
public void delete(String[] id);
}
INSERT INTO t_customer (
name,
gender,
telephone,
address
)
VALUES
(
#{name},
#{gender},
#{telephone},
#{address}
);
UPDATE ssm.t_customer
SET
NAME = #{name} ,
gender = #{gender} ,
telephone = #{telephone} ,
address = #{address}
WHERE
id = #{id}
DELETE FROM ssm.t_customer
id
#{id}
package cn.sm1234.service;
import cn.sm1234.pojo.Customer;
import java.util.List;
public interface CustomerService {
/**
* 查询所有数据
* @return
*/
public List findAl() ;
/**
* 保存数据
* @param customer
*/
public void save(Customer customer);
/**
*
* @param id
* @return
*/
public Customer findById(Integer id);
/**
* 删除数据
* @param id
*/
public void delete(String[] id);
}
package cn.sm1234.service;
import cn.sm1234.dao.CustomerMapper;
import cn.sm1234.pojo.Customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class CustomerServiceImpl implements CustomerService {
//注入mapper接口对象
@Autowired
private CustomerMapper customerMapper;
@Override
public List findAl() {
return customerMapper.findAl();
}
@Override
public void save(Customer customer) {
//判断是添加还是修改
if (customer.getId()!=null){
//修改
customerMapper.update(customer) ;
}else {
customerMapper.save(customer);
}
}
@Override
public Customer findById(Integer id) {
return customerMapper.findById(id);
}
@Override
public void delete(String[] id) {
customerMapper.delete(id) ;
}
}
package cn.sm1234.pojo;
public class Customer {
private Integer id;
private String name;
private String gender;
private String telephone;
private String address;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Customer [id=" + id + ", name=" + name + ", gender=" + gender
+ ", telephone=" + telephone + ", address=" + address + "]";
}
}
applicationContext.xml
helperDialect=mysql
jdbc.properties
jdbc.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf-8
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=123456
spring-mvc.xml
省略JSP文件与JS文件 ....