myeclipse的ssm框架整合(实现增删改查)

一、实验效果:
myeclipse的ssm框架整合(实现增删改查)_第1张图片
myeclipse的ssm框架整合(实现增删改查)_第2张图片myeclipse的ssm框架整合(实现增删改查)_第3张图片二、环境搭建说明:
myEclipse+jdk1.8+mysql1.6+ssm框架
ssm所有的jar包(百度网盘):
https://pan.baidu.com/s/1tPw0tYy-LKLyX3p3yp6wxQ
提取码:9lr5
三、项目分层截图:
myeclipse的ssm框架整合(实现增删改查)_第4张图片四、数据库设计截图:
myeclipse的ssm框架整合(实现增删改查)_第5张图片五、web.xml配置文件:



  nueo_ssmOne
  
    index.html
    index.htm
    index.jsp
    default.html
    default.htm
    default.jsp
  
  
  
		springMVC
		org.springframework.web.servlet.DispatcherServlet
		
		
			contextConfigLocation
			
			classpath:applicationContext.xml
		
		1
	
    
		springMVC
		/
	
  
  
	
		charcterEncoding
		org.springframework.web.filter.CharacterEncodingFilter
		
			encoding
			utf-8
		
	
	
		charcterEncoding
		/*
	
  
  	
	
		org.springframework.web.context.ContextLoaderListener
	
	
	
	
		contextConfigLocation
		classpath:applicationContext.xml
	

六、applicationContext.xml配置




	
	
		
		
		
		
	
	
	
	
		
		
		
		
		
		
	
	
	
	
			
		
			
		
	
	
	
	
		
		
		
	
	
	
	
		
		
	
	  
      
          
          
        
              
              
              
              
          
    
	
	  
      
          
          
    
	
	
	
	
	
	
	
	
	
	

八、各层代码的实现:
1、实体类pojo:
Person

public class Person implements Serializable {
	private int pid;
	private String pname;
//	年龄
	private int page;
	
	public int getPid() {
		return pid;
	}
	public void setPid(int pid) {
		this.pid = pid;
	}
	public String getPname() {
		return pname;
	}
	public void setPname(String pname) {
		this.pname = pname;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public Person() {
	}
	public Person(int pid, String pname, int page) {
		super();
		this.pid = pid;
		this.pname = pname;
		this.page = page;
	}
	@Override
	public String toString() {
		return "Person [pid=" + pid + ", pname=" + pname + ", page=" + page
				+ "]";
	}
}

PageBean.java

/**
 * 分页Model类
 * @author 
 *
 */
public class PageBean {
	/**
	 * 总记录数
	 */
	private int total;
	/**
	 * 总页数
	 */
	private int totalPage;
	private int page; // 第几页
	private int pageSize; // 每页记录数
	private int start;  // 起始页
	
	
	public int getTotal() {
		return total;
	}
	public void setTotal(int total) {
		this.total = total;
	}
//	计算总页数
	public int getTotalPage() {
		return total%pageSize==0?total/pageSize:(total/pageSize+1);
	}
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	public PageBean(int page, int pageSize) {
		super();
		this.page = page;
		this.pageSize = pageSize;
	}
	
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	
	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getStart() {
		return (page-1)*pageSize;
	}
	@Override
	public String toString() {
		return "PageBean [total=" + total + ", totalPage=" + totalPage
				+ ", page=" + page + ", pageSize=" + pageSize + ", start="
				+ start + "]";
	}
}

2、mapper:
接口:PersonMapper.java

public interface PersonMapper {
	public List getAll();
	public void add(Person person);
	public void delete(int pid);
	public Person getById(int pid);
	public void update(Person person);
		//分页
	public List findPage(Map map);
	public Long getTotal();
}

映射mapper:PersonMapper.xml


	
	
	
		insert into person(pname,page) values(#{pname},#{page})

	
	
		delete from person where pid=#{pid}
	
	
	
		update person set pname=#{pname}, page=#{page} where pid=#{pid}
	
	
		

	
 

3、service层:
接口:PersonService.java

public interface PersonService {
	public List getAll();
	public void add(Person person);
	public void delete(int pid);
	public Person getById(int pid);
	public void update(Person person);
	public List findPage(Map map);
	
	public Long getTotal();
}

实现类:

//将service实现交给springIOC管理
@Service(value="personServiceImpl")
public class PersonServiceImpl implements PersonService{
	
	@Resource
	private PersonMapper personMapper;
	
	@Override
	public List getAll() {
		return personMapper.getAll();
	}

	@Override
	public void add(Person person) {
		personMapper.add(person);
	}

	@Override
	public void delete(int pid) {
		personMapper.delete(pid);
	}

	@Override
	public Person getById(int pid) {
		return personMapper.getById(pid);
	}

	@Override
	public void update(Person person) {
		personMapper.update(person);
	}
		@Override
	public List findPage(Map map) {
		return personMapper.findPage(map);
	}

	@Override
	public Long getTotal() {
		return personMapper.getTotal();
	}

}

4、controller控制器:
PersonController.java

@Controller
public class PersonController {
	@Resource
	private PersonService personService;
		@RequestMapping("/list")
	public String list(@RequestParam(value = "page", required = false,defaultValue="1")String page,ModelMap datamap){
//		if (page == null || page.equals("")) {
//			page = "1";
//		}
		//每页显示条数
		int pagesize=2;
		PageBean pageBean = new PageBean(Integer.parseInt(page), pagesize);
//		获得数据总条数
		long total = personService.getTotal();
//		将总数set进页面对象
		pageBean.setTotal(Integer.parseInt(total + ""));
		
		Map map = new HashMap();
		map.put("start", pageBean.getStart());
		map.put("size", pageBean.getPageSize());
		List list = personService.findPage(map);
		System.out.println("pageBean:"+pageBean);
		System.out.println("list:"+list);
		datamap.put("page", pageBean);
		datamap.put("list", list);

		return "forward:/list.jsp";
	} 
	@RequestMapping("/getall")
	public String getAll(ModelMap datamap) {
		List list = personService.getAll();
		
		datamap.put("list", list);
		//return "forward:/list.jsp";
		return "list";

	}
	
	@RequestMapping("/add")
	public String add(Person p) {
		personService.add(p);
		return "redirect:/getall";

	}
	
	@RequestMapping("/delete")
	public String delete(int pid) {
		personService.delete(pid);
		return "redirect:/getall";
	}
	
	@RequestMapping("/getbyid")
	public String getById(int pid, ModelMap map) {
		Person p = personService.getById(pid);
		map.put("person", p);
		//return "forward:/update.jsp";
		return "update";
	}
	
	@RequestMapping("/update")
	public String update(Person p) {
		personService.update(p);
		return "redirect:/getall";
	}
}

九、前端页面的:
1、index.jsp

 
  <%response.sendRedirect("getall"); %>
  

2、list.jsp


	添加
	
			
编号 姓名 年龄 操作
${person.pid } ${person.pname } ${person.page } 删除 修改

总页数:${page.totalPage } 当前页:${page.page } 首页 上一页 下一页 末页

3、add.jsp

  
  
姓名:
年龄:

4、update.jsp

 
  
姓名:
年龄:

你可能感兴趣的:(springMVC)