闫平平
写代码也要酷酷的!

导航

 

ssm整合

一、导包(pom.xml文件)

 

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <project xmlns="http://maven.apache.org/POM/4.0.0"
  3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5 <modelVersion>4.0.0</modelVersion>
  6 
  7 <groupId>aaa</groupId>
  8 <artifactId>test_maven_qy97_03</artifactId>
  9 <version>1.0-SNAPSHOT</version>
 10 <properties>
 11 <spring.version>4.3.18.RELEASE</spring.version>
 12 </properties>
 13 
 14 <dependencies>
 15 <!--spring的核心的jar包-->
 16 <dependency>
 17 <groupId>org.springframework</groupId>
 18 <artifactId>spring-core</artifactId>
 19 <version>${spring.version}</version>
 20 </dependency>
 21 
 22 <dependency>
 23 <groupId>org.springframework</groupId>
 24 <artifactId>spring-beans</artifactId>
 25 <version>${spring.version}</version>
 26 </dependency>
 27 
 28 <dependency>
 29 <groupId>org.springframework</groupId>
 30 <artifactId>spring-context</artifactId>
 31 <version>${spring.version}</version>
 32 </dependency>
 33 
 34 <dependency>
 35 <groupId>org.springframework</groupId>
 36 <artifactId>spring-tx</artifactId>
 37 <version>${spring.version}</version>
 38 </dependency>
 39 <!--spring mvc的jar包-->
 40 <dependency>
 41 <groupId>org.springframework</groupId>
 42 <artifactId>spring-web</artifactId>
 43 <version>${spring.version}</version>
 44 </dependency>
 45 
 46 <dependency>
 47 <groupId>org.springframework</groupId>
 48 <artifactId>spring-webmvc</artifactId>
 49 <version>${spring.version}</version>
 50 </dependency>
 51 <!--spring jdbc的jar包-->
 52 <dependency>
 53 <groupId>org.springframework</groupId>
 54 <artifactId>spring-jdbc</artifactId>
 55 <version>${spring.version}</version>
 56 </dependency>
 57 
 58 <!--mybatis的jar包-->
 59 <dependency>
 60 <groupId>org.mybatis</groupId>
 61 <artifactId>mybatis</artifactId>
 62 <version>3.4.6</version>
 63 </dependency>
 64 <!--spring 和mybatis整合的中间件jar包-->
 65 <dependency>
 66 <groupId>org.mybatis</groupId>
 67 <artifactId>mybatis-spring</artifactId>
 68 <version>1.3.2</version>
 69 </dependency>
 70 
 71 <!--orace的驱动包-->
 72 <dependency>
 73 <groupId>com.oracle</groupId>
 74 <artifactId>ojdbc6</artifactId>
 75 <version>6</version>
 76 </dependency>
 77 <!--spring 实现文件上传的jar包-->
 78 <dependency>
 79 <groupId>commons-io</groupId>
 80 <artifactId>commons-io</artifactId>
 81 <version>1.3.2</version>
 82 </dependency>
 83 
 84 <dependency>
 85 <groupId>commons-fileupload</groupId>
 86 <artifactId>commons-fileupload</artifactId>
 87 <version>1.2.1</version>
 88 </dependency>
 89 
 90 <!--解析json数据的jar包-->
 91 <dependency>
 92 <groupId>com.fasterxml.jackson.core</groupId>
 93 <artifactId>jackson-core</artifactId>
 94 <version>2.9.8</version>
 95 </dependency>
 96 <dependency>
 97 <groupId>com.fasterxml.jackson.core</groupId>
 98 <artifactId>jackson-annotations</artifactId>
 99 <version>2.9.8</version>
