学习笔记のspringmvc + spring + jdbc + oracle+c3p0+easyUI

学习完springmvc之后的一个小的整合性项目(属于学习笔记)



数据库表使用的是oracle的scott用户的自带表emp

emp(empno/ename/job/mgr/hiredate/sal/comm/deptno)


web项目
1.导入jar包

---spring的CORE、AOP和WEB模块的jar包

org.springframework.asm-3.0.5.RELEASE.jar
org.springframework.beans-3.0.5.RELEASE.jar
org.springframework.context-3.0.5.RELEASE.jar
org.springframework.core-3.0.5.RELEASE.jar
org.springframework.expression-3.0.5.RELEASE.jar
org.springframework.web-3.0.5.RELEASE.jar
org.springframework.web.servlet-3.0.5.RELEASE.jar

---oracle数据库驱动包

ojdbc6.jar

---C3P0数据库连接池包(高版本需要导入依赖包)

c3p0-0.9.1.2.jar

---工具类包

commons-dbutils-1.4.jar
commons-logging.jar

---json转换包

jackson-core-asl-1.9.11.jar
jackson-mapper-asl-1.9.11.jar


2.导入前台框架easyUI的源文件

jquery.easyui.min.js

easyui-lang-zh_TW.js
jquery.min.js

(建议放在WebRoot下新建的js文件夹中)

和主题包themes(内有css样式的easyUI的图标)


3.配置文件配置

---c3p0数据源文件c3p0-config.xml(建议放在src目录下)



	
		oracle.jdbc.OracleDriver
		jdbc:oracle:thin:@127.0.0.1:1521:orcl
		scott
		tiger
				
		2
				
		5
				
		5
				
 		1
		


---web.xml文件配置(在WEB-INF目录下)


  	
  
   
  
  	DispatcherServlet
  	org.springframework.web.servlet.DispatcherServlet
  	
  		contextConfigLocation
  		classpath:springmvc.xml
  	
  	1
  
  
  	DispatcherServlet
  	*.do
  
  
  
  	CharacterEncodingFilter
  	 org.springframework.web.filter.CharacterEncodingFilter
  
  
  	CharacterEncodingFilter
  	/*
  
  
  
    index.jsp
  


---spring的核心配置文件springmvc.xml(文件名称和位置不固定,此处是因为我在web.xml中配置的名称是springmvc.xml,路径为src目录下)



   
   
   
   
   
   
   
   		
   			
   				
   			
   		
   
   
  
  


4.配置JavaBean

---与数据库表emp对应的实体类EmpBean.java

package com.emp.bean;

import java.util.Date;

public class EmpBean {

	private Integer empno;
	private String ename;
	private String job;
	private Integer mgr;
	private Date hiredate;
	private Integer sal;
	private Integer comm;
	private Integer deptno;
	@Override
	public String toString() {
		return "EmpBean [empno=" + empno + ", ename=" + ename + ", job=" + job
				+ ", mgr=" + mgr + ", hiredate=" + hiredate + ", sal=" + sal
				+ ", comm=" + comm + ", deptno=" + deptno + "]";
	}
	public EmpBean() {
		super();
		// TODO Auto-generated constructor stub
	}
	public EmpBean(Integer empno, String ename, String job, Integer mgr, Date hiredate,
			Integer sal, Integer comm, Integer deptno) {
		super();
		this.empno = empno;
		this.ename = ename;
		this.job = job;
		this.mgr = mgr;
		this.hiredate = hiredate;
		this.sal = sal;
		this.comm = comm;
		this.deptno = deptno;
	}
	public Integer getEmpno() {
		return empno;
	}
	public void setEmpno(Integer empno) {
		this.empno = empno;
	}
	public String getEname() {
		return ename;
	}
	public void setEname(String ename) {
		this.ename = ename;
	}
	public String getJob() {
		return job;
	}
	public void setJob(String job) {
		this.job = job;
	}
	public Integer getMgr() {
		return mgr;
	}
	public void setMgr(Integer mgr) {
		this.mgr = mgr;
	}
	public Date getHiredate() {
		return hiredate;
	}
	public void setHiredate(Date hiredate) {
		this.hiredate = hiredate;
	}
	public Integer getSal() {
		return sal;
	}
	public void setSal(Integer sal) {
		this.sal = sal;
	}
	public Integer getComm() {
		return comm;
	}
	public void setComm(Integer comm) {
		this.comm = comm;
	}
	public Integer getDeptno() {
		return deptno;
	}
	public void setDeptno(Integer deptno) {
		this.deptno = deptno;
	}
}

---连接数据库的工具类JdbcUtil.java

package com.emp.util;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class JdbcUtil {

	/**
	 * 去src目录下加载c3p0-config.xml配置文件
	 */
	private static ComboPooledDataSource dataSource=new ComboPooledDataSource();
	/**
	 * 
	 * date:2017-7-23
	 * author:PeterW
	 * function:获取数据源
	 */
	public static ComboPooledDataSource getDataSource(){
		
		return dataSource;
	}
}


