eclipse中SSH三大框架环境搭建<二>


前言

通过上一篇博客我们可以轻松搭建strtus2的环境,接下来由我来继续介绍spring的环境搭建以及spring注入的简单使用

相关链接

eclipse中SSH三大框架环境搭建<一>
eclipse中SSH三大框架环境搭建<三>
本例业务需求:将数据库一张表的信息通过JDBC查询出来并显示在页面中
  流程:action控制层–>service业务层–>dao数据层–>数据库

第一步:我们还是需要加入spring开发中所需要的jar包

  找到下载并解压好的spring文件,然后找到该文件下的libs目录下文件,我们可以将所有jar包复制到我们web项目下的lib目录下

注意:1>.javadoc.jar文档和.sources.jar源码我们可以不拷贝到我们的项目中,如果想加深自己的理解小编建议大家看看源码(毕竟这是第一手资料)
   2>我们还需要加入commons-logging.jar包。用来记录程序运行时的活动的日志记录。该文件在struts2文件中app目录下的struts2-showcase.war包目录下的WEB-INF下的lib中

3>我们还需要加入struts2中的一个插件包struts2-spring-plugin-2.3.30.jar(经常容易忘记)
4>由于本例我们还需要操作mySql数据库因此还需加入数据库驱动包mysql-connector-java-5.1.39-bin.jar

第二步:由于本例用到了struts所以还是需要先配置strtus.xml文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
<!-- 告知Struts2运行时使用Spring来创建对象 -->
<constant name="struts.objectFactory" value="spring" />
<package name="001pck" extends="struts-default">
<action name="Index" class="myIndexAction">
<result name="success">/WEB-INF/jsp/index.jsp</result>
</action>

</package>
</struts>

注意:这里的class并没有引用一个具体的类而是取了一个别名,接下来再由spring给它注入

第三步:配置applicationContext.xml文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
<!-- 类似于财务部门一样,类就是钱,所有需要类的实例都由srping去管理, pojo除外-->
<bean id="myIndexAction" class="action.IndexAction" scope="prototype">
<property name="index" ref="myService1"></property>
</bean>
<!-- index = new IndexServiceImpl() -->
<bean id="myDao1" class="dao.BookCardDaoImpl" scope="prototype">
<property name="c" ref="myConnection1"></property>
</bean>

<bean id="myService1" class="service.IndexServiceImpl" scope="prototype">
<property name="bcd" ref="myDao1"></property>
</bean>

<bean id="myConnection1" class="util.MyConnection_mysql" scope="prototype"></bean>

<bean id="myConnection2" class="util.MyConnection_sqlserver" scope="prototype"></bean>

</beans>

在这里我介绍一下这份配置文件的相关信息
  1 头文件我们一般从官方模板中拷贝过来,需要注意一下版本号
  2 bean标记就是我们需要给项目中的每一个实例配置一个bean(pojo除外),当哪一层需要调用时,spring会帮我们注入。
  3 bean标记中id属性为在本文件中起一标识作用用来区分其他bean,class属性的值便是当前bean引用了哪一个类 ,scope属性的值为prototype为非单例的意思
  4 bean标记中的子标记property便是当前这个类中需要注入的属性,name属性值为我们java类中的属性名,ref属性值为当前类需要引用哪一个实现类。
  本例中我们可以看到dao中引用了myConnection1也就是引用了mysql的连接,如此一来便可以轻松切换mySQL和sqlServer两个数据库,而并不需要改动我们的任何逻辑代码。
  下面我给出一个数据层dao的样例代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
public class BookCardDaoImpl implements BookCardDao {

//只做属性的申明,不写死具体的实现
private MyConnection c;

//提供一个set方法spring会自动注入
public void setC(MyConnection c) {
this.c = c;
}

@Override
public List<BookCard> getAllCardInfo() {

// 第一步:获取数据库连接
Connection conn = c.getConnection();

// 第二步:查询数据库
// (操作JDBC,需要sql语句,需要执行sql语句对象,需要执行sql语句后结果集合)
String sql = "select * from BookCard";// 需要执行的sql语句
PreparedStatement stmt = null;// 执行sql语句对象
ResultSet rs = null;// 执行sql语句后的结果集
try {
stmt = conn.prepareStatement(sql);
rs = stmt.executeQuery();
} catch (Exception e) {
e.printStackTrace();
}

// 第三步
// 拿到rs后,执行一个遍历以及封装
List<BookCard> myBookCardList = new ArrayList<BookCard>();
try {
while (rs.next()) {
// 风格:实体类是一个pojo对象,一般直接new就行
BookCard bc = new BookCard();
// 进行数据库封装
bc.setCid(rs.getInt("cid"));
bc.setName(rs.getString("name"));
bc.setSex(rs.getString("sex"));
bc.setCardDate(rs.getDate("cardDate"));
bc.setDeposit(rs.getBigDecimal("deposit"));
myBookCardList.add(bc);
}
} catch (Exception e1) {
e1.printStackTrace();
}

//关闭资源
try {
rs.close();
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}

// 第5步返回结果集List给客户端
return myBookCardList;
}

spring的环境在这一步已经是搭建完成了,我再来介绍一下spring的注入。

大家可以清晰的看到左边这幅图呢是传统的面向对象编程的写法,每一层都与另外一层紧密耦合,当一层出现问题,层层都需要变动,在软件工程中是大忌
  如图中,层与层之间都已经分离出来了,这里具体实现是通过在每一层都定义一个接口,层与层之间调用的都是接口并不关心是哪一个实现类,真正的实现类是谁不在代码中出现而是交给spring让他给我们注入。

坚持原创技术分享,您的支持将鼓励我继续创作!