整合思路 將工程的三層結構中的JavaBean分別使用Spring容器(通過XML方式)進行管理。 整合持久層mapper,包括數據源、會話工程及mapper代理對象的整合; 整合業務層Service,包括事務及service的bean的配置; 整合表現層Controller,直接使用springm ...
整合思路
將工程的三層結構中的JavaBean分別使用Spring容器(通過XML方式)進行管理。
- 整合持久層mapper,包括數據源、會話工程及mapper代理對象的整合;
- 整合業務層Service,包括事務及service的bean的配置;
- 整合表現層Controller,直接使用springmvc的配置;
- Web.xml載入spring容器(包含多個XML文件);
Spring 核心配置文件:
- applicationContext-dao.xml
- applicationContext-service.xml
- springmvc.xml
需求分析
表現層
請求URL:/queryItem
請求參數:無
請求返回值:ModelAndView指定Model和View(item-list.jsp)
Request域(Model):key為itemList
業務層
業務處理邏輯(需求分析):實現商品列表的查詢
持久層
只針對錶進行查詢
工程搭建
依賴包
- spring(包括springmvc)
- mybatis
- mybatis-spring整合包
- 資料庫驅動
- 第三方連接池
- JSTL
- servlet-api
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.cyb.ssm</groupId> <artifactId>ssm-project</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <dependencies> <!-- 持久層依賴開始 --> <!-- spring ioc組件需要的依賴包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.2.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.2.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>5.2.1.RELEASE</version> </dependency> <!-- spring 事務管理和JDBC依賴包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>5.2.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.2.1.RELEASE</version> </dependency> <!-- mysql驅動 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.18</version> </dependency> <!-- dbcp連接池依賴包 --> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>javax.annotation</groupId> <artifactId>javax.annotation-api</artifactId> <version>1.3.1</version> </dependency> <!-- mybatis依賴 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.3</version> </dependency> <!-- mybatis-spring整合依賴 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.3</version> </dependency> <!-- 持久層依賴結束 --> <!-- 業務層依賴開始 --> <!-- 基於AspectJ的aop依賴 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>5.2.1.RELEASE</version> </dependency> <dependency> <groupId>aopalliance</groupId> <artifactId>aopalliance</artifactId> <version>1.0</version> </dependency> <!-- 業務層依賴結束 --> <!-- 表現層依賴開始 --> <!-- spring MVC依賴包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.2.1.RELEASE</version> </dependency> <!-- jstl --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- servlet --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <!-- 表現層依賴結束 --> <!-- spring 單元測試組件包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.2.1.RELEASE</version> <scope>test</scope> </dependency> <!-- 單元測試Junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <!-- 配置Maven的JDK編譯級別 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.2</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <port>8080</port> </configuration> </plugin> <!-- tomcat依賴包 --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> </plugin> </plugins> </build> </project>
工程整合(配置文件)
applicationContext-dao.xml(持久層)
路徑:src/main/resources/spring/applicationContext-dao.xml
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" 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"> <!-- 讀取java配置文件,替換占位符數據 --> <context:property-placeholder location="classpath:db.properties"/> <!-- 配置數據源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${db.driverClassName}"></property> <property name="url" value="${db.url}"></property> <property name="username" value="${db.username}"></property> <property name="password" value="${db.password}"></property> </bean> <!-- 配置SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 註入dataSource --> <property name="dataSource" ref="dataSource"></property> <!-- mybatis批量別名配置 --> <property name="typeAliasesPackage" value="com.cyb.ssm.po"></property> </bean> <!-- 批量代理對象的生成 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 指定需要生成代理的介面所在的包名 --> <property name="basePackage" value="com.cyb.ssm.mapper"></property> </bean> </beans>
applicationContext-service.xml(業務層)
路徑:src/main/resources/spring/applicationContext-service.xml
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" 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"> <!-- 掃描業務bean --> <context:component-scan base-package="com.cyb.ssm.service"></context:component-scan> </beans>
applicationContext-tx.xml(事務)
路徑:src/main/resources/spring/applicationContext-tx.xml
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" 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"> <!-- 配置平臺事務管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 事務通知 --> <!-- tx:advice:對應的處理器類是TransactionInterceptor類(實現了MethodInterceptor) --> <!-- TransactionInterceptor類實現事務是通過transaction-manager屬性指定的值進行事務管理 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <!-- 設置事務管理信息 --> <tx:attributes> <!-- 增刪改使用REQUIRED事務傳播行為 --> <!-- 查詢使用read-only --> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="insert*" propagation="REQUIRED" /> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="create*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="remove*" propagation="REQUIRED" /> <tx:method name="del*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="modify*" propagation="REQUIRED" /> <tx:method name="edit*" propagation="REQUIRED" /> <tx:method name="query*" read-only="true"/> <tx:method name="find*" read-only="true"/> <tx:method name="select*" read-only="true"/> </tx:attributes> </tx:advice> <!-- 基於AspectJ+XML方式實現聲明式事務 --> <aop:config> <!-- aop:advisor標簽使用的是傳統spring aop開發方式實現的 --> <!-- spring已經實現了該增強功能,spring使用的是實現MethodInterceptor介面的方式實現的 --> <aop:advisor advice-ref="txAdvice" pointcut="execution(* *..*.*ServiceImpl.*(..))" /> </aop:config> </beans>
springmvc.xml(掃描)
路徑:src/main/resources/spring/springmvc.xml
<?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:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 處理器類的掃描 --> <context:component-scan base-package="com.cyb.ssm.controller"></context:component-scan> <mvc:annotation-driven /> <!-- 顯示配置視圖解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
db.properties
路徑:src/main/resources/db.properties
db.driverClassName=com.mysql.cj.jdbc.Driver db.url=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai db.username=root db.password=root
log4j.properties
路徑:src/main/resources/log4j.properties
#dev env [debug] product env [info]
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
提示
POJO是逆向工程自動生成的,各自視情況而定!!!!
包
com.cyb.ssm.controller
路徑:src/main/java
ItemController.java
package com.cyb.ssm.controller; import java.util.ArrayList; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.stereotype.Service; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import com.cyb.ssm.po.Item; import com.cyb.ssm.service.ItemService; @Controller public class ItemController { @Autowired private ItemService Service; @RequestMapping("queryItem") public ModelAndView queryItem() { List<Item> itemList = Service.queryItemList(); ModelAndView mvAndView = new ModelAndView(); mvAndView.addObject("itemList", itemList); mvAndView.setViewName("item/item-list"); return mvAndView; } }
com.cyb.ssm.mapper
路徑:src/main/java
ItemMapper.java
package com.cyb.ssm.mapper; import com.cyb.ssm.po.Item; import com.cyb.ssm.po.ItemExample; import java.util.List; import org.apache.ibatis.annotations.Param; public interface ItemMapper { int countByExample(ItemExample example); int deleteByExample(ItemExample example); int deleteByPrimaryKey(Integer id); int insert(Item record); int insertSelective(Item record); List<Item> selectByExampleWithBLOBs(ItemExample example); List<Item> selectByExample(ItemExample example); Item selectByPrimaryKey(Integer id); int updateByExampleSelective(@Param("record") Item record, @Param("example") ItemExample example); int updateByExampleWithBLOBs(@Param("record") Item record, @Param("example") ItemExample example); int updateByExample(@Param("record") Item record, @Param("example") ItemExample example); int updateByPrimaryKeySelective(Item record); int updateByPrimaryKeyWithBLOBs(Item record); int updateByPrimaryKey(Item record); }
OrdersMapper.java
package com.cyb.ssm.mapper; import com.cyb.ssm.po.Orders; import com.cyb.ssm.po.OrdersExample; import java.util.List; import org.apache.ibatis.annotations.Param; public interface OrdersMapper { int countByExample(OrdersExample example);