5.DAO数据持久层EmpDao.java(带分页的列表展示方法和查询总记录数的方法)

package com.emp.dao;

import java.math.BigDecimal;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.springframework.stereotype.Repository;

import com.emp.bean.EmpBean;
import com.emp.util.JdbcUtil;

/**
 * 员工管理模块
 * 持久层实现类 
 * @author PETER
 */
@Repository
public class EmpDao {

	/**
	 * 
	 * date:2017-7-23
	 * author:PeterW
	 * function:查询所有员工
	 * @throws Exception 
	 */
	public List findAll(int currentPage,int pageSize) throws Exception{
		
		QueryRunner runner=new QueryRunner(JdbcUtil.getDataSource());
		String sql="select * from (select e.*,rownum rn from emp e) where rn>=(1+"+(currentPage-1)+"*"+pageSize+") and rn <(1+"+(currentPage)+"*"+pageSize+")";
		List list = runner.query(sql, new BeanListHandler(EmpBean.class));
		
		return list;
	}
	/**
	 * 
	 * date:2017-7-23
	 * author:PeterW
	 * function:查询总记录数
	 * ScalarHandler
	 * @throws Exception 
	 */
	public int listCount() throws Exception{
		
		QueryRunner runner=new QueryRunner(JdbcUtil.getDataSource());
		String sql="select count(*) from emp";
		BigDecimal bd = (BigDecimal) runner.query(sql,new ScalarHandler());
		
		return bd.intValue();
	}

	public static void main(String[] args) throws Exception {
		EmpDao dao = new EmpDao();
		
		/*for (EmpBean emp: dao.findAll(1,3)) {
			System.out.print("---"+emp.getEmpno());
			System.out.print("---"+emp.getEname());
			System.out.print("---"+emp.getSal());
		}*/
		
		int i = dao.listCount();
	}
}

6.service业务逻辑层EmpService.java

package com.emp.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.emp.bean.EmpBean;
import com.emp.dao.EmpDao;

/**
 * 员工管理模块
 * 业务层实现类 
 * @author PETER
 */
@Service
public class EmpService {

	@Resource(name="empDao")
	private EmpDao empDao;
	/**
	 * 
	 * date:2017-7-23
	 * author:PeterW
	 * function:查询所有员工
	 * @throws Exception 
	 */
	public List findAllEmp(int currentPage,int pageSize) throws Exception{
		
		return empDao.findAll(currentPage,pageSize);
	}
	
	public int listCount() throws Exception{
		
		return empDao.listCount();
	}
}


7.控制层EmpAction.java

package com.emp.action;

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

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.emp.bean.EmpBean;
import com.emp.service.EmpService;

/**
 * 员工管理模块
 * 控制器 
 * @author PETER
 */
@Controller
@RequestMapping("/emp")
public class EmpAction {

	@Resource(name="empService")
	private EmpService empService;
	
	/**
	 * 
	 * date:2017-7-23
	 * author:PeterW
	 * function:查询所有的员工
	 * @throws Exception 
	 */
	@RequestMapping("/findAllEmpMethod")
	public @ResponseBody Map findAllEmpMethod(int page,int rows) throws Exception{
		System.out.println("1-----"+page);
		System.out.println("2-----"+rows);
		//调用业务层,返回List对象
		List list = empService.findAllEmp(page,rows);
		//查询总记录数
		int listCount = empService.listCount();
		//创建Map对象
		Map map = new LinkedHashMap();
		//封装DataGrid需要的二个参数
		map.put("total", listCount);
		map.put("rows", list);
		System.out.println("map="+map);
		//返回需要转成json文本的对象
		return map;
	}
}


8.在WebRoot下创建empList.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'empList.jsp' starting page
    
	
	
	    
	
	
	
	
	
	

  
  
  
  
  
  
  	
搜索


9.把项目部署到tomcat服务器上,运行,效果如下图



 
 

你可能感兴趣的:(学习笔记のspringmvc + spring + jdbc + oracle+c3p0+easyUI)