三层架构实现文件上传和下载1(dao,service)

在写程序前写好文档:

readme.txt

 

1.准备开发环境
	1.1导入开发包
		mysql驱动
		c3po连接池
		dbutils
		fileupload组件:
		1)io包
		beanutils开发包:
		log4j开发包
		jstl开发
		
	1.2创建组织程序的开发包
	
	com.du.dao.impl
	com.du.service.impl
	com.du.web.controller
	com.du.utils
	junits.test
	com.du.domain
	
	1.3 准备库和表
		create database day18;
		use day18;
		create table upfile
		(
			id varchar(40) primary key,
			uuidname varchar(100) not null unique,
			filename varchar(100) not null,
			savepath varchar(255) not null,
			uptime datetime not null,
			description varchar(255),
			username varchar(40) not null
		);
	
2.做实体

3.做dao

4.做service

5.做web层

 

 

 

jdbc工具类,jdbcutils:

 

 

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class JdbcUtils {

	private static DataSource ds;

	static {
		ds = new ComboPooledDataSource();//c3p0获取连接池
	}

	public static DataSource getDataSource() {
		
		return ds;
	}
}
 

 

 

注意:用c3p0获取连接池的时候,要编写c3p0-config.xml 文档

 

 

<?xml version="1.0" encoding="UTF-8"?>


<c3p0-config>
	<default-config>
		<property name="driverClass">com.mysql.jdbc.Driver</property>
		<property name="jdbcUrl">jdbc:mysql://localhost:3306/day18</property>
		<property name="user">root</property>
		<property name="password">root</property>
		
		<property name="acquireIncrement">5</property>
		<property name="initialPoolSize">10</property>
		<property name="minPoolSize">5</property>
		<property name="maxPoolSize">20</property>
		
		
	</default-config>

	<named-config name="flx">
		<property name="driverClass">com.mysql.jdbc.Driver</property>
		<property name="jdbcUrl">jdbc:mysql://localhost:3306/day16</property>
		<property name="user">root</property>
		<property name="password">root</property>
		<property name="acquireIncrement">5</property>
		<property name="initialPoolSize">10</property>
		<property name="minPoolSize">5</property>
		<property name="maxPoolSize">20</property>
	</named-config>

</c3p0-config>
 

 

在里面编写好要连接的数据库信息!

 

Dao层

 

 

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

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.du.dao.UpfileDao;
import com.du.domain.Upfile;
import com.du.utils.JdbcUtils;

public class UpfileDaoImpl implements UpfileDao {
	/*
	 * create table upfile
		(
			id varchar(40) primary key,
			uuidname varchar(100) not null unique,
			filename varchar(100) not null,
			savepath varchar(255) not null,
			uptime datetime not null,
			description varchar(255),
			username varchar(40) not null
		);
	 */

	public void add(Upfile uf){
		QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());//dbutils
		String sql ="insert into upfile(id,uuidname,filename,savepath,uptime,description,username) values(?,?,?,?,?,?,?)";
		Object[] params={uf.getId(),uf.getUuidname(),uf.getFilename(),uf.getSavepath(),uf.getUptime(),uf.getDescription(),uf.getUsername()};
		try {
			qr.update(sql, params);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
	}
	
	public List getAll(){
		
		QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
		String sql="select * from upfile";
		try {
			List list =(List) qr.query(sql, new BeanListHandler(Upfile.class));
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}

	}
	
	public Upfile find(String id){
		
		QueryRunner qr =new QueryRunner(JdbcUtils.getDataSource());
		String sql="select * from upfile where id = ?";
		try {
			Upfile up=(Upfile) qr.query(sql, id, new BeanHandler(Upfile.class));
			return up;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
		
	}
}
 

 

 

注意:QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());

 

这句话是要导入dbutils包

 

 

service层:

 

 

import java.util.List;

import com.du.dao.UpfileDao;
import com.du.domain.Upfile;
import com.du.factory.DaoFactory;

public class BusinessServiceImpl {
	
	UpfileDao dao = DaoFactory.getInstance().getDao(UpfileDao.class);
	public void add(Upfile up){
		dao.add(up);
	}
	
	
	public List getAll(){
		
		return dao.getAll();
	}
	
	public Upfile find(String id){
		return dao.find(id);
	}

}
 

 

注意:这里用到了工厂模式

 

 

 

import java.io.IOException;
import java.util.Properties;

public class DaoFactory {
//单例模式
	
	private static Properties pro =new Properties();
	static{
		try {
			pro.load(DaoFactory.class.getClassLoader().getResourceAsStream("dao.properties"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private static DaoFactory instance = new DaoFactory();
	private DaoFactory(){
		
	}
	public static DaoFactory getInstance(){
		return instance;
	}
	
	public <T> T getDao(Class<T> interfaceClass){
		
		String name=interfaceClass.getSimpleName();//获取类的名字,之后在properties中获取相应的Dao
		String daoname=pro.getProperty(name);
		try {
			return (T) Class.forName(daoname).newInstance();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
	}
	
}
 

 

 

配置文件:

 

 

UpfileDao=com.du.dao.impl.UpfileDaoImpl
 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(jsp,servlet)