隨筆-67  評(píng)論-522  文章-0  trackbacks-0
              Spring更新到3.0之后,其MVC框架加入了一個(gè)非常不錯(cuò)的東西——那就是REST它的開放式特性,與Spring的無縫集成,以及Spring框架的優(yōu)秀表現(xiàn),使得現(xiàn)在很多公司將其作為新的系統(tǒng)開發(fā)框架。大象根據(jù)實(shí)際的項(xiàng)目經(jīng)驗(yàn),以之前SSH2例子為基礎(chǔ),對(duì)其進(jìn)行一次大改造,詳細(xì)的為大家講解如何實(shí)現(xiàn)SSM3全注解式的開發(fā)。
              這次大象將采取兩種構(gòu)建方式,一是很多人喜歡用的MyEclipse,另一個(gè),則是用Eclipse+Maven。這一篇,將主要講解開發(fā)環(huán)境設(shè)置Maven構(gòu)建方式。
              1、
          開發(fā)環(huán)境
              
              
          JDK1.6.0_18
              Eclipse-3.6  Maven3.0.1  m2eclipse0.12.1.20110112-1712
              Tomcat6.0.10  maven-jetty-plugin6.1.26
              MySQL5.0.27  Navicat Lite for MySQL 8.1.20
              每個(gè)人的開發(fā)環(huán)境可能會(huì)有差異,但有一點(diǎn)我需要說明的是,JDK的版本不得低于1.5,因?yàn)橛玫搅撕芏?/span>1.5版才支持的新特性。TomcatJettyMavenMySQL請(qǐng)不要低于我所用的版本,因?yàn)槲覜]在其它的版本上進(jìn)行測(cè)試。Navicat則是MySQL數(shù)據(jù)庫(kù)的圖形化操作工具。接下來我將介紹如何在Eclipse3.6中,使用m2eclipse插件構(gòu)建web應(yīng)用及測(cè)試開發(fā)環(huán)境。
              
          2、
          設(shè)置Maven
              Maven的安裝很簡(jiǎn)單,只需要解壓即可,請(qǐng)?jiān)O(shè)置PATH變量,這樣可以使用命令行進(jìn)行操作,然后就要在%MAVEN_HOME%\conf目錄下,對(duì)settings.xml作下修改
              
              這就是設(shè)置本地倉(cāng)庫(kù),目錄可以根據(jù)自己的實(shí)際情況更改,不過請(qǐng)使用"/"正斜杠,因?yàn)槲以趯?shí)際使用中,發(fā)現(xiàn)反斜杠有時(shí)候獲取不到資源。對(duì)于個(gè)人使用,設(shè)置好這個(gè)就OK了,至于Nexus配置不在本文討論范圍內(nèi),大家如有興趣可以去看看juven xu的博客,他是目前公認(rèn)的Maven專家。
              
          3、
          安裝m2eclipse
              選擇Help->Install New Software...在彈出的窗口中,點(diǎn)擊Add...又會(huì)彈出一個(gè)小窗口,輸入m2eclipse的安裝地址,如下圖所示:
              
              輸入完成后,點(diǎn)擊OK,這時(shí)Eclipse就開始查找這個(gè)插件了,請(qǐng)耐心等一會(huì)。
              
              插件找到后,請(qǐng)勾選要安裝的內(nèi)容,接下來就是一般的安裝流程了,此處省略500字。安裝完成請(qǐng)重新啟動(dòng)Eclipse,然后再對(duì)這個(gè)插件進(jìn)行一番設(shè)置。
              
          4、
          設(shè)置m2eclipse
              進(jìn)入Perferences,選擇Maven,去掉Download repository index updates on startup前的勾,默認(rèn)情況是會(huì)在每次啟動(dòng)Eclipse的時(shí)候自動(dòng)去Maven中央倉(cāng)庫(kù)下載索引,這無疑是非常不好的,要知道Maven中央倉(cāng)庫(kù)所包含的jar資源非常龐大,而且每天都會(huì)有很多新的項(xiàng)目上傳,弄不好Eclipse還沒開始用,就被這些東西搞掛掉了。
              
              接下來選擇Installations將這個(gè)插件自帶的Maven換成之前安裝的Maven,這樣就保證了版本的一致性。
              
              設(shè)置好這個(gè)之后,再點(diǎn)擊User Settings,用本機(jī)mavensettings.xml替換默認(rèn)的配置文件,因?yàn)槟J(rèn)的設(shè)置是會(huì)將本地倉(cāng)庫(kù)放到系統(tǒng)盤符\Documents and Settings\用戶目錄\.m2\repository這個(gè)目錄下面。
              
              大家可以看到,用maven中的配置文件替換后,下面的Local Repository自動(dòng)變更為settings.xml中的設(shè)置。
              
          5、
          創(chuàng)建maven工程
              做完這些,我們就可以開始創(chuàng)建Maven工程了。選擇New->other...->Maven->Maven Project,然后選擇下一步
              
              請(qǐng)?jiān)O(shè)置工作空間路徑,大象的默認(rèn)工作空間放在eclipse的根目錄下面,這里的Location顯示的應(yīng)該為空,為了進(jìn)行說明,特將路徑顯示出來。確定沒問題后,Next>
              
              這個(gè)列表顯示的就是maven支持的所有項(xiàng)目創(chuàng)建類型,我們是開發(fā)web應(yīng)用,所以請(qǐng)選擇maven-archetype-webapp
              
              最后一步輸入我們要?jiǎng)?chuàng)建的項(xiàng)目,在Maven世界中,使用坐標(biāo)來唯一標(biāo)識(shí)一個(gè)構(gòu)件,可以理解為項(xiàng)目,資源等等。Group Id表示當(dāng)前項(xiàng)目所屬的實(shí)際項(xiàng)目,Artifact Id定義實(shí)際項(xiàng)目中的一個(gè)Maven項(xiàng)目,根據(jù)名字就可以看出來,Group Id是一個(gè)大范圍,而Artifact Id是一個(gè)小范圍。比如大家都很熟悉的spring,就分成了spring-core.jarspring-beans.jarspring-context.jar等等。在maven里面,它的Group Id就是org.springframework,而Artifact Id則為spring-corespring-beansspring-context。怎么樣?理解了沒有?
              
              到此,項(xiàng)目生成了,請(qǐng)展開src/main,在main目錄下新建一個(gè)java文件夾,打開ssm3的項(xiàng)目屬性,選擇Java Build Path->Source->Add Folder...->勾選java,這樣做的目的,就是將src/main/java這個(gè)路徑作為源文件的文件夾,這和以往用MyEclipse做開發(fā)的目錄結(jié)構(gòu)是不同的。而maven的規(guī)則也是這樣定義的,假如你不進(jìn)行這個(gè)設(shè)置,就算你在main下面創(chuàng)建了java目錄,再添加包或類時(shí),就會(huì)有問題,大家試試,看會(huì)出現(xiàn)什么錯(cuò)誤。
              
              
          6、
          運(yùn)行ssm3
              接下來,在pom.xml里面加入maven-jetty-plugin插件,默認(rèn)生成的配置都可以去掉,整個(gè)pom就只有下圖所示的配置。
              
              打開Run Configurations,這有多種方式打開,可以從菜單Run里面選,也可以從工具欄選擇,還可以在項(xiàng)目點(diǎn)擊右鍵選擇。
              
              
              在彈出的窗口,Maven Build里面設(shè)置運(yùn)行參數(shù),點(diǎn)擊Browse Workspace...會(huì)彈出下圖那個(gè)小窗口,設(shè)定Base directory,加入jetty:run,點(diǎn)擊Run,啟動(dòng)jetty
              
              在瀏覽器中輸入http://localhost:8080/ssm3會(huì)顯示Hello World!,調(diào)用的是ssm3/main/webapp/index.jsp,大象加了點(diǎn)內(nèi)容,結(jié)果就是這樣的
              
              到這里,關(guān)于在Eclipse里搭建maven環(huán)境,配置,創(chuàng)建,運(yùn)行,測(cè)試就全部講完了,大家動(dòng)手做做,熟悉一下這種開發(fā)方式,接下來就會(huì)在這個(gè)骨架上開發(fā)SSM3示例。恩,我們下次繼續(xù)。
              本文為菠蘿大象原創(chuàng),如要轉(zhuǎn)載請(qǐng)注明出處。http://bolo.blogjava.net/
          posted on 2011-05-23 00:57 菠蘿大象 閱讀(29235) 評(píng)論(14)  編輯  收藏 所屬分類: Spring3

          評(píng)論:
          # re: Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解實(shí)例詳解(一) 2011-05-23 09:30 | roywong
          終于出來了終于出來了,期待已久。哈哈 感謝   回復(fù)  更多評(píng)論
            
          # re: Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解實(shí)例詳解(一) 2011-05-23 13:51 | QAlong
          問個(gè)問題,
          前幾天我也實(shí)踐了一個(gè)全注解的方式
          使用了mybatis-spring-integration,所有dao中的代碼都不需要使用實(shí)現(xiàn)類,而是直接接口對(duì)應(yīng)著mapping
          但是在實(shí)際中遇到批量操作數(shù)據(jù)的時(shí)候卻沒有很好的方式解決。
          所以請(qǐng)教一下有沒有好的建議  回復(fù)  更多評(píng)論
            
          # re: Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解實(shí)例詳解(一) 2011-05-23 14:14 | 菠蘿大象
          @QAlong
          你說的批量操作數(shù)據(jù)是什么意思?insert?update?delete?另外我感覺在mapping中定義namespace的方式,要?jiǎng)?chuàng)建好多個(gè)接口,維護(hù)起來不是很方便。  回復(fù)  更多評(píng)論
            
          # re: Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解實(shí)例詳解(一) 2011-05-23 17:58 | 目繪藝術(shù)
          您好!我看了你前面一篇關(guān)于eclipse與myeclipse的討論帖。
          不過我今天看你使用maven的方式全是圖形化界面。
          一種比較好的方式,使用命令行創(chuàng)建一個(gè)webapp項(xiàng)目。
          然后使用mvn eclipse:eclipse生成eclipse特性文件,很方便根本不需要你修改Java Build Path,麻煩。

          你寫的文章排版很不錯(cuò),看上去很舒心。圖片上面的水印是用什么工具做的?  回復(fù)  更多評(píng)論
            
          # re: Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解實(shí)例詳解(一) 2011-05-23 18:44 | 菠蘿大象
          @目繪藝術(shù)
          你說的在命令行操作是很好,不過要打出一長(zhǎng)串命令,還有大小寫之分,對(duì)于初學(xué)者來說,不是很適合。水印是用輕松水印工具做的。  回復(fù)  更多評(píng)論
            
          # re: Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解實(shí)例詳解(一) 2011-05-26 00:39 | gavingeng
          呵呵,不錯(cuò)!
          config的很基礎(chǔ)
          期待后續(xù)的更新......  回復(fù)  更多評(píng)論
            
          # re: Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解實(shí)例詳解(一) 2011-06-01 20:59 | QAlong
          @菠蘿大象
          我說的是batch操作,一次性插入成上千條數(shù)據(jù)的時(shí)候

          比如我是這樣實(shí)現(xiàn)的:
          public interface AreaDao {
          List<Area> getAll();
          void insert(Area area);
          void update(Area area);
          void delete(Area area);
          }

          public interface AreaManager {
          List<Area> getAll();
          String insert(Area area);
          void update(Area area);
          void delete(Area area);
          }

          @Service
          public class AreaManagerImpl implements AreaManager {
          @Autowired
          public AreaDao areaDao;
          public void setAreaDao(AreaDao areaDao) {
          this.areaDao = areaDao;
          }
          public List<Area> getAll() {
          return areaDao.getAll();
          }
          public String insert(Area area) {
          if (areaDao.getByArea(area).size() != 0)
          return "Error";
          areaDao.insert(area);
          return "Success";
          }
          public void update(Area area) {
          areaDao.update(area);
          }
          public void delete(Area area) {
          areaDao.delete(area);
          }
          }


          <mapper namespace="com.gc.dao.AreaDao">

          <resultMap id="RM.Area" type="com.gc.entity.Area">
          <result property="id" column="ID" />
          <result property="areaCode" column="AREA_CODE" />
          <result property="areaNameChn" column="AREA_NAME_CHN" />
          <result property="areaNameEng" column="AREA_NAME_ENG" />
          <result property="areaNameKrn" column="AREA_NAME_KRN" />
          <result property="isDeleted" column="IS_DELETED" />
          <result property="live" column="LIVE" />
          <collection property="parent" column="PARENT_ID" select="getParentArea" />
          </resultMap>

          <sql id="Area.columns">
          <![CDATA[
          ID,AREA_CODE,AREA_NAME_CHN,AREA_NAME_ENG,AREA_NAME_KRN,PARENT_ID,IS_DELETED
          ]]>
          </sql>

          <select id="getAll" resultMap="RM.Area">
          select * from bs_area where
          is_deleted=0
          </select>

          略。。。
          </mapper>








          <beans xmlns="http://www.springframework.org/schema/beans"" target="_new">http://www.springframework.org/schema/beans"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"" target="_new">http://www.springframework.org/schema/aop"
          xmlns:tx="http://www.springframework.org/schema/tx"" target="_new">http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"" target="_new">http://www.springframework.org/schema/jdbc"
          xmlns:context="http://www.springframework.org/schema/context"" target="_new">http://www.springframework.org/schema/context"
          xsi:schemaLocation="
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
          http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
          <bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource">
          <property name="driverClassName" value="${jdbc.driverClassName}" />
          <property name="url" value="${jdbc.url}" />
          <property name="username" value="${jdbc.username}" />
          <property name="password" value="${jdbc.password}" />
          </bean>

          <!-- transaction manager, use JtaTransactionManager for global tx -->
          <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
          <property name="dataSource" ref="dataSource" />
          </bean>
          <!-- enable component scanning (beware that this does not enable mapper
          scanning!) -->
          <context:component-scan base-package="com.gc.biz.impl" />
          <!-- enable autowire -->
          <context:annotation-config />

          <!-- enable transaction demarcation with annotations -->
          <tx:annotation-driven />

          <!-- scan for mappers and let them be autowired -->
          <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
          <property name="basePackage" value="com.gc.dao" />
          </bean>
          <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
          <property name="dataSource" ref="dataSource" />
          <property name="configLocation" value="classpath:mybatis-config.xml" />
          </bean>

          <!-- define the SqlSessionFactory, notice that configLocation is not needed
          when you use MapperFactoryBean
          <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
          <property name="dataSource">
          <ref bean="dataSource" />
          </property>
          </bean>-->
          <bean id="expensesItemBatchDao" class="com.gc.dao.util.ExpensesItemBatchDao">
          <!-- <property name="jdbcTemplate" ref="jdbcTemplate"> </property> -->
          <property name="dataSource">
          <ref bean="dataSource" />
          </property>
          </bean>
          </beans>

            回復(fù)  更多評(píng)論
            
          # re: Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解實(shí)例詳解(一) 2011-06-01 23:31 | 菠蘿大象
          @QAlong
          你的mapper文件里略去了insert方法的配置,我不知道你是怎么寫的,如果是一條普通的insert語(yǔ)句,從你的areaDao的插入方法可以看出,你傳入的參數(shù)是一個(gè)pojo對(duì)象,你想通過這樣一個(gè)對(duì)象來實(shí)現(xiàn)一次性插入成千上萬(wàn)條數(shù)據(jù),我覺得這不可能。  回復(fù)  更多評(píng)論
            
          # re: Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解實(shí)例詳解(一) 2011-06-03 11:48 | 秦?zé)j
          "Group Id是一個(gè)大范圍,而Artifact Id是一個(gè)小范圍"根據(jù)spring實(shí)例講解 我更加深了印象。

          另外eclipse3.6 maven plugin安裝時(shí)候 可以從Help->Eclipse Marketplace->Search maven。 好處就是不用記那么多url了。  回復(fù)  更多評(píng)論
            
          # re: Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解實(shí)例詳解(一) 2012-05-23 23:08 | bj
          新建個(gè)maven project

          Select an Archetype 時(shí)候選擇了 maven-archetype-webapp

          finish的時(shí)候 提示我以下的錯(cuò)誤

          Could not resolve archetype org.apache.maven.archetypes:maven-archetype-webapp:RELEASE from any of the configured repositories.  回復(fù)  更多評(píng)論
            
          # re: Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解實(shí)例詳解(一) 2012-05-24 10:33 | 菠蘿大象
          @bj
          你裝的插件是什么版本?你本地裝了maven嗎?不要用插件自帶的maven,down一個(gè)3.0的版本,按我寫的步驟做,是沒問題的  回復(fù)  更多評(píng)論
            
          # re: Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解實(shí)例詳解(一) 2012-05-27 11:01 | bj
          @菠蘿大象
          問題解決!謝謝大象!  回復(fù)  更多評(píng)論
            
          # re: Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解實(shí)例詳解(一)[未登錄] 2012-07-09 10:34 |
          @bj
          @bj
          你咋解決的?  回復(fù)  更多評(píng)論
            
          # re: Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解實(shí)例詳解(一) 2013-03-12 20:18 | 神一樣存在
          大象你好niubi  回復(fù)  更多評(píng)論
            
          主站蜘蛛池模板: 湟中县| 鄢陵县| 元氏县| 乐陵市| 莫力| 宁远县| 敖汉旗| 阜平县| 沁阳市| 连平县| 西充县| 银川市| 曲水县| 吴旗县| 新营市| 哈尔滨市| 高要市| 临邑县| 盘锦市| 商南县| 甘德县| 丰顺县| 营口市| 惠水县| 湖口县| 承德县| 白银市| 天峨县| 峨山| 南乐县| 银川市| 台州市| 长沙县| 吉木乃县| 荔波县| 呼图壁县| 九台市| 古田县| 屯昌县| 巴东县| 扶绥县|