SpringMVC+Spring+Mybatis框架配置詳細(xì)步驟(eclipse普通版)_第1頁(yè)
SpringMVC+Spring+Mybatis框架配置詳細(xì)步驟(eclipse普通版)_第2頁(yè)
SpringMVC+Spring+Mybatis框架配置詳細(xì)步驟(eclipse普通版)_第3頁(yè)
SpringMVC+Spring+Mybatis框架配置詳細(xì)步驟(eclipse普通版)_第4頁(yè)
SpringMVC+Spring+Mybatis框架配置詳細(xì)步驟(eclipse普通版)_第5頁(yè)
已閱讀5頁(yè),還剩37頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

SSI框架搭建S+Spring3.1.2+編號(hào):SSI-SMVC3-S3-I3版本:V1.0級(jí)別:公開(kāi)編寫(xiě)時(shí)間:2016-02-17目錄1導(dǎo)言 11.1目的 11.2范圍 11.3說(shuō)明 12搭建SpringMVC 22.1搭建所需jar包 22.2其他依賴包 32.3搭建步驟 4創(chuàng)建項(xiàng)目 4導(dǎo)入jar包 6配置web.xml 7配置spring-servlet.xml 9配置applicationContext.xml 10配置perties 103整合mybatis 113.1整合所需jar包 113.2其他依賴包 113.3整合步驟 11導(dǎo)入jar包 11配置perties 12配置spring-dataSource.xml 123.3.4配置applicationContext.xml 15配置mybatis-config.xml 16創(chuàng)建實(shí)體model 17創(chuàng)建實(shí)例化dao 193.3.8創(chuàng)建業(yè)務(wù)服務(wù)service 21創(chuàng)建控制層controller 23頁(yè)面代碼 28啟動(dòng)項(xiàng)目 37導(dǎo)言目的本文檔是根據(jù)個(gè)人的工作經(jīng)驗(yàn)搭建的輕量級(jí)SSI框架,也是實(shí)際應(yīng)用中比較全面的基礎(chǔ)框架,用于指導(dǎo)SSI框架初學(xué)者學(xué)習(xí)搭建SSI框架,希望能給各位使用者提供幫助,同時(shí)也希望朋友們盡量去幫助其他人。范圍本次框架搭建的版本是S+Spring3.1.2+,數(shù)據(jù)庫(kù)采用的是mysql,在eclipse開(kāi)發(fā)工具下搭建直接搭建的web項(xiàng)目,頁(yè)面采用的是h5,ajax實(shí)現(xiàn)數(shù)據(jù)訪問(wèn),如果頁(yè)面為jsp等,則修改controller的返回類型即可。說(shuō)明本文檔是個(gè)人總結(jié)的經(jīng)驗(yàn),僅供參考學(xué)習(xí),同時(shí)歡迎朋友們一起溝通學(xué)習(xí),文檔中會(huì)對(duì)使用的jar做詳細(xì)解說(shuō),對(duì)配置的過(guò)程使用圖文說(shuō)明,因?yàn)镾SI博大精深,很多知識(shí)在搭建過(guò)程中沒(méi)有使用到,只是一個(gè)基礎(chǔ)框架的搭建,能測(cè)試通過(guò)就可以,所以對(duì)于高要求的開(kāi)發(fā),需要自己添加jar、增加方法和配置,同時(shí)文檔中出現(xiàn)的紕漏希望朋友們等指正。開(kāi)發(fā)環(huán)境eclipse-jee-indigo-SR1-win32-dic,Tomcat7.0,JDK1.7,mysql。

