SSM框架整合
SSM(Spring+SpringMVC+MyBatis)框架集由Spring、MyBatis兩個開源框架整合而成(SpringMVC是Spring中的部分內(nèi)容)膏执。
Spring
Spring就像是整個項目中裝配bean的大工廠驻售,在配置文件中可以指定使用特定的參數(shù)去調(diào)用實體類的構造方法來實例化對象。也可以稱之為項目中的粘合劑更米。
Spring的核心思想是IoC(控制反轉)欺栗,即不再需要程序員去顯式地`new`一個對象,而是讓Spring框架幫你來完成這一切。
SpringMVC
SpringMVC在項目中攔截用戶請求迟几,它的核心Servlet即DispatcherServlet承擔中介或是前臺這樣的職責消请,將用戶請求通過HandlerMapping去匹配Controller,Controller就是具體對應請求所執(zhí)行的操作类腮。SpringMVC相當于SSH框架中struts臊泰。
Mybatis
Mybatis是對jdbc的封裝,它讓數(shù)據(jù)庫底層操作變的透明蚜枢。Mybatis的操作都是圍繞一個sqlSessionFactory實例展開的因宇。Mybatis通過配置文件關聯(lián)到各實體類的Mapper文件,Mapper文件中配置了每個類對數(shù)據(jù)庫所需進行的sql語句映射祟偷。在每次與數(shù)據(jù)庫交互時察滑,通過sqlSessionFactory拿到一個sqlSession,再執(zhí)行sql命令修肠。
頁面發(fā)送請求給控制器贺辰,控制器調(diào)用業(yè)務層處理邏輯,邏輯層向持久層發(fā)送請求嵌施,持久層與數(shù)據(jù)庫交互饲化,后將結果返回給業(yè)務層,業(yè)務層將處理邏輯發(fā)送給控制器吗伤,控制器再調(diào)用視圖展現(xiàn)數(shù)據(jù)吃靠。
(摘自百度)
現(xiàn)在使用eclipse作為開發(fā)工具;maven作為項目管理工具足淆;使用逆向工程連接數(shù)據(jù)庫巢块,生成表對應的實體類、mapper接口以及接口映射的sql文件巧号,搭建一個SSM純凈版的web項目族奢,供大家學習交流。
步驟如下:
1.???導入逆向工程丹鸿,更改相關配置越走,運行逆向工程項目,生成項目實體類靠欢、mapper接口以及接口映射的sql文件廊敌;
2.???創(chuàng)建maven工程的web項目,通過pom文件導入和管理相應的jar包门怪;
3.???編寫web.xml骡澈,在web.xml中加載Spring配置文件(非必須,可整合在spring整合mybatis的配置文件中)薪缆、spring整合mybatis的配置文件秧廉、Mybatis的配置文件(非必須,可整合在spring整合mybatis的配置文件中)拣帽、SpringMVC配置文件疼电,以及數(shù)據(jù)源文件和日志文件的配置;
4.???編寫jsp頁面代碼减拭;
5.???編寫后端java代碼蔽豺,如controller、service層(dao層逆向工程生成)拧粪;
6.???項目運行并訪問修陡。
現(xiàn)在按以上步驟進行項目搭建
一、導入逆向工程可霎,更改相關配置魄鸦,運行逆向工程項目,生成項目實體類癣朗、mapper接口以及接口映射的sql文件拾因;
逆向工程可在mybatis官網(wǎng)下載,如下
相關配置的修改
運行后的目錄結構
二旷余、創(chuàng)建maven工程的web項目绢记,通過pom文件導入和管理相應的jar包;
創(chuàng)建項目后的目錄結構
pom文件依賴的管理正卧,以及tomcat插件蠢熄、編譯插件的配置
pom.xml配置,相關jar的導入和管理炉旷,tomcat插件和編譯插件
? ? <build>
? ? ? ?<finalName>maven01</finalName>
? ? ? ?<plugins>
? ? ? ? ?? <plugin>
? ? ? ? ?? ? ? <groupId>org.apache.tomcat.maven</groupId>
????????????????????? <artifactId>tomcat7-maven-plugin</artifactId>
????????????????????? <version>2.2</version>
????????????????????? <configuration>
??????????????????????????? <port>8080</port>
??????????????????????????? <path>/ssmtest001</path>
????????????????????? </configuration>
? ? ? ? ?? </plugin>
? ? ? ? ?? <plugin>
????????????????????? <groupId>org.apache.maven.plugins</groupId>
????????????????????? <artifactId>maven-compiler-plugin</artifactId>
????????????????????? <version>3.2</version>
???????????????? ?? <configuration>
???????????????? ?? ? ? <source>1.7</source>
???????????????? ?? ? ? <target>1.7</target>
???????????????? ?? </configuration>
???????????????? </plugin>
? ? ? ?</plugins>
? ? </build>
</project>
三签孔、編寫web.xml,在web.xml中加載Spring配置文件(非必須窘行,可整合在spring整合mybatis的配置文件中)骏啰、spring整合mybatis的配置文件、Mybatis的配置文件(非必須抽高,可整合在spring整合mybatis的配置文件中)判耕、SpringMVC配置文件,以及數(shù)據(jù)源文件和日志文件的配置翘骂;
web.xml的配置壁熄,注意加載配置文件時classpath的配置??
? <!-- Spring和mybatis的配置文件 -->
? <context-param>
????? <param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-mybatis.xml,classpath:spring/applicationContext.xml</param-value>
? </context-param>
? <!-- Spring裝配xml文件監(jiān)聽器 -->
? <listener>
????? <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
? </listener>
? <context-param>
? ? ?<param-name>log4jConfigLocation</param-name>
? ? ?<param-value>classpath:log4j.properties</param-value>
? </context-param>
? <context-param>
? ? ?<param-name>log4jRefreshInterval</param-name>
? ? ?<param-value>10000</param-value>
? </context-param>
? <listener>
? ? ?<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
? </listener>
? <filter>
????? <filter-name>encoding</filter-name>
??<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
????? <init-param>
?????????? <param-name>encoding</param-name>
?????????? <param-value>UTF-8</param-value>
????? </init-param>
? </filter>
? <filter-mapping>
????? <filter-name>encoding</filter-name>
????? <url-pattern>/*</url-pattern>
? </filter-mapping>
? <servlet>
????? <servlet-name>springmvc</servlet-name>
??<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
????? <init-param>
?????????? <param-name>contextConfigLocation</param-name>
?????????? <param-value>classpath:springmvc/springmvc.xml</param-value>
????? </init-param>
????? <load-on-startup>1</load-on-startup>
? </servlet>
? <servlet-mapping>
????? <servlet-name>springmvc</servlet-name>
????? <url-pattern>/</url-pattern>
? </servlet-mapping>
spring配置文件applicationContent.xml
????? <!--掃描包Service實現(xiàn)類 -->
? ? <context:component-scanbase-package="com.it.service"></context:component-scan>
</beans>
spring整合mybatis的配置文件spring-mybatis.xml
????? <!--加載數(shù)據(jù)庫配置文件 -->
????? <context:property-placeholderlocation="classpath:db.properties"/>
????? <!--引入配置文件,方式二 -->??
? ? <!-- <beanid="propertyConfigurer"??
? ? ? ?class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">??
? ? ? ? <propertyname="location" value="classpath:db.properties"/>??
? ? </bean>? -->
????? <!--配置連接池 -->
????? <beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource"
?????????? destroy-method="close">
?????????? <propertyname="driverClassName" value="${jdbc.driver}" />
?????????? <propertyname="url" value="${jdbc.url}" />
?????????? <propertyname="username" value="${jdbc.username}" />
?????????? <propertyname="password" value="${jdbc.password}" />
?????????? <!--初始化連接大小 -->??
? ? ? ? <propertyname="initialSize"value="${initialSize}"></property>??
? ? ? ? <propertyname="maxActive"value="${maxActive}"></property>??
? ? ? ? <propertyname="maxIdle"value="${maxIdle}"></property>??
? ? ? ? <propertyname="minIdle"value="${minIdle}"></property>??
? ? ? ? <propertyname="maxWait"value="${maxWait}"></property>?
????? </bean>
????? <!--配置SqlSessionFactory mybatis工廠對象 -->
????? <beanid="sqlSessionFactoryBean"class="org.mybatis.spring.SqlSessionFactoryBean">
?????????? <propertyname="dataSource" ref="dataSource"/>
?????????? <propertyname="configLocation"value="classpath:mybatis/sqlMapConfig.xml"/>
?????????? <!--自動掃描mapping.xml文件 -->??
? ? ? ? <propertyname="mapperLocations"value="classpath:com/it/mapper/*.xml"></property>?
????? </bean>
????? <beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer">
?????????? <propertyname="basePackage" value="com.it.mapper"/>
????? </bean>
????? <!--配置事務 -->
????? <beanid="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
?????????? <propertyname="dataSource" ref="dataSource" />
????? </bean>
????? <!--開啟注解事務 -->
????? <tx:annotation-driventransaction-manager="transactionManager"/>
mybatis的配置文件SqlMapConfig.xml??
????? <!--打印SQL語句 -->
? ? <settings>
? ? ? ? <settingname="logImpl" value="STDOUT_LOGGING"/>
? ? </settings>
????? <typeAliases>
?????????? <!--指定掃描包,會把包內(nèi)所有的類都設置別名碳竟,別名的名稱就是類名草丧,大小寫不敏感 -->
?????????? <packagename="com.it.pojo"/>
????? </typeAliases>
? ? <!-- <plugins>
? ? ? ? <plugininterceptor="com.github.pagehelper.PageInterceptor">
?分頁參數(shù)合理化
? ? ? ? ?? <property name="reasonable" value="true"/>
? ? ? ?</plugin>
? ? </plugins> -->
springmvc的配置文件,springmvc.xml? ?? ??
? ? ? ?<context:component-scan base-package="com.it.controller"/>
<!--? ? ? ??<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>-->
<!--? ? ? ??<beanclass="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>-->
? ? ? ?<mvc:annotation-driven />
? ? ? ? <beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver">
? ? ? ??<property name="prefix"value="/WEB-INF/jsp/"/>
? ? ? ??<property name="suffix"value=".jsp"/>
? ? ? ?</bean>
????? ?? <bean id="multipartResolver"? ??
? ? ? ?class="org.springframework.web.multipart.commons.CommonsMultipartResolver">???
????? ?? ? ? <property name="defaultEncoding" value="utf-8"/>? ??
????? ?? ? ? <property name="maxUploadSize"value="10485760000" />? ??
????? ?? ? ? <property name="maxInMemorySize"value="40960" />? ??
????? ?? ? ? <property name="resolveLazily"value="true"/>
????? ?? </bean>? ?
db.properties數(shù)據(jù)源的配置
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/xxxpassword=root
#定義初始連接數(shù) ?
initialSize=0 ?#定義最大連接數(shù) ?
maxActive=20 ?#定義最大空閑 ?
maxIdle=20 ?#定義最小空閑 ?
minIdle=1 ?#定義最長等待時間 ?
maxWait=60000?
log4j.properties日志文件的配置
log4j.rootLogger=INFO,Console,File?
#定義日志輸出目的地為控制臺 ?
log4j.appender.Console=org.apache.log4j.ConsoleAppender?
log4j.appender.Console.Target=System.out?
#可以靈活地指定日志輸出格式莹桅,下面一行是指定具體的格式 ?
log4j.appender.Console.layout= org.apache.log4j.PatternLayout ?
log4j.appender.Console.layout.ConversionPattern=[%c]- %m%n ?
#文件大小到達指定尺寸的時候產(chǎn)生一個新的文件 ?
log4j.appender.File =org.apache.log4j.RollingFileAppender ?
#指定輸出目錄 ?
log4j.appender.File.File= logs/ssm.log ?
#定義文件最大大小 ?
log4j.appender.File.MaxFileSize= 10MB ?
#?輸出所以日志昌执,如果換成DEBUG表示輸出DEBUG以上級別日志 ?
log4j.appender.File.Threshold= ALL ?
log4j.appender.File.layout= org.apache.log4j.PatternLayout ?
log4j.appender.File.layout.ConversionPattern=[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n
四、編寫后端代碼
編寫controller
項目結構
五、項目啟動懂拾,訪問
啟動采用maven中配置的tomcat插件啟動
項目右鍵煤禽,執(zhí)行,run as,maven configuration...
輸入命令岖赋,tomcat:run
控制臺日志輸出
訪問
項目下載鏈接
鏈接:https://pan.baidu.com/s/1PMW_b7TszulnTTD0dR_afw?
提取碼:x6n0?