第四章综合实验.docx

上传人:b****5 文档编号:8306329 上传时间:2023-01-30 格式:DOCX 页数:11 大小:283.68KB
下载 相关 举报
第四章综合实验.docx_第1页
第1页 / 共11页
第四章综合实验.docx_第2页
第2页 / 共11页
第四章综合实验.docx_第3页
第3页 / 共11页
第四章综合实验.docx_第4页
第4页 / 共11页
第四章综合实验.docx_第5页
第5页 / 共11页
点击查看更多>>
下载资源
资源描述

第四章综合实验.docx

《第四章综合实验.docx》由会员分享,可在线阅读,更多相关《第四章综合实验.docx(11页珍藏版)》请在冰豆网上搜索。

第四章综合实验.docx

第四章综合实验

云南财经大学信息学院

实验报告册

学期:

2015年-2016年上半学期

课程名称:

JavaEE项目开发教程

班级:

计专14-1班

学号:

201405000016

姓名:

李鑫

2015年11月9日

实验名称

第四章综合实验:

搭建项目框架

实验目的

 

按照设计方案,搭建网上书店系统的整体项目框架

 

实验设备

安装了

(1)JDK运行平台:

jdk1.7.0_07和jre7

(2)Web服务器:

Tomcat7.0.32

(3)IDE工具:

MyEclipse10

(4)数据库:

MySQLServer5.5.27或者NavicatforMySQL并且环境整合完毕的电脑

 

实验过程

1.首先是工程结构如下图:

\

2.使用到的jar包如下:

\

3.我首先写的是web.xml,代码如下:

xmlversion="1.0"encoding="UTF-8"?

>

xsi="http:

//www.w3.org/2001/XMLSchema-instance"xsi:

schemaLocation="

ssiprj

--spring配置文件位置-->

contextConfigLocation

classpath:

conf/applicationContext*.xml

--将spring框架装载进我们的工程里-->

org.springframework.web.context.ContextLoaderListener

index.jsp

这里面主要是向工程里装载spring框架。

4.下面我在conf包下面建立constants.properties文件,这个文件用来放置一些经常会变化的配置信息,例如:

数据库配置信息、文件的路径等等,内容如下:

#db.driverClass=oracle.jdbc.driver.OracleDriver

#db.user=sharpxiajun

#db.password=sharpxiajun

#db.jdbcUrl=jdbc:

oracle:

thin:

@127.0.0.1:

1521:

orcl

db.driverClass=com.mysql.jdbc.Driver

db.user=root

db.password=root

db.jdbcUrl=jdbc\:

mysql\:

//localhost\:

3306/sq_xidi?

useUnicode\=true&characterEncoding\=utf-8

5.接下来写的是applicationContext.xml文件,这里首先是扫描spring组件和导入constants.properties文件的内容,接下来配置数据源(oracle或mysql),然后在spring里装载ibatis框架和定义ibatis操作模板类,最后定义事物管理,这些做javaee开发的工程师都很清楚,我就不具体讲解,实际使用copy就行了。

内容如下:

xmlversion="1.0"encoding="UTF-8"?

>

//www.springframework.org/schema/beans"

xmlns:

xsi="http:

//www.w3.org/2001/XMLSchema-instance"

xmlns:

context="http:

//www.springframework.org/schema/context"

xmlns:

aop="http:

//www.springframework.org/schema/aop"

xmlns:

tx="http:

//www.springframework.org/schema/tx"

xsi:

schemaLocation="http:

//www.springframework.org/schema/beanshttp:

//www.springframework.org/schema/beans/spring-beans-2.5.xsd/http:

//www.springframework.org/schema/context/http:

//www.springframework.org/schema/context/spring-context-2.5.xsdhttp:

//www.springframework.org/schema/txhttp:

//www.springframework.org/schema/tx/spring-tx-2.0.xsdhttp:

//www.springframework.org/schema/aophttp:

//www.springframework.org/schema/aop/spring-aop-2.0.xsd">

--扫描该路径下的spring组件-->

component-scanbase-package=".sharpxiajun"/>

--读取资源文件-->

classpath:

conf/constants.properties

--配置数据源-->

--

-->

classpath:

conf/SqlMapConfig.xml

6.接着我在文件路径cn/com/sharpxiajun/dao/sqlmap/下编写了USERS.xml配置文件,内容如下:

xmlversion="1.0"encoding="UTF-8"?

>

DOCTYPEsqlMapPUBLIC"-//ibatis.apache.org//DTDSQLMap2.0//EN""http:

//ibatis.apache.org/dtd/sql-map-2.dtd">

selectt.username,t.password,t.enabledfromuserst

 

里面我只写了一个查询方法,parameterClass="java.util.Map"代表传入参数是map,resultClass="java.util.HashMap"表示返回的结果是一个map,这里正好应用了我前一篇博文里面谈到的各个逻辑层用map来做为传输的介质。

7.然后我在路径.sharpxiajun.dao下编写接口UsersDao,内容如下:

