小学期实践3

这三天主要是自己做一个库存管理系统,和之前老师带领我们做的客户信息系统相似。

先是做需求分析,库存信息,主要包括了商品编号、商品名称、商品数量、商品单价,主要的操作有查询、增加、删除、修改。

对此,先设计了一个product商品的数据库

接着要搭建环境ssh、框架、配置文件、做好和数据库的连接。主要注意的点有包的结构:各个actiondao接口,service接口,配置strutsBean中有:Java Class,hbm.xmlDAO中有DAO接口、DAO实现,Service中有Service接口、Service实现,Action要对应好ServiceDAO

配置application:实现myeclipse和数据库的连接

<!--数据库-配置数据连接池 -->
	<bean id="dataSource"
		class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName"
			value="com.mysql.jdbc.Driver">
		</property>
		<property name="url"
			value="jdbc:mysql://localhost:3306/dbssh">
		</property>
		<property name="username" value="root"></property>
		<property name="password" value="123456"></property>
		<property name="maxActive" value="100"></property>
		<property name="maxWait" value="500"></property>
		<property name="defaultAutoCommit" value="true"></property>
	</bean>

<!--sessionFactory配置与管理  -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.MySQLDialect
				</prop>
				<prop key="hibernate.show_sql">true</prop>
			</props>
		</property>
		<property name="mappingResources">
			<list>
				<value>com/crm/bean/Product.hbm.xml</value>
			</list>
		</property>
	</bean>

  

配置Product.hbm.xml:和数据库中的各项数据要对应

<hibernate-mapping>
	<class name="com.crm.bean.Product" table="product">
		<id name="id" type="java.lang.Integer" column="id" length="11">
			<generator class="increment"></generator>
		</id>
		<property name="productno" type="string" column="productno" length="20"/>
		<property name="productname" type="string" column="productname" length="20"/>
	    <property name="number" type="string" column="number" length="20"/>
	    <property name="price" type="string" column="price" length="20"/>
	</class>
</hibernate-mapping>

  

与之前的客户管理系统的操作功能相比,添加了一个新的功能,将数据库中的数据生成Excel表。

ProductService.java中:

	public InputStream getInputStream();
在ProductServiceImpl.java中:

	public InputStream getInputStream() {
		//Apache poi hssf对象
		HSSFWorkbook wb = new HSSFWorkbook();
		//创建sheet
		HSSFSheet sheet = wb.createSheet("sheet1");
		//表头开始
		//创建行
		HSSFRow row = sheet.createRow(0);
		//创建单元格 第一个单元格从零开始 第一列
		HSSFCell cell = row.createCell((short)0);
		//设置编码
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("商品编号");
		//第二列
		cell = row.createCell((short)1);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("商品名称");
		//第三列
		cell = row.createCell((short)2);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("商品数量");
		//第四列
		cell = row.createCell((short)3);
		cell.setEncoding(HSSFCell.ENCODING_UTF_16);
		cell.setCellValue("商品单价");
		//表头结束
		//查询数据库
		List<Product> list = this.productDao.findAllProduct();
		for(int i=0 ; i< list.size() ; ++i){
			Product product = list.get(i);
			//把数据放到表格中
			row = sheet.createRow(i+1);
			cell = row.createCell((short)0);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			cell.setCellValue(product.getProductno());
			
			//
			cell = row.createCell((short)1);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			cell.setCellValue(product.getProductname());
			
			cell = row.createCell((short)2);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			cell.setCellValue(product.getNumber());
			
			cell = row.createCell((short)3);
			cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			cell.setCellValue(product.getPrice());

		}
		//根据输出流,输出到文件中
		File file = new File("cust.xls");
		try {
			OutputStream os = new FileOutputStream(file);
			//输出写入到文件中
			wb.write(os);
			//关闭输出流
			os.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		InputStream is = null;
		try {
			is = new FileInputStream(file);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return is;
	}
在struts中配置:
        <!-- 导出excel -->
        <action name="generateExcel" class="generateExcelAction">
	    <result name="success" type="stream">
	    <param name="contentType">application/vnd.ms-excel</param>
	    <param name="contentDisposition">filename="AllProduct.xls"</param>
	    <param name="inputName">downloadFile</param>
	    </result>

  

applicationContext.xml中配置

	 <!-- 导出excel -->
    <bean id="generateExcelAction" class="com.crm.action.GenerateExcelAction" scope="prototype">
    <property name="excelService">
      <ref bean="productService"></ref>
    </property>
</bean>

  

jsp中:

 function funExcel(){
 	location.href='generateExcel.action';
 }
		<input width="100" type = "button"  value="生成excel" onClick="funExcel();"/>

  

最终达到以下效果,可以输出一个Excel

 

在这三天的实践中,遇到了一些问题都与之前相似,,主要是404和500的错误,都顺利的解决了。

原文地址:https://www.cnblogs.com/ao2chen/p/7122619.html