springmvc+Ibatis+Bootstrap自定义标签分页+登录模版

阅读更多
package com.org.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.org.dao.IUserDao;
import com.org.model.User;
import com.org.service.IUserService;

@Service 
@SuppressWarnings("all")
public class UserServiceImpl implements IUserService{
	/**
	 * 注入
	 * 	@Resource 和 @Autowrite是同等可以实现
	 */
	@Resource 
	private IUserDao userDao;
	
	public List getUserList() {
		return userDao.getUserList();
	}

	@Override
	public List getUserLists(Map map) {
		return userDao.getUserLists(map);
	}

	@Override
	public Integer getUserCount(Map map) {
		return userDao.getUserCount(map);
	}

	@Override
	public User getUserById(Integer primaryKeyId) {
		return userDao.getUserById(primaryKeyId);
	}

	@Override
	public void delUserById(Integer primaryKeyId) {
		  userDao.delUserById(primaryKeyId);
	}

	@Override
	public void addUser(User entity) {
		userDao.addUser(entity);
	}

	@Override
	public void editUser(User entity) {
		userDao.addUser(entity);
	}
	
}

 

package com.org.dao.impl;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import com.org.IbatisBaseDao;
import com.org.dao.IUserDao;
import com.org.model.User;

@Repository//@Component也可以实现
@SuppressWarnings("all")
public class UserDaoImpl extends IbatisBaseDao implements IUserDao {
	/***
	 * sqlMapClient和getSqlMapClientTemplate()是一样可以实现
	 */
	public List getUserList() {
		try {
			// return sqlMapClient.queryForList("getUserList");
			return getSqlMapClientTemplate().queryForList("findUserList");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	
	public List getUserLists(Map map) {
		try {
			return sqlMapClient.queryForList("findUserLists", map);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	
	public Integer getUserCount(Map map) {
		try {
			return (Integer) sqlMapClient.queryForObject("findUserCount", map);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0;
	}

	
	public User getUserById(Integer primaryKeyId) {
		try {
			sqlMapClient.queryForObject("findUserById", primaryKeyId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	
	public void delUserById(Integer primaryKeyId) {
		try {
			sqlMapClient.delete("findDeleteUser", primaryKeyId);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	
	public void addUser(User entity) {
		try {
			sqlMapClient.insert("findInsertUser", entity);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	
	public void editUser(User entity) {
		try {
			sqlMapClient.update("findUpdateUser", entity);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

 

package com.org.controller;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.org.BaseController;
import com.org.model.User;
import com.org.service.IUserService;

/**
 * @Author:liangjilong
 * @Date:2014-2-25
 * @Version:1.0
 * @Description:
 */
@Controller
public class UserController extends BaseController {

	@Resource
	private IUserService userService;

	/***
	 * 方法一请求使用String
	 * 
	 * 请求@RequestMapping匹配的URL request
	 * 
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/userList1.do")
	public String userList1(HttpServletRequest request, HttpServletResponse response) throws Exception {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		List lists = userService.getUserList();
		if (lists != null) {
			request.setAttribute("userList", lists);
		}
		// userList指跳转到userList.jsp页面
		return "userList";
	}

	/**
	 * 方法二请求使用ModelAndView
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/userList2.do")
	public ModelAndView userList2(HttpServletRequest request, HttpServletResponse response) throws Exception {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		List lists = userService.getUserList();
		if (lists != null) {
			request.setAttribute("userList", lists);
		}
		// userList指跳转到userList.jsp页面
		return new ModelAndView("userList");
	}

	/***
	 * 自定义标签实现分页
	 * 
	 * @param request
	 * @param response
	 * @param @ResponseBody ajax响应
	 * @param method={RequestMethod.POST,RequestMethod.GET}表单请求
	 * @param consumes="application/json; charset=UTF-8"请求格式是json
	 * @return
	 * @throws UnsupportedEncodingException 
	 * @throws Exception
	 */
	@RequestMapping(value = "/pageList.do" ,method={RequestMethod.POST,RequestMethod.GET})
	public @ResponseBody ModelAndView getUserInfo(Model model, @RequestParam(required = false) String username,
			@RequestParam(required = false) Integer pageNum, @RequestParam(required = false) Integer pageSize)  {
		try {
			String userName = new String(username.getBytes("ISO-8859-1"),"UTF-8");//处理乱码
			
			Map map = new HashMap();
			username=(username==null)?"":username;
			map.put("username", username);//username必须要和ibatis配置的property=username一致
			Integer totalCount = this.userService.getUserCount(map);
			this.initPage(map, pageNum, pageSize, totalCount);
			List list = this.userService.getUserLists(map);
			this.initResult(model, list, map);
			
			return new ModelAndView("pagerList");
		} catch (Exception e) {
			e.printStackTrace();
		}  
		return null;
	}
	 
	/**
	 * 添加用户
	 * @param type
	 * @param model
	 * @return
	 */
	@RequestMapping("/addUser.do")
	public ModelAndView addUser(@RequestParam String username, Model model) {
		User user = new User();
		user.setUsername(username);
		this.userService.addUser(user);
		return this.getUserInfo(model, null, null, null);
	}
	/***
	 * 删除用户
	 * @param id
	 * @param pageNum
	 * @param pageSize
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/delUser.do",method={RequestMethod.POST,RequestMethod.GET},consumes="application/json; charset=UTF-8")
	@ResponseBody
	public ModelAndView delUser(@RequestParam(required = true) Integer id, @RequestParam(required = false) Integer pageNum,
			@RequestParam(required = false) Integer pageSize, Model model, HttpServletRequest request,HttpServletResponse response) 
	{
		PrintWriter out=null;
		 
		JSONObject result=new JSONObject();
		try {
			out=response.getWriter();
		
			this.userService.delUserById(id);
			result.put("flag", true);
			
			out.write(result.toString());
		} catch (Exception e) {
			try {
				result.put("flag", false);
				out.write(result.toString());
			} catch (JSONException e1) {
				e1.printStackTrace();
			}
		}
		
		return null;
		//return this.getUserInfo(model, null, pageNum, pageSize);
	}

	/***
	 * 编辑用户
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("/getUserById.do")
	public ModelAndView getUserById(@RequestParam(required = true) Integer id, Model model) {
		User u = this.userService.getUserById(id);
		model.addAttribute("user", u);
		return new ModelAndView("editUser");
	}
	
	/***
	 * 编辑用户
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("/editUser.do")
	public ModelAndView editUser(@RequestParam(required = true) Integer id, @RequestParam String username,
			@RequestParam(required = false) Integer pageNum,
			@RequestParam(required = false) Integer pageSize, Model model) {
		User u = new User();
		u.setUsername(username);
		this.userService.editUser(u);
		return this.getUserInfo(model, null, pageNum, pageNum);
	}

}

 

package com.org;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.ibatis.sqlmap.client.SqlMapClient;


/**
 * @author:liangjilong
 */
public class IbatisBaseDao extends SqlMapClientDaoSupport{

	/**
	 * spring注入此配置和整合hibernate是一致..
	 *  http://jilongliang.iteye.com/blog/2061557
	 */
	@Resource(name = "sqlMapClient")
	public SqlMapClient sqlMapClient;  
  
    @PostConstruct  
    public void initSqlMapClient() {  
        super.setSqlMapClient(sqlMapClient);  
    } 
}

 

package com.org.utils.taglib;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.org.utils.encode.CharacterEncode;

/***
 * 分页控件
 * springmvc+hibernate+Bootstrap自定义标签分页
 * http://jilongliang.iteye.com/blog/2062068
 */
public class Pager extends TagSupport {

	private static final long serialVersionUID = 1L;
	private String params;
	private Integer curPage;
	private Integer totalPage;
	private Integer pageSize =10; 
	private Integer totalCount = 0;
	private String formId;

	public void setCurPage(Integer curPage) {
		this.curPage = curPage;
	}

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

	public void setTotalPage(Integer totalPage) {
		this.totalPage = totalPage;
	}

	public void setFormId(String formId) {
		this.formId = formId;
	}

	public Integer getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(Integer totalCount) {
		this.totalCount = totalCount;
	}

	/***
	 * javascript:void(0);是去掉#很有必要
	 */
	public int doStartTag() throws JspException {
		StringBuffer buffer=new StringBuffer();
		JspWriter out = pageContext.getOut();

		int pageNumber = 0;
		if (totalPage % pageSize == 0) {
			pageNumber = totalPage / pageSize;
		} else {
			pageNumber = (totalPage / pageSize) + 1;
		}
		if (curPage < 1) {
			curPage = 1;
		}

		try {
			if (pageNumber > 0) {
				buffer.append("");//script-end
				
				out.print(buffer.toString());

				out.append("
");//page-number-strip out.print("
    ");//添加Bootstrap分页的样式pagination int start = 1; int end = totalPage; for (int i = 4; i >= 1; i--) { if ((curPage - i) >= 1) { start = curPage - i; break; } } for (int i = 4; i >= 1; i--) { if ((curPage + i) <= totalPage) { end = curPage + i; break; } } // 如果小于9则右侧补齐 if (end - start + 1 <= 9) { Integer padLen = 9 - (end - start + 1); for (int i = padLen; i >= 1; i--) { if ((end + i) <= totalPage) { end = end + i; break; } } } // 如果还小于9左侧补齐 if (end - start + 1 <= 9) { Integer padLen = 9 - (end - start + 1); for (int i = padLen; i >= 1; i--) { if ((start - i) >= 1) { start = start - i; break; } } } if (curPage > 1) { if (start > 1) { out.print("
  • 首页
  • "); } out.print("
  • 上一页
  • "); } for (int i = start; i <= end; i++) { if (i == curPage) { out.print("
  • " + i + "
  • "); } else { out.print("
  • " + i + "
  • "); } } if (curPage < totalPage) { out.print("
  • 下一页
  • "); if (end < totalPage) { out.print("
  • 尾页
  • "); } } out.print("
  • 共" + totalPage + "页" + this.totalCount + "条
  • "); out.print("
"); out.print("
"); } } catch (IOException e) { e.printStackTrace(); } return super.doStartTag(); } /*** * 获取页面的参数 * @param 格式(parms="username=${user.username}) * @return */ public String getParms() { StringBuffer buffer=new StringBuffer(); if(params!=null && params.length()>0){ //分离参数 A=b String[] parmsArr=params.split("&"); for(int i=0;i 0 ? "&" : ""); buffer.append(parmsEqArr[0]).append("="); if(parmsEqArr.length>1){ //ps.append(URLDecoder.decode(parmsEqArr[1],"UTF-8")); buffer.append(CharacterEncode.URLEncoder(parmsEqArr[1])); } } catch (Exception e) { return ""; } } } return buffer.toString(); } public static Integer getStartIndex(Integer pageNum, Integer pageSize) { Integer res = 0; if (pageNum > 0) { res = (pageNum - 1) * pageSize; } return res; } public String getParams() { return params; } public void setParams(String params) { this.params = params; } }

 


	

	
	
	
	
	
	
	
	 
	
	
	
	
	
	
		  update user  set Id=#id#
		password=#password#
		username=#username#
		  where Id=#id# 
	
	
	
	 
    	  INSERT INTO user
    	 (
    	  	id,
			username,
			password,
			createDate,
			modifyDate,
			type
    	  )
    	  values(
    	  	#id#,
    	  	#username#,
    	  	#password#,
    	  	#createDate#,
    	  	#modifyDate#,
    	  	#type#
    	  );
	  
	  
	  
	  
	  		delete from user where id=#id#
	  

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="/morinda-tags" prefix="m"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


  
    ${title }
	
	
	
  
  
  
  
	 
搜索条件
用户名:
编号 用户 创建时间 修改时间 操作
${st.count} ${user.username } ${user.createDate} ${user.modifyDate} 删除 修改

 




	 
	
	
	  
	
	
	
	
		
		
		
		 
	
	 
	
	
	
		
			classpath:jdbc.properties
		
	
	
	
		
		
		
		
		
          
            
                
         
	
		
	 
   
       
           classpath:ibatis-Base.xml
       
       
       		 
       
    
   
	
		
	
	
	
		
	
	
	
	  
	
		
		
	

	
	
		
			
			
			
			
			
		
	
	 
	 

  
   
  
 

 

package com.org;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.ibatis.sqlmap.client.SqlMapClient;

/**
 *@DEMO:dress
 *@Java:T.java
 *@Date:2015-1-9下午5:03:13
 *@Author:liangjilong
 *@Email:[email protected]
 *@Weibo:http://weibo.com/jilongliang
 *@Version:1.0
 *@Description:IbatisDao的封装类
 */
@SuppressWarnings("all")
public class BaseIbatisDao extends SqlMapClientDaoSupport{
	
	private Logger log4j = Logger.getLogger(BaseIbatisDao.class);
	/**
	 * @Autowired和@Resource一样的可以注入
	 * 
	 */
	@Resource(name = "sqlMapClient")
    public SqlMapClient sqlMapClient;  
  
    @PostConstruct  
    public void initSqlMapClient() {  
        super.setSqlMapClient(sqlMapClient);  
    } 
    
    /************************--------------*新增----------------********************/

	/**
	 * 根据条件查询对象集合
	 * 
	 * @param sqlid
	 *            对应IBATIS xml SQL_ID
	 * @param paramObj
	 *            参数对象
	 * @return
	 */
	
	public  List loadList(String sqlid, Object paramObj) {
		return (List) getSqlMapClientTemplate()
				.queryForList(sqlid, paramObj);
	}

	/**
	 * 根据条件查询对象所有数据
	 * 
	 * @param 
	 * @param sqlid
	 *            对应IBATIS xml SQL_ID
	 * @return
	 */
	
	public  List loadList(String sqlid) {
		return (List) getSqlMapClientTemplate().queryForList(sqlid);
	}

	/**
	 * 根据ID查询ENTITY 对象
	 * 
	 * @param 
	 * @param sqlid对应IBATIS
	 *            xml SQL_ID
	 * @return  实体对象
	 */
	
	public  T loadObject(String sqlid) {
		return (T) getSqlMapClientTemplate().queryForObject(sqlid);
	}

	/**
	 * 根据ID查询ENTITY 对象
	 * 
	 * @param 
	 * @param sqlid对应IBATIS
	 *            xml SQL_ID
	 * @param id
	 *            实体ID
	 * @return  实体对象
	 */
	
	public  T loadObject(String sqlid, String id) {
		return (T) getSqlMapClientTemplate().queryForObject(sqlid, id);
	}

	/**
	 * 根据ID查询ENTITY 对象
	 * 
	 * @param 
	 * @param sqlid对应IBATIS
	 *            xml SQL_ID
	 * @param id
	 *            实体ID
	 * @return  实体对象
	 */
	
	public  T loadObject(String sqlId, Long id) {
		return (T) getSqlMapClientTemplate().queryForObject(sqlId, id);
	}

	/**
	 * 根据条件查询对象
	 * 
	 * @param 
	 * @param sqlid对应IBATIS
	 *            xml SQL_ID
	 * @param paramObj
	 *            参数
	 * @return  实体对象
	 */
	
	public  T loadObject(String sqlId, Object paramObj) {
		return (T) getSqlMapClientTemplate().queryForObject(sqlId, paramObj);
	}

	/**
	 * 保存对象
	 * 
	 * @param sqlid
	 *            对应IBATIS xml SQL_ID
	 * @param entity
	 *            保存的对象
	 */
	public void save(String sqlid, Object entity) {
		getSqlMapClientTemplate().insert(sqlid, entity);
	}

	/**
	 * 保存对象
	 * 
	 * @param sqlid
	 *            对应IBATIS xml SQL_ID
	 * @param entity
	 *            保存的对象
	 */
	public void save(String sqlid, Map entity) {
		getSqlMapClientTemplate().insert(sqlid, entity);
	}

	/**
	 * 更新对象
	 * 
	 * @param sqlid
	 *            对应IBATIS xml SQL_ID
	 * @param entity
	 *            修改对象
	 */
	public void update(String sqlId, Map entity) {
		getSqlMapClientTemplate().update(sqlId, entity);
	}

	/**
	 * 更新对象
	 * 
	 * @param sqlid
	 *            对应IBATIS xml SQL_ID
	 * @param entity
	 *            修改对象
	 */
	public void update(String sqlId, Object entity) {
		getSqlMapClientTemplate().update(sqlId, entity);
	}

	/**
	 * 删除指定的对象
	 * 
	 * @param sqlId
	 * @param object
	 *            需要删除的对象
	 */
	public void delete(String sqlId, Object object) {
		getSqlMapClientTemplate().delete(sqlId, object);
	}

	/**
	 * 查询数据总条数
	 * 
	 * @param sqlid
	 * @param object
	 * @return
	 */
	public Long loadRecordCountObject(String sqlid, Object object) {
		log4j.info("sqlid====" + sqlid);
		return (Long) getSqlMapClientTemplate().queryForObject(sqlid, object);
	}

	/**
	 * 查询数据总条数
	 * 
	 * @param sqlid
	 * @param object
	 * @return 返回Int
	 */
	public Integer loadRecordCount(String sqlid, Object object) {
		log4j.info("sqlid====" + sqlid);
		return (Integer) getSqlMapClientTemplate() .queryForObject(sqlid, object);
	}

	/**
	 * @Description: 返回表中ID最大值加一
	 * @param:sqlMapId Ibatis配置的ID
	 * @param tabName 表名
	 * @return:
	 * @returnType: Long
	 * @throws
	 */
	public Long findNextId(String sqlMapId,String tabName) {
		Long id = 0l;
		String seqName = tabName.substring(3) + "_S";
		id = (Long) getSqlMapClientTemplate().queryForObject(sqlMapId, seqName);
		if (id == null || id.equals(0l)){
			id=null;
		}
		return id;
	}
	/**
	 * 
	 * @param:sqlMapId Ibatis配置的ID
	 * @return
	 */
	public Date findOracleSysDate(String sqlMapId) {
		return (Date) getSqlMapClientTemplate().queryForObject(sqlMapId, null);
	}
}

 

 
springmvc+Ibatis+Bootstrap自定义标签分页+登录模版_第1张图片
 
springmvc+Ibatis+Bootstrap自定义标签分页+登录模版_第2张图片
 
springmvc+Ibatis+Bootstrap自定义标签分页+登录模版_第3张图片
 
springmvc+Ibatis+Bootstrap自定义标签分页+登录模版_第4张图片
 源代码

  • springmvc+Ibatis+Bootstrap自定义标签分页+登录模版_第5张图片
  • 大小: 15.9 KB
  • springmvc+Ibatis+Bootstrap自定义标签分页+登录模版_第6张图片
  • 大小: 63.3 KB
  • springmvc+Ibatis+Bootstrap自定义标签分页+登录模版_第7张图片
  • 大小: 70.6 KB
  • springmvc+Ibatis+Bootstrap自定义标签分页+登录模版_第8张图片
  • 大小: 54.4 KB
  • 查看图片附件

你可能感兴趣的:(springmvc,ibatis)