package.sharpxiajun.dao;

importjava.util.List;

importjava.util.Map;

publicinterfaceUsersDao{

publicstaticfinalStringQUERY_USERS_SQL="USERS.queryUserList";

publicList>queryUserList(Mapmap)throwsException;

}

在路径.sharpxiajun.dao.impl下实现了UsersDaoImpl接口,代码如下:

package.sharpxiajun.dao.impl;

importjava.util.List;

importjava.util.Map;

importorg.springframework.beans.factory.annotation.Autowired;

importorg.springframework.beans.factory.annotation.Qualifier;

importorg.springframework.context.annotation.Scope;

importorg.springframework.orm.ibatis.SqlMapClientTemplate;

importorg.springframework.stereotype.Repository;

import.sharpxiajun.dao.UsersDao;

@SuppressWarnings("unchecked")

@Scope("prototype")

@Repository("usersDao")

publicclassUsersDaoImplimplementsUsersDao{

@Autowired

@Qualifier("sqlMapClientTemplate")

privateSqlMapClientTemplatesqlMapClientTemplate=null;

publicList>queryUserList(Mapmap)

throwsException{

returnsqlMapClientTemplate.queryForList(QUERY_USERS_SQL,map);

}

}

@Scope("prototype"):

多线程,生成多个实例。

@Repository("usersDao")将UsersDaoImpl注册为spring的bean对象,Repository标签只用于dao层,因为Repository标签里面还封装了dao层抛出的异常类型。

8.写好了dao层我接下来编写了SqlMapConfig.xml文件,内容如下:

xmlversion="1.0"encoding="UTF-8"?

>

DOCTYPEsqlMapConfigPUBLIC"-//ibatis.apache.org//DTDSQLMapConfig2.0//EN""http:

//ibatis.apache.org/dtd/sql-map-config-2.dtd">

maxRequests="64"maxSessions="20"maxTransactions="10"

useStatementNamespaces="true"/>

9.最后我们编写该UserDao单元测试类,代码如下:

package.sharpxiajun.junittest.dao;

 

importjava.util.HashMap;

importjava.util.List;

importjava.util.Map;

importorg.junit.After;

importorg.junit.Before;

importorg.junit.Test;

importorg.junit.runner.RunWith;

importorg.springframework.beans.factory.annotation.Autowired;

importorg.springframework.test.context.ContextConfiguration;

importorg.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;

importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner;

importorg.springframework.test.context.transaction.TransactionConfiguration;

import.sharpxiajun.dao.UsersDao;

importjunit.framework.TestCase;

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations={"classpath:

conf/applicationContext.xml"})

@TransactionConfiguration(defaultRollback=false)

publicclassUsersDaoImplTestextendsAbstractTransactionalJUnit4SpringContextTests{

@Autowired

privateUsersDaousersDao;

publicUsersDaoImplTest()

{

System.out.println("初始化测试类....");

}

@Before

publicvoidsetUp()throwsException

{

System.out.println("测试开始....");

}

@After

publicvoidtearDown()throwsException

{

System.out.println("测试结束!

!

");

}

@Test

publicvoidtestQueryUserList()

{

Mapmap=newHashMap();

try{

List>list=usersDao.queryUserList(map);

System.out.println(list);

}catch(Exceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}

}

}

运行结果如下:

初始化测试类....

2011-10-923:

22:

22org.springframework.beans.factory.xml.XmlBeanDefinitionReaderloadBeanDefinitions

信息:

LoadingXMLbeandefinitionsfromclasspathresource[conf/applicationContext.xml]

2011-10-923:

22:

23org.springframework.context.support.AbstractApplicationContextprepareRefresh

信息:

Refreshingorg.springframework.context.support.GenericApplicationContext@290fbc:

startupdate[SunOct0923:

22:

23CST2011];rootofcontexthierarchy

2011-10-923:

22:

23org.springframework.core.io.support.PropertiesLoaderSupportloadProperties

信息:

Loadingpropertiesfilefromclasspathresource[conf/constants.properties]

2011-10-923:

22:

23org.springframework.beans.factory.support.DefaultListableBeanFactorypreInstantiateSingletons

信息:

Pre-instantiatingsingletonsinorg.springframework.beans.factory.support.DefaultListableBeanFactory@d16fc1:

definingbeans[usersDao,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,propertyConfigurer,myDataSource,sqlMapClient,sqlMapClientTemplate,transactionManager];rootoffactoryhierarchy

2011-10-923:

22:

23org.springframework.test.context.transaction.TransactionalTestExecutionListenerstartNewTransaction

信息:

Begantransaction

(1):

transactionmanager[org.springframework.jdbc.datasource.DataSourceTransactionManager@1b7c76];rollback[false]

测试开始....

[{enabled=false,username=admin,password=admin},{enabled=false,username=test,password=test}]

测试结束!

!

2011-10-923:

22:

23org.springframework.test.context.transaction.Transaction

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 工作范文 > 行政公文

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1