搭建SpringMVC搭建所需jar包序號(hào)名稱描述1spring面向切面編程,提供AOP(面向切面編程)2spring獨(dú)立的asm程序[spring2.5.6的時(shí)候需要asmJar包,3.0開(kāi)始提供他自己獨(dú)立的asm.jar

.ASM是小巧輕便的Java字節(jié)碼操控框架,它能方便地生成和改造Java代碼。Hibernate和Spring在底層都用到了ASM(CgLib底層用的asm)]3springIoC(依賴注入)的基礎(chǔ)實(shí)現(xiàn),所有應(yīng)用都要用到的,它包含訪問(wèn)配置文件、創(chuàng)建和管理bean以及進(jìn)行InversionofControl/DependencyInjection(IoC/DI)操作相關(guān)的所有類4spring提供在基礎(chǔ)IoC功能上的擴(kuò)展服務(wù),此外還提供許多企業(yè)級(jí)服務(wù)的支持,如郵件服務(wù)、任務(wù)調(diào)度、JNDI定位、EJB集成、遠(yuǎn)程訪問(wèn)、緩存以及各種視圖層框架的封裝等5包含Spring框架基本的核心工具類,Spring其它組件要都要使用到這個(gè)包里的類,是其它組件的基本核心6spring表達(dá)式語(yǔ)言7包含對(duì)Spring對(duì)JDBC數(shù)據(jù)訪問(wèn)進(jìn)行封裝的所有類8封裝了spring對(duì)于事物的控制9包含Web應(yīng)用開(kāi)發(fā)時(shí),用到Spring框架時(shí)所需的核心類,包括自動(dòng)載入WebApplicationContext

特性的類、Struts與JSF集成類、文件上傳的支持類、Filter類和大量工具輔助類10SpringWebMVC是一種基于Java的實(shí)現(xiàn)了WebMVC設(shè)計(jì)模式的請(qǐng)求驅(qū)動(dòng)類型的輕量級(jí)Web框架,即使用了MVC架構(gòu)模式的思想,將web層進(jìn)行職責(zé)解耦,基于請(qǐng)求驅(qū)動(dòng)指的就是使用請(qǐng)求-響應(yīng)模型其他依賴包序號(hào)名稱描述1這個(gè)包是AOP聯(lián)盟的API包,里面包含了針對(duì)面向切面的接口。