100 </dependency>
101 <dependency>
102 <groupId>com.fasterxml.jackson.core</groupId>
103 <artifactId>jackson-databind</artifactId>
104 <version>2.9.8</version>
105 </dependency>
106 <!--数据库连接池的jar包-->
107 
108 <dependency>
109 <groupId>commons-dbcp</groupId>
110 <artifactId>commons-dbcp</artifactId>
111 <version>1.4</version>
112 </dependency>
113 
114 <!--jstl的jar包-->
115 <dependency>
116 <groupId>jstl</groupId>
117 <artifactId>jstl</artifactId>
118 <version>1.2</version>
119 </dependency>
120 
121 </dependencies>
122 
123 
124 <build>
125 <resources>
126 <!--指定src main java下所有的目录下的xml文件都编译-->
127 <resource>
128 <directory>src/main/java</directory>
129 <includes>
130 <include>**/*.xml</include>
131 </includes>
132 </resource>
133 </resources>
134 </build>
135 
136 </project>

 

二、准备数据库表

 

三、准备pojo(java类)也就是实体类,对应数据库


四、准备jdbc.properties文件

 

 1 mysql:
 2 driver=com.mysql.jdbc.Driver
 3 url=jdbc:mysql://localhost:3306/ssm?characterEncoding=utf-8
 4 username=root
 5 password=admin
 6 
 7 oracle:
 8 driver=oracle.jdbc.driver.OracleDriver
 9 url=jdbc:oracle:thin:@localhost:1521:orcl
10 user=scott
11 password=tiger

 

五、applicationContext.xml配置文件(也就是spring主配置文件)

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4 xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
 5 xmlns:aop="http://www.springframework.org/schema/aop"
 6 
 7 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
 8 
 9 <!--第一步,扫描service -->
10 <context:component-scan base-package="com.aaa.ssm.service"></context:component-scan>
11 <!--第二步,加载jdbc.properties-->
12 <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
13 <!--第三步,创建dbcp数据源连接池-->
14 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
15 <property name="driverClassName" value="${driver}"></property>
16 <property name="url" value="${url}"></property>
17 <property name="username" value="${user}"></property>
18 <property name="password" value="${password}"></property>
19 </bean>
20 <!--第四步,创建mybatis的工厂类对象-->
21 <bean class="org.mybatis.spring.SqlSessionFactoryBean">
22 <!--指定数据源-->
23 <property name="dataSource" ref="dataSource"></property>
24 <!--加载mybatis的映射文件 在value中可以使用*号通配符-->
25 <property name="mapperLocations" value="classpath:com/aaa/ssm/dao/*.xml"></property>
26 </bean>
27 <!--第五步,在spring 的工厂中生成dao接口的实现类对象 -->
28 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
29 <!--指定要扫描哪个包下面所有的dao接口-->
30 <property name="basePackage" value="com.aaa.ssm.dao"></property>
31 </bean>
32 <!--第六步,创建spring的事物管理器-->
33 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
34 <property name="dataSource" ref="dataSource"></property>
35 </bean>
36 <!--第七步,声明以注解的方式配置声明式事物-->
37 <tx:annotation-driven transaction-manager="transactionManager" ></tx:annotation-driven>
38 
39 <!--第八步,配置事物-通知 -->
40 <tx:advice id="txAdvice" transaction-manager="transactionManager">
41 <tx:attributes>
42 <tx:method name="add*" propagation="REQUIRED"/>
43 <tx:method name="delete*" propagation="REQUIRED"/>
44 <tx:method name="find*" propagation="REQUIRED"/>
45 <tx:method name="edit*" propagation="REQUIRED"/>
46 </tx:attributes>
47 </tx:advice>
48 <!-- 第九步,配置事物-切面 -->
49 <aop:config>
50 <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.aaa.ssm.service.*.*(..))"/>
51 </aop:config>
52 
53 </beans>

 

步骤:

1、扫描service
2、加载jdbc
3、创建dbcp数据源连接池
4、创建mybatis的工厂类对象
5、在spring的工厂中生成dao接口的实现类对象
6、创建spring的事务管理器
7、声明以注解的方式配置声明式事务
8、配置事务通知
9、配置aop

 

六、spring-mvc配置文件

 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4 xmlns:context="http://www.springframework.org/schema/context"
 5 xmlns:mvc="http://www.springframework.org/schema/mvc"
 6 xsi:schemaLocation="http://www.springframework.org/schema/beans
 7 http://www.springframework.org/schema/beans/spring-beans.xsd
 8 http://www.springframework.org/schema/context
 9 http://www.springframework.org/schema/context/spring-context.xsd
10 http://www.springframework.org/schema/mvc
11 http://www.springframework.org/schema/mvc/spring-mvc.xsd">
12 <!-- 配置controller扫描-->
13 <context:component-scan base-package="com.aaa.ssm.controller"></context:component-scan>
14 <!-- 配置注解驱动 -->
15 <mvc:annotation-driven></mvc:annotation-driven>
16 <!-- 配置视图解析器 -->
17 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
18 <property name="prefix" value="/WEB-INF/jsp/"></property>
19 <property name="suffix" value=".jsp"></property>
20 </bean>
21 </beans>

 

步骤:

1:、配置controller扫描
2、配置注解驱动
3、配置视图解析器

 

七、dao层以及mapper实现


注:在applicationContext.xml中配置了自动扫描mapper,所有路径一定要保持一致,命名也是一样。

 

八、service层


根据所需要的业务逻辑来调用dao层的接口来实现业务处理。

 

九、controller层


控制层调用service来实现业务处理并且返回相对应的jsp页面以及重定向等。(注:jsp页面的路径问题,在spring-mvc配置文件中配置了视图解析的前缀和后缀,注意

 

十、写自己所需要的jsp


这个并不是必须在最后写的,也可以先写一些会用到的,也可以在写controller的过程中,需要哪个页面然后就写哪个就好。

 

十一、最后部署到服务器上,注意jar包也要同时部署到服务器。

 

posted on 2019-06-18 22:59  写代码也要酷酷的  阅读(2688)  评论(0编辑  收藏  举报