ssm(三)
2019獨角獸企業(yè)重金招聘Python工程師標準>>>
(一)
項目分層:
表現(xiàn)層:springmvc(controller)
業(yè)務層:service
持久層mybatis(mapper)
通過spring框架整合的思路:
1.controller/service/mapper都是javaBean;
2.整合表現(xiàn)層,把表現(xiàn)層的對象讓spring管理,實現(xiàn)表現(xiàn)層中調用業(yè)務層的對象。
3.整合業(yè)務層,把業(yè)務層對象和事務讓spring管理,實現(xiàn)在業(yè)務層中調用持久層對象。
4.整合持久層,把持久層的對象讓spring管理,實現(xiàn)數(shù)據(jù)庫的crud操作。
(二)
風騷的操作
1.準備環(huán)境:jdk/ide/database/web 容器(tomcat)
2.準備數(shù)據(jù)
3.創(chuàng)建項目:復制黏貼請注意:properties-web project setting
4.配置pom.xml,加入依賴:
? ? mybatits框架包;spring-webmvc框架包;mybatis-spring整合包;數(shù)據(jù)庫驅動包(dbcp);jstl標簽庫包;log4j日志包
<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.buff</groupId><artifactId>ssm</artifactId><version>0.0.1-SNAPSHOT</version><packaging>war</packaging><name>ssm Maven Webapp</name><url>http://maven.apache.org</url><properties><!-- spring版本號 --><spring.version>4.3.8.RELEASE</spring.version><!-- aspectj版本號 --><aspectj.version>1.6.12</aspectj.version><!-- jstl標簽版本 --><jstl.version>1.2</jstl.version><!-- mybatis版本號 --><mybatis.version>3.4.5</mybatis.version><!-- mybatis-spring整合包版本 --><mybatis.spring.version>1.3.1</mybatis.spring.version><!-- mysql版本號 --><mysql.version>5.1.30</mysql.version><!-- dbcp數(shù)據(jù)源連接池jar包 --><dbcp.version>1.2.2</dbcp.version><!-- log4j日志包版本 --><slf4j.version>1.7.7</slf4j.version><log4j.version>1.2.17</log4j.version><!-- commons-lang版本 --><commons.lang.version>2.6</commons.lang.version></properties><dependencies><!-- springmvc依賴包 --><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aop</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>${aspectj.version}</version></dependency><!-- JSTL標簽類 --><dependency><groupId>jstl</groupId><artifactId>jstl</artifactId><version>${jstl.version}</version></dependency><!-- mybatis核心包 --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>${mybatis.version}</version></dependency><!-- mybatis-spring整合包 --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>${mybatis.spring.version}</version></dependency><!-- 導入Mysql數(shù)據(jù)庫鏈接jar包 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>${mysql.version}</version></dependency><!-- 導入dbcp數(shù)據(jù)源連接池jar包 --><dependency><groupId>commons-dbcp</groupId><artifactId>commons-dbcp</artifactId><version>${dbcp.version}</version></dependency><!-- 日志文件管理包 --><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>${log4j.version}</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>${slf4j.version}</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>${slf4j.version}</version></dependency><dependency><groupId>commons-lang</groupId><artifactId>commons-lang</artifactId><version>${commons.lang.version}</version></dependency><!-- jsp依賴包,只在編譯時需要 --><dependency><groupId>javax.servlet</groupId><artifactId>jsp-api</artifactId><version>2.0</version><scope>provided</scope></dependency></dependencies><build><finalName>ssm</finalName><plugins><plugin><groupId>org.apache.tomcat.maven</groupId><artifactId>tomcat7-maven-plugin</artifactId><version>2.1</version><configuration><!-- tomcat 的端口號 --><port>8080</port><!-- 訪問應用的路徑 --><path>/ssm</path><!-- URL按UTF-8進行編碼,解決中文參數(shù)亂碼 --><uriEncoding>UTF-8</uriEncoding><!-- tomcat名稱 --><server>tomcat7</server></configuration></plugin></plugins></build> </project>5.準備配置文件
????sqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><!-- 配置自定義別名 --><typeAliases><!-- 包掃描方式配置別名 --><package name="com.buff.ssm.po"/></typeAliases></configuration>? ? springmvc.xml
配置包掃描controller;配置處理器映射器;配置處理器適配器;配置視圖解析器
<?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:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"><!-- 配置包掃描controller --><context:component-scan base-package="com.buff.ssm.controller"/><!-- 注解驅動的方式配置處理器映射器和處理器適配器 --><mvc:annotation-driven></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>? ? applicationContext-dao.xml
配置數(shù)據(jù)源對象(DataSource);?配置SqlSessionFactory;配置mapper掃描器(MapperScannerConfigurer)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"><!-- 加載db.properties文件 --><context:property-placeholder location="classpath:db.properties"/><!-- 配置數(shù)據(jù)源 --><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"><property name="driverClassName" value="${db.driverClassName}"/><property name="url" value="${db.url}" /><property name="username" value="${db.username}" /><property name="password" value="${db.password}" /><!-- 最大連接數(shù)量 --><property name="maxActive" value="${db.maxActive}"/><!-- 最小空閑連接 --><property name="minIdle" value="${db.minIdle}"/><!-- 最大空閑連接 --><property name="maxIdle" value="${db.maxIdle}"/><!-- 初始化連接數(shù) --><property name="initialSize" value="${db.initialSize}"/><!-- 超時等待時間以毫秒為單位 --><property name="maxWait" value="${db.maxWait}"/></bean><!-- 配置sqlSessionFactory --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 注入數(shù)據(jù)源對象 --><property name="dataSource" ref="dataSource"></property><!-- 加載mybatis主配置文件 --><property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml"></property></bean><!-- 配置mapper掃描器 --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><!-- 配置要掃描的包,說明:1.如果有多個包,在同一個父包下,配置父包即可2.不在同一個父包,以半角逗號進行分割:,--><property name="basePackage" value="com.buff.ssm.mapper"></property></bean></beans>? ? applicationContext-service.xml
配置包掃描service
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"><!-- 配置service掃描 --><context:component-scan base-package="com.buff.ssm.service"/></beans>? ? applicationContext-trans.xml
配置事務管理器(transactionManager);配置通知(txAdvice);配置切面(aop:config)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"><!-- 配置事務管理器 --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><!-- 指定數(shù)據(jù)源 --><property name="dataSource" ref="dataSource"/></bean><!-- 配置事務通知 --><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><!-- 配置傳播行為 --><tx:method name="add*" propagation="REQUIRED"/><tx:method name="insert*" propagation="REQUIRED"/><tx:method name="save*" propagation="REQUIRED"/><tx:method name="update*" propagation="REQUIRED"/><tx:method name="delete*" propagation="REQUIRED"/><tx:method name="find*" propagation="SUPPORTS" read-only="true"/><tx:method name="select*" propagation="SUPPORTS" read-only="true"/><tx:method name="get*" propagation="SUPPORTS" read-only="true"/><tx:method name="query*" propagation="SUPPORTS" read-only="true"/></tx:attributes></tx:advice><!-- 事務切面配置 --><aop:config><!-- 指定com.buff.ssm.service包下的任意類的任意方法,需要事務 --><aop:advisor advice-ref="txAdvice" pointcut="execution(* com.buff.ssm.service..*.*(..))"/></aop:config></beans>? ? db.properties
db.driverClassName=com.mysql.jdbc.Driver db.url=jdbc:mysql://127.0.0.1:3306/springmvc?characterEncoding=utf-8 db.username=root db.password=123456db.maxActive=10 db.minIdle=2 db.maxIdle=5 db.initialSize=5 db.maxWait=6000? ? log4j.properties
# Global logging configuration log4j.rootLogger=INFO, 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? ? web.xml
配置?加載spring配置文件(context-param);spring提供的監(jiān)聽器(ContextLoaderListener);前端控制器(DispatcherServlet)
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"><display-name>ssm</display-name><!-- 加載spring配置文件 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring/applicationContext-*.xml</param-value></context-param><!-- 配置spring監(jiān)聽器 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 配置前端控制器 --><servlet><servlet-name>ssm</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- 加載springmvc.xml --><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring/springmvc.xml</param-value></init-param><!-- 配置tomcat啟動就加載前端控制器 --><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>ssm</servlet-name><!--配置請求的url規(guī)則,說明:1.*.do,表示以.do結尾的請求進入前端控制器2./,表示所有請求都進入前端控制器--><url-pattern>*.do</url-pattern></servlet-mapping><!--默認的歡迎頁面 --><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list> </web-app>?
轉載于:https://my.oschina.net/u/3905833/blog/1845138
總結
- 上一篇: process credentials(
- 下一篇: MySQL MGR集群搭建