通常Spring等其它具備動(dòng)態(tài)織入功能的框架依賴此包2SpringAOP所要用到的包3跟java.lang這個(gè)包的作用類似,CommonsLang這一組API也是提供一些基礎(chǔ)的、通用的操作和處理,如自動(dòng)生成toString()的結(jié)果、自動(dòng)實(shí)現(xiàn)hashCode()和equals()方法、數(shù)組操作、枚舉、日期和時(shí)間的處理等等4在sun開(kāi)發(fā)logger前,apache項(xiàng)目已經(jīng)開(kāi)發(fā)了功能強(qiáng)大的log4j日志工具,并向sun推薦將其納入到j(luò)dk的一部分,可是sun拒絕了apache的提議,sun后來(lái)自己開(kāi)發(fā)了一套記錄日志的工具??墒乾F(xiàn)在的開(kāi)源項(xiàng)目都使用的是log4j,log4j已經(jīng)成了事實(shí)上的標(biāo)準(zhǔn),但由于又有一部分開(kāi)發(fā)者在使用sunlogger,因此apache才推出commons-logging,使得我們不必關(guān)注我們正在使用何種日志工具5日志組件搭建步驟創(chuàng)建項(xiàng)目創(chuàng)建一個(gè)web項(xiàng)目,如下所示:需要配置tomcat,用于web啟動(dòng)容器。導(dǎo)入jar包項(xiàng)目中點(diǎn)擊lib,復(fù)制需要的jar到lib中即完成導(dǎo)入,如下所示:如果沒(méi)有導(dǎo)入完成,右鍵點(diǎn)擊項(xiàng)目名(SSH),選擇properties-JavaBuildPath,點(diǎn)擊AddExtenrnalJARs,選擇項(xiàng)目lib中的jar包進(jìn)行導(dǎo)入,如下所示:配置web.xml在WEB-INF下配置web.xml,若沒(méi)有該文件,則自己創(chuàng)建一個(gè)xml文件,主要配置spring容器,內(nèi)容如下所示:<?xmlversion="1.0"encoding="UTF-8"?><web-appxmlns:xsi="/2001/XMLSchema-instance"xmlns="/xml/ns/javaee"xmlns:web="/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="/xml/ns/javaee/xml/ns/javaee/web-app_3_0.xsd"id="WebApp_ID"version="3.0"><!--定義LOG4J監(jiān)聽(tīng)器--><listener><listener-class>org.springframework.web.util.Log4jConfigListener</listener-class></listener><!--log4jConfigLocation:log4j配置文件存放路徑--><context-param><param-name>log4jConfigLocation</param-name><param-value>classpath:config/perties</param-value></context-param><!--springMVCServlet配置--><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:config/spring-servlet.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>springMvc</servlet-name><url-pattern>*.do</url-pattern></servlet-mapping><!--spring監(jiān)聽(tīng)器--><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!--防止spring內(nèi)存溢出監(jiān)聽(tīng)器--><listener><listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class></listener><!--裝載spring配置文件--><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:config/applicationContext.xml</param-value></context-param><!--字符集過(guò)濾器--><filter><filter-name>encodingFilter</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>encodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!--會(huì)話有效期(單位:分鐘)--><session-config><session-timeout>15</session-timeout></session-config><!--系統(tǒng)首頁(yè)--><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>備注:web.xml中,引入了spring-servlet.xml和applicationContext.xml文件,需要把log4j配置在前面,否則會(huì)報(bào)警告,因?yàn)楹罄m(xù)加載的程序有打印日志的需求,不過(guò)不影響系統(tǒng)運(yùn)行。配置spring-servlet.xml該文件主要進(jìn)行springmvc的相關(guān)配置,如注解、映射、解析等。<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="/schema/beans"xmlns:mvc="/schema/mvc"xmlns:xsi="/2001/XMLSchema-instance"xmlns:p="/schema/p"xmlns:context="/schema/context""><!--啟用springmvc注解--><mvc:annotation-driven/><!--設(shè)置使用注解的類所在的jar包--><context:component-scanbase-package=""/><!--完成請(qǐng)求和注解POJO的映射--><beanclass="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/><!--對(duì)轉(zhuǎn)向頁(yè)面的路徑解析。prefix:前綴,suffix:后綴--><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"p:prefix="/jsp/"p:suffix=".jsp"/></beans>配置applicationContext.xml配置mybatis的時(shí)候再一起介紹。配置pertieslog4j.rootLogger=info,Console,Filelog4j.appender.Console.encoding=utf-8log4j.appender.Console.layout.ConversionPattern=%-d{yyyy-MM-ddHH:mm:ss}%p(%F:%L)-%m%n#log4j.appender.File=org.apache.log4j.RollingFileAppenderlog4j.appender.File=org.apache.log4j.DailyRollingFileAppenderlog4j.appender.File.DatePattern='.'yyyy-MM-ddlog4j.appender.File.File=../logs/SpringMvc.log#log4j.appender.File.MaxFileSize=10MBlog4j.appender.File.Threshold=ALLlog4j.appender.File.layout.ConversionPattern=%-d{yyyy-MM-ddHH:mm:ss}%p(%F:%L)-%m%n整合mybatis整合所需jar包序號(hào)名稱描述1mybatis核心包2mybatis和spring整合包3mysql驅(qū)動(dòng)包其他依賴包序號(hào)名稱描述1Druid是目前最好的數(shù)據(jù)庫(kù)連接池,在功能、性能、擴(kuò)展性方面,都超過(guò)其他數(shù)據(jù)庫(kù)連接池2jackson-core設(shè)置@ResponseBody,把對(duì)象轉(zhuǎn)換成json格式傳輸,Json序列化和反序列化的包3同上4sql解析工具5mybatis分頁(yè)插件整合步驟導(dǎo)入jar包項(xiàng)目中點(diǎn)擊lib,復(fù)制需要的jar到lib中即完成導(dǎo)入,如下所示:配置perties該文件為數(shù)據(jù)庫(kù)配置文件,內(nèi)容如下:#數(shù)據(jù)庫(kù)連接信息配置jdbc_username=rootjdbc_password=rootjdbc_url=jdbc:mysql://:3306/springmvc_test?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull配置spring-dataSource.xml該文件主要進(jìn)行數(shù)據(jù)庫(kù)相關(guān)的配置,如下所示:<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="/schema/beans"xmlns:xsi="/2001/XMLSchema-instance"xmlns:context="/schema/context"xmlns:tx="/schema/tx"xmlns:aop="/schema/aop"xsi:schemaLocation="/schema/aop/spring-aop-3.2.xsd"><!--配置數(shù)據(jù)源,使用阿里巴巴的druid數(shù)據(jù)源(也可以使用c3p0或者dbcp,druid的性能更好點(diǎn))--><beanid="dataSource"name="dataSource"class="com.alibaba.druid.pool.DruidDataSource"init-method="init"destroy-method="close"><!--配置數(shù)據(jù)庫(kù)連接參數(shù),druid會(huì)根據(jù)url自動(dòng)識(shí)別dialect和driverClass無(wú)須配置--><propertyname="url"value="${jdbc_url}"/><propertyname="username"value="${jdbc_username}"/><propertyname="password"value="${jdbc_password}"/><!--初始化來(lái)連接大小--><propertyname="initialSize"value="10"/><!--連接池中使用的最大連接數(shù)量,在druid中相當(dāng)于maxPoolSize--><propertyname="maxActive"value="100"/><!--連接池中最大的空閑連接數(shù)--><propertyname="maxIdle"value="10"/><!--連接池中最小的空閑連接數(shù),在druid中相當(dāng)于minPoolSize--><propertyname="minIdle"value="5"/><!--獲取連接的最大等待時(shí)間,單位毫秒--><propertyname="maxWait"value="60000"/><!--配置間隔多久才進(jìn)行一次檢測(cè),檢測(cè)需要關(guān)閉的空閑連接,單位是毫秒--><propertyname="timeBetweenEvictionRunsMillis"value="60000"/><!--配置一個(gè)連接在池中最小生存的時(shí)間,單位是毫秒--><propertyname="minEvictableIdleTimeMillis"value="300000"/><propertyname="validationQuery"value="SELECT'x'"/><propertyname="testWhileIdle"value="true"/><propertyname="testOnBorrow"value="false"/><propertyname="testOnReturn"value="false"/><!--打開(kāi)removeAbandoned功能--><propertyname="removeAbandoned"value="true"/><!--1800秒,也就是30分鐘--><propertyname="removeAbandonedTimeout"value="1800"/><!--關(guān)閉abanded連接時(shí)輸出錯(cuò)誤日志--><propertyname="logAbandoned"value="true"/><!--打開(kāi)PSCache,并且指定每個(gè)連接上PSCache的大小--><propertyname="poolPreparedStatements"value="false"/><propertyname="maxPoolPreparedStatementPerConnectionSize"value="20"/><!--打開(kāi)druid的監(jiān)控和統(tǒng)計(jì)功能并通過(guò)日志文件記錄sql--><propertyname="filters"value="mergeStat,log4j"/></bean><!--mybatis相關(guān)配置--><beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean"><propertyname="dataSource"ref="dataSource"/><!--加載mybatis的配置文件--><propertyname="configLocation"value="classpath:config/mybatis-config.xml"/><!--自動(dòng)掃描model目錄中的映射xml文件,省去了在config中手工配置--><propertyname="mapperLocations"value="classpath:com/xxx/dao/mapping/**/*.xml"/><!--為指定包下的類指定別名,省去了編寫(xiě)全限定名,默認(rèn)使用類名--><propertyname="typeAliasesPackage"value="com.xxx"/></bean><beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer"><propertyname="basePackage"value="com.xxx.dao"/><propertyname="sqlSessionFactoryBeanName"value="sqlSessionFactory"/></bean><!--配置事物--><beanid="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><propertyname="dataSource"ref="dataSource"/></bean><!--攔截器方式配置事物--><tx:adviceid="transactionAdvice"transaction-manager="transactionManager"><tx:attributes><tx:methodname="save*"propagation="REQUIRED"/><tx:methodname="update*"propagation="REQUIRED"/><tx:methodname="delete*"propagation="REQUIRED"/><tx:methodname="load*"propagation="SUPPORTS"/><tx:methodname="find*"propagation="SUPPORTS"/><tx:methodname="list*"propagation="SUPPORTS"/><tx:methodname="check*"propagation="SUPPORTS"/><tx:methodname="*"propagation="SUPPORTS"/></tx:attributes></tx:advice><aop:config><aop:pointcutid="transactionPointcut"expression="execution(*com.xxx.service..*Impl.*(..))"/><aop:advisorpointcut-ref="transactionPointcut"advice-ref="transactionAdvice"/></aop:config></beans>配置applicationContext.xml該文件主要進(jìn)行自動(dòng)注入,配置文件引入等配置,如下所示:<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="/schema/beans" xmlns:xsi="/2001/XMLSchema-instance"xmlns:context="/schema/context"/schema/context/spring-context-3.2.xsd"><!--自動(dòng)掃描包(自動(dòng)注入)--><context:component-scanbase-package="com.xxx.service"/> <!--引入配置文件--> <context:property-placeholderlocation="classpath:config/perties"/> <!--引入數(shù)據(jù)源文件--> <importresource="spring-dataSource.xml"/></beans>配置mybatis-config.xml該文件主要進(jìn)行mybatis配置,如下所示:<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEconfigurationPUBLIC"-////DTDConfig3.0//EN""/dtd/mybatis-3-config.dtd"><configuration><settings><!--開(kāi)啟緩存--><settingname="cacheEnabled"value="true"/><!--關(guān)閉延遲加載,所有的對(duì)象都即時(shí)加載--><settingname="lazyLoadingEnabled"value="true"/><!--如果存在延時(shí)加載,則對(duì)象屬性按需加載--><settingname="aggressiveLazyLoading"value="true"/><settingname="logImpl"value="LOG4J"/></settings><plugins>為PageHelper類所在包名--><plugininterceptor="com.github.pagehelper.PageHelper"><propertyname="dialect"value="mysql"/><!--該參數(shù)默認(rèn)為false--><!--設(shè)置為true時(shí),會(huì)將RowBounds第一個(gè)參數(shù)offset當(dāng)成pageNum頁(yè)碼使用--><!--和startPage中的pageNum效果一樣--><propertyname="offsetAsPageNum"value="true"/><!--該參數(shù)默認(rèn)為false--><!--設(shè)置為true時(shí),使用RowBounds分頁(yè)會(huì)進(jìn)行count查詢--><propertyname="rowBoundsWithCount"value="true"/><!--設(shè)置為true時(shí),如果pageSize=0或者RowBounds.limit=0就會(huì)查詢出全部的結(jié)果--><!--(相當(dāng)于沒(méi)有執(zhí)行分頁(yè)查詢,但是返回結(jié)果仍然是Page類型)--><propertyname="pageSizeZero"value="true"/><!--分頁(yè)參數(shù)合理化,默認(rèn)false禁用--><!--啟用合理化時(shí),如果pageNum<1會(huì)查詢第一頁(yè),如果pageNum>pages會(huì)查詢最后一頁(yè)--><!--禁用合理化時(shí),如果pageNum<1或pageNum>pages會(huì)返回空數(shù)據(jù)--><propertyname="reasonable"value="true"/></plugin></plugins></configuration>創(chuàng)建實(shí)體model創(chuàng)建一個(gè)用戶實(shí)體,通過(guò)用戶名和密碼登錄系統(tǒng)。packagecom.xxx.model;importjava.io.Serializable;/***影院店鋪信息表**@authorauthor*@date2016/02/16*/publicclassSmiShopInfoimplementsSerializable{ privateIntegerid;privateStringshopNo;privateStringshopName;privateStringshopType;privateStringshopPassword;privateStringnote; publicIntegergetId(){ returnid; } publicvoidsetId(Integerid){ this.id=id; } publicStringgetShopNo(){ returnshopNo; } publicvoidsetShopNo(StringshopNo){ this.shopNo=shopNo; } publicStringgetShopName(){ returnshopName; } publicvoidsetShopName(StringshopName){ this.shopName=shopName; } publicStringgetShopType(){ returnshopType; } publicvoidsetShopType(StringshopType){ this.shopType=shopType; } publicStringgetShopPassword(){ returnshopPassword; } publicvoidsetShopPassword(StringshopPassword){ this.shopPassword=shopPassword; } publicStringgetNote(){ returnnote; } publicvoidsetNote(Stringnote){ this.note=note; }}創(chuàng)建實(shí)例化dao創(chuàng)建實(shí)例化dao接口,及映射關(guān)系以及實(shí)現(xiàn),如下所示:SmiShopInfoMapper.javapackagecom.xxx.dao;importorg.apache.ibatis.annotations.Param;importcom.xxx.model.SmiShopInfo;/***影院店鋪信息映射關(guān)系*數(shù)據(jù)實(shí)例化接口**@authorauthor*@date2016/02/16*/publicinterfaceSmiShopInfoMapper{ /** *根據(jù)門(mén)店編碼和密碼獲取門(mén)店信息 * *@parampassword *門(mén)店密碼 *@paramshopNo *門(mén)店編碼 */ publicSmiShopInfogetByNoAndPwd(@Param("password")Stringpassword,@Param("shopNo")StringshopNo);}SmiShopInfoMapper.xml<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEmapperPUBLIC"-////DTDMapper3.0//EN""/dtd/mybatis-3-mapper.dtd"><mappernamespace="com.smi170.dao.SmiShopInfoMapper"><resultMapid="BaseResultMap"type="com.smi170.model.SmiShopInfo"><idcolumn="id"property="id"jdbcType="INTEGER"/><resultcolumn="shop_no"property="shopNo"jdbcType="VARCHAR"/><resultcolumn="shop_name"property="shopName"jdbcType="VARCHAR"/><resultcolumn="shop_type"property="shopType"jdbcType="VARCHAR"/><resultcolumn="shop_password"property="shopPassword"jdbcType="VARCHAR"/><resultcolumn="note"property="note"jdbcType="VARCHAR"/></resultMap><sqlid="Base_Column_List">id,shop_no,shop_name,shop_type,shop_password,note</sql><!--根據(jù)門(mén)店編碼和密碼獲取門(mén)店信息--><selectid="getByNoAndPwd"resultMap="BaseResultMap">select<includerefid="Base_Column_List"/>fromsmi_shop_infowhereshop_no=#{shopNo,jdbcType=VARCHAR}andshop_password=#{password,jdbcType=VARCHAR}</select></mapper>創(chuàng)建業(yè)務(wù)服務(wù)service創(chuàng)建業(yè)務(wù)服務(wù)接口,及實(shí)現(xiàn),如下所示:ISmiShopInfoService.javapackagecom.xxx.service;importcom.xxx.model.SmiShopInfo;/***影院店鋪信息接口服務(wù)**@authorauthor*@date2016/02/16*/publicinterfaceISmiShopInfoService{ /** *根據(jù)店鋪的編碼和密碼查詢店鋪信息 *@paramshopNo店鋪編碼 *@parampassword密碼 *@return店鋪信息 *@throwsException */ publicSmiShopInfogetShopByNoAndPwd(StringshopNo,Stringpassword)throwsException; }SmiShopInfoService.javapackagecom.xxx.service.impl;importorg.apache.log4j.Logger;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;importcom.xxx.dao.SmiShopInfoMapper;importcom.xxx.model.SmiShopInfo;importcom.xxx.service.ISmiShopInfoService;/***影院店鋪信息接口服務(wù)實(shí)現(xiàn)**@authorauthor*@date2016/02/16*/@Service("smiShopInfoService")publicclassSmiShopInfoServiceimplementsISmiShopInfoService{ privateLoggerlogger=Logger.getLogger(this.getClass()); @Autowired publicSmiShopInfoMappershopInfoMapper; @Override publicSmiShopInfogetShopByNoAndPwd(StringshopNo,Stringpassword)throwsException{ if(null==shopNo||"".equals(shopNo)){ logger.warn("門(mén)店編碼為null或空,請(qǐng)檢查!"); thrownewException("門(mén)店編碼為空"); } returnshopInfoMapper.getByNoAndPwd(password,shopNo); } }創(chuàng)建控制層controller創(chuàng)建控制層,以及vo,提供給頁(yè)面異步調(diào)用,前端采用ajax調(diào)用,如下所示:ShopInfoAction.javapackagecom.xxx.controller;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importorg.apache.log4j.Logger;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.ResponseBody;importcom.xxx.controller.valueobject.ShopInfoVO;importcom.xxx.model.SmiShopInfo;importcom.xxx.service.ISmiShopInfoService;/***影院店鋪信息action**@authorauthor*@date2016/02/16*/@ControllerpublicclassShopInfoAction{ privateLoggerlogger=Logger.getLogger(this.getClass()); @Autowired privateISmiShopInfoServicesmiShopInfoService; /** *用戶登錄 * *@ResponseBody設(shè)置該參數(shù),指定response的type為比如json或xml,本文采用json序列化方式傳輸數(shù)據(jù) * *@paramrequest *@paramresponse *@paramshopNo *@parampassword *@return *@throwsException */ @RequestMapping(value="/login.do") public@ResponseBodyShopInfoVOlogin(HttpServletRequestrequest,HttpServletResponseresponse,StringshopNo,Stringpassword)throwsException{ SmiShopInfoshopInfo=smiShopInfoService.getShopByNoAndPwd(shopNo,password); if(null==shopInfo||"".equals(shopInfo)){ logger.warn(shopNo+":登錄失敗,用戶名或密碼錯(cuò)誤"); returnnull; }else{ ShopInfoVOvo=newShopInfoVO(); vo.setId(shopInfo.getId()); vo.setShopNo(shopInfo.getShopNo()); vo.setShopName(shopInfo.getShopName()); vo.setShopPassword("");//密碼不傳輸?shù)巾?yè)面 vo.setShopType(shopInfo.getShopType()); vo.setNote(shopInfo.getNote()); (shopInfo.getShopName()+"["+shopNo+"]:登錄成功"); returnvo; } }}ShopInfoVO.java序列化成json文件,提供給頁(yè)面使用packagecom.xxx.controller.valueobject;importorg.codehaus.jackson.map.annotate.JsonSerialize;importorg.springframework.stereotype.Component;/***影院店鋪信息vo**@authorauthor*@date2016/02/16*/@Component@JsonSerializepublicclassShopInfoVO{ privateIntegerid; privateStringshopNo; privateStringshopName;privateStringshopType;privateStringshopPassword;privateStringnote; /** *@returntheid */ publicIntegergetId(){ returnid; } /** *@paramidtheidtoset */ publicvoidsetId(Integerid){ this.id=id; } /** *@returntheshopNo */ publicStringgetShopNo(){ returnshopNo; } /** *@paramshopNotheshopNotoset */ publicvoidsetShopNo(StringshopNo){ this.shopNo=shopNo; } /** *@returntheshopName */ publicStringgetShopName(){ returnshopName; } /** *@paramshopNametheshopNametoset */ publicvoidsetShopName(StringshopName){ this.shopName=shopName; } /** *@returntheshopType */ publicStringgetShopType(){ returnshopType; } /** *@paramshopTypetheshopTypetoset */ publicvoidsetShopType(StringshopType){ this.shopType=shopType; } /** *@returntheshopPassword */ publicStringgetShopPassword(){ returnshopPassword; } /** *@paramshopPasswordtheshopPasswordtoset */ publicvoidsetShopPassword(StringshopPassword){ this.shopPassword=shopPassword; } /** *@returnthenote */ publicStringgetNote(){ returnnote; } /** *@paramnotethenotetoset */ publicvoidsetNote(Stringnote){ this.note=note; }}頁(yè)面代碼頁(yè)面采用ajax調(diào)用后臺(tái)服務(wù),如下所示:login.css@charset"utf-8";html{font-size:62.5%;}body{font-size:1.6rem;font-family:"microsoftyahei","arial,helvetica,sans-serif";}body,div,ul,li,h1,h2,h3,h4,h5,h6,pre,form,fieldset,input,textarea,p,blockquote,th,td{padding:0;margin:0;}ul,li{list-style:none;list-style-type:none;}.login.hd{background:-webkit-gradient(linear,00,0100%,from(#f8f8f8),to(#e6e6e6));height:45px;-webkit-box-shadow:01px1px0#c4c4c4;text-align:center;line-height:45px;font-weight:bold;position:relative;}.login.hd.back_shop{display:block;position:absolute;top:14px;left:12px;border:8pxsolidtransparent;border-right:8pxsolid#ef2185;width:0;height:0;content:''}.login.bd{margin:12px20px;}.login.bd.item{height:40px;}.login.bdlabel{display:inline-block;width:80px;}.login.bdinput{background:-webkit-gradient(linear,00,0100%,from(#f5f5f5),to(#fdfdfd));border:solid1px#DDD;-webkit-appearance:none;border-top-color:#bbb;height:33px;-webkit-box-sizing:border-box;box-sizing:border-box;width:100%;font-size:1.3rem;padding:02%;}.login.ft{margin:020px;}.login.ft.btnLogin{font-size:20px;background:#ef2185;height:40px;text-align:center;border:0;color:#fff;text-shadow:0-1px0#8E5244;-webkit-box-shadow:0-1px0#bf3210inset;width:100%;}#msg{width:300px;background:#000;border-radius:005px5px;line-height:30px;color:#fff;text-align:center;position:fixed;left:50%;margin:000-150px;top:0;z-index:100;opacity:0.8}.slidedown{ animation:fromtop0.5sease; -webkit-animation:fromtop0.5sease;}@keyframesfromtop{ from{transform:translateY(-100%)}}@-webkit-keyframesfromtop{ from{-webkit-transform:translateY(-100%)}}.slideup{ animation:totop0.5sease; -webkit-animation:totop0.5sease;}@keyframestotop{ to{transform:translateY(-100%)}}@-webkit-keyframestotop{ to{-webkit-transform:translateY(-100%)}}login.html<!DOCTYPEHTML><html><head><title>xxx系統(tǒng)</title><metaname="viewport"content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no"><linkrel="stylesheet"type="text/css"href="css/login.css"></head><body><divclass="login"><divclass="hd">xxx系統(tǒng)</div><divclass="bd"><divclass="item"><inputtype="text"name="username"id="username"placeholder="請(qǐng)輸入門(mén)店編號(hào)"/></div><divclass="item"><inputtype="password"name="password"id="password"placeholder="請(qǐng)輸入密碼"/></div></div><divclass="ft"><inputtype="submit"value="登錄"id="btnLogin"class="btnLogin"/></div></div><scriptsrc="script/jquery.min.js"></script><scriptsrc="script/md5.js"></script><scriptsrc="script/common.js"></script><scriptsrc="script/login.js"></script><script>varG={timer:null,timer2:null};functionshowmsg(str){if(!$("#msg").length){G.msg=$("<divid='msg'/>").prependTo(document.body);}if(G.timer){clearTimeout(G.timer);G.msg.hide();G.msg.removeClass("slidedown").removeClass("slideup");}if(G.timer2){clearTimeout(G.timer2);}G.msg.html(str).show().addClass("slidedown");G.timer=setTimeout(function(){G.msg.removeClass("slidedown").addClass("slideup");G.timer2=setTimeout(function(){G.msg.hide();},500);},3000);}</script></body></html>login.js//初始化數(shù)據(jù)$(document).ready(function(){ varusername=getLocalStorage("username"); if(!isNull(username)){ $('#username').val(username); }});//登錄$("#btnLogin").click(function(){ varusername=$('#username').val(); varpassword=$('#password').val(); if(isNull(username)){showmsg("請(qǐng)輸入門(mén)店編號(hào)");returnfalse;}if(isNull(password)){showmsg("請(qǐng)輸入您的密碼");returnfalse;} //加密password=$.md5(password); showLoadingDialog("登錄中,請(qǐng)稍后..."); $.ajax({ type:"POST", async:true, url:ctx+"/login.do", data:{"shopNo":username,"password":password}, error:function(){ hideLoadingDialog(); showmsg("登錄異常"); }, success:function(res){ hideLoadingDialog(); if(!isNull(res)){ //console.log(res.shopNo); //console.log(res); clearLocalStorage(); setLocalStorage("username",res.shopNo);//用于記住用戶名,不清除 setLocalStorage("shopNo",res.shopNo); setLocalStorage("shopId",res.id); setLocalStorage("shopInfo",JSON.stringify(res)); showmsg("恭喜你,登錄成功!"); }else{ showmsg("用戶名或密碼錯(cuò)誤"); } } });});common.jsvarctx='/SpringMvc-web';varLOCAL_STORAGE_KEY_PREFIX="smi_local_storage";varstorage=window.localStorage;functiongetLocalStorage(key){varvalue=storage.getItem(LOCAL_STORAGE_KEY_PREFIX+key);if(value==""||value==null||value==undefined){return"";}returnvalue;}functionsetLocalStorage(key,value){storage.setItem(LOCAL_STORAGE_KEY_PREFIX+key,value);}functionclearLocalStorage(){ //保留登錄用戶名 varusername=getLocalStorage("username"); storage.clear(); setLocalStorage("username",username);}functionremoveStorage(key){storage.removeItem(LOCAL_STORAGE_KEY_PREFIX+key);}functionremovesStorage(key){window.sessionStorage.removeItem(LOCAL_STORAGE_KEY_PREFIX+key);}vardic=newObject();dic.getJsonData=function(serviceCode,methodName){returndic.getFormData(serviceCode,methodName);};dic.getFormData=function(serviceCode,methodName,formId){varobj=newObject();if(formId){varformData=$('#'+f

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論