随笔-67  评论-522  文章-0  trackbacks-0
    Spring更新到3.0之后,其MVC框架加入了一个非常不错的东西——那就是REST它的开放式特性,与Spring的无缝集成,以及Spring框架的优秀表现,使得现在很多公司将其作为新的系统开发框架。大象根据实际的项目经验,以之前SSH2例子为基础,对其进行一次大改造,详细的为大家讲解如何实现SSM3全注解式的开发。
    这次大象将采取两种构建方式,一是很多人喜欢用的MyEclipse,另一个,则是用Eclipse+Maven。这一篇,将主要讲解开发环境设置Maven构建方式。
    1、
开发环境
    
    
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
    每个人的开发环境可能会有差异,但有一点我需要说明的是,JDK的版本不得低于1.5,因为用到了很多1.5版才支持的新特性。TomcatJettyMavenMySQL不要低于我所用的版本,因为我没在其它的版本上进行测试。Navicat则是MySQL数据库的图形化操作工具。接下来我将介绍如何在Eclipse3.6中,使用m2eclipse插件构建web应用及测试开发环境。
    
2、
设置Maven
    Maven的安装很简单,只需要解压即可,请设置PATH变量,这样可以使用命令行进行操作,然后就要在%MAVEN_HOME%\conf目录下,对settings.xml作下修改
    
    这就是设置本地仓库,目录可以根据自己的实际情况更改,不过请使用"/"正斜杠,因为我在实际使用中,发现反斜杠有时候获取不到资源。对于个人使用,设置好这个就OK了,至于Nexus配置不在本文讨论范围内,大家如有兴趣可以去看看juven xu的博客,他是目前公认的Maven专家。
    
3、
安装m2eclipse
    选择Help->Install New Software...在弹出的窗口中,点击Add...又会弹出一个小窗口,输入m2eclipse的安装地址,如下图所示:
    
    输入完成后,点击OK,这时Eclipse就开始查找这个插件了,请耐心等一会。
    
    插件找到后,请勾选要安装的内容,接下来就是一般的安装流程了,此处省略500字。安装完成请重新启动Eclipse,然后再对这个插件进行一番设置。
    
4、
设置m2eclipse
    进入Perferences,选择Maven,去掉Download repository index updates on startup前的勾,默认情况是会在每次启动Eclipse的时候自动去Maven中央仓库下载索引,这无疑是非常不好的,要知道Maven中央仓库所包含的jar资源非常庞大,而且每天都会有很多新的项目上传,弄不好Eclipse还没开始用,就被这些东西搞挂掉了。
    
    接下来选择Installations将这个插件自带的Maven换成之前安装的Maven,这样就保证了版本的一致性。
    
    设置好这个之后,再点击User Settings,用本机mavensettings.xml替换默认的配置文件,因为默认的设置是会将本地仓库放到系统盘符\Documents and Settings\用户目录\.m2\repository这个目录下面。
    
    大家可以看到,用maven中的配置文件替换后,下面的Local Repository自动变更为settings.xml中的设置。
    
5、
创建maven工程
    做完这些,我们就可以开始创建Maven工程了。选择New->other...->Maven->Maven Project,然后选择下一步
    
    请设置工作空间路径,大象的默认工作空间放在eclipse的根目录下面,这里的Location显示的应该为空,为了进行说明,特将路径显示出来。确定没问题后,Next>
    
    这个列表显示的就是maven支持的所有项目创建类型,我们是开发web应用,所以请选择maven-archetype-webapp
    
    最后一步输入我们要创建的项目,在Maven世界中,使用坐标来唯一标识一个构件,可以理解为项目,资源等等。Group Id表示当前项目所属的实际项目,Artifact Id定义实际项目中的一个Maven项目,根据名字就可以看出来,Group Id是一个大范围,而Artifact Id是一个小范围。比如大家都很熟悉的spring,就分成了spring-core.jarspring-beans.jarspring-context.jar等等。在maven里面,它的Group Id就是org.springframework,而Artifact Id则为spring-corespring-beansspring-context。怎么样?理解了没有?
    
    到此,项目生成了,请展开src/main,在main目录下新建一个java文件夹,打开ssm3的项目属性,选择Java Build Path->Source->Add Folder...->勾选java,这样做的目的,就是将src/main/java这个路径作为源文件的文件夹,这和以往用MyEclipse做开发的目录结构是不同的。而maven的规则也是这样定义的,假如你不进行这个设置,就算你在main下面创建了java目录,再添加包或类时,就会有问题,大家试试,看会出现什么错误。
    
    
6、
运行ssm3
    接下来,在pom.xml里面加入maven-jetty-plugin插件,默认生成的配置都可以去掉,整个pom就只有下图所示的配置。
    
    打开Run Configurations,这有多种方式打开,可以从菜单Run里面选,也可以从工具栏选择,还可以在项目点击右键选择。
    
    
    在弹出的窗口,Maven Build里面设置运行参数,点击Browse Workspace...会弹出下图那个小窗口,设定Base directory,加入jetty:run,点击Run,启动jetty
    
    在浏览器中输入http://localhost:8080/ssm3会显示Hello World!,调用的是ssm3/main/webapp/index.jsp,大象加了点内容,结果就是这样的
    
    到这里,关于在Eclipse里搭建maven环境,配置,创建,运行,测试就全部讲完了,大家动手做做,熟悉一下这种开发方式,接下来就会在这个骨架上开发SSM3示例。恩,我们下次继续。
    本文为菠萝大象原创,如要转载请注明出处。http://bolo.blogjava.net/
posted on 2011-05-23 00:57 菠萝大象 阅读(29189) 评论(14)  编辑  收藏 所属分类: Spring3

评论:
# re: Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解实例详解(一) 2011-05-23 09:30 | roywong
终于出来了终于出来了,期待已久。哈哈 感谢   回复  更多评论
  
# re: Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解实例详解(一) 2011-05-23 13:51 | QAlong
问个问题,
前几天我也实践了一个全注解的方式
使用了mybatis-spring-integration,所有dao中的代码都不需要使用实现类,而是直接接口对应着mapping
但是在实际中遇到批量操作数据的时候却没有很好的方式解决。
所以请教一下有没有好的建议  回复  更多评论
  
# re: Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解实例详解(一) 2011-05-23 14:14 | 菠萝大象
@QAlong
你说的批量操作数据是什么意思?insert?update?delete?另外我感觉在mapping中定义namespace的方式,要创建好多个接口,维护起来不是很方便。  回复  更多评论
  
# re: Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解实例详解(一) 2011-05-23 17:58 | 目绘艺术
您好!我看了你前面一篇关于eclipse与myeclipse的讨论帖。
不过我今天看你使用maven的方式全是图形化界面。
一种比较好的方式,使用命令行创建一个webapp项目。
然后使用mvn eclipse:eclipse生成eclipse特性文件,很方便根本不需要你修改Java Build Path,麻烦。

你写的文章排版很不错,看上去很舒心。图片上面的水印是用什么工具做的?  回复  更多评论
  
# re: Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解实例详解(一) 2011-05-23 18:44 | 菠萝大象
@目绘艺术
你说的在命令行操作是很好,不过要打出一长串命令,还有大小写之分,对于初学者来说,不是很适合。水印是用轻松水印工具做的。  回复  更多评论
  
# re: Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解实例详解(一) 2011-05-26 00:39 | gavingeng
呵呵,不错!
config的很基础
期待后续的更新......  回复  更多评论
  
# re: Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解实例详解(一) 2011-06-01 20:59 | QAlong
@菠萝大象
我说的是batch操作,一次性插入成上千条数据的时候

比如我是这样实现的:
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>

  回复  更多评论
  
# re: Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解实例详解(一) 2011-06-01 23:31 | 菠萝大象
@QAlong
你的mapper文件里略去了insert方法的配置,我不知道你是怎么写的,如果是一条普通的insert语句,从你的areaDao的插入方法可以看出,你传入的参数是一个pojo对象,你想通过这样一个对象来实现一次性插入成千上万条数据,我觉得这不可能。  回复  更多评论
  
# re: Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解实例详解(一) 2011-06-03 11:48 | 秦焜
"Group Id是一个大范围,而Artifact Id是一个小范围"根据spring实例讲解 我更加深了印象。

另外eclipse3.6 maven plugin安装时候 可以从Help->Eclipse Marketplace->Search maven。 好处就是不用记那么多url了。  回复  更多评论
  
# re: Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解实例详解(一) 2012-05-23 23:08 | bj
新建个maven project

Select an Archetype 时候选择了 maven-archetype-webapp

finish的时候 提示我以下的错误

Could not resolve archetype org.apache.maven.archetypes:maven-archetype-webapp:RELEASE from any of the configured repositories.  回复  更多评论
  
# re: Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解实例详解(一) 2012-05-24 10:33 | 菠萝大象
@bj
你装的插件是什么版本?你本地装了maven吗?不要用插件自带的maven,down一个3.0的版本,按我写的步骤做,是没问题的  回复  更多评论
  
# re: Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解实例详解(一) 2012-05-27 11:01 | bj
@菠萝大象
问题解决!谢谢大象!  回复  更多评论
  
# re: Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解实例详解(一)[未登录] 2012-07-09 10:34 |
@bj
@bj
你咋解决的?  回复  更多评论
  
# re: Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解实例详解(一) 2013-03-12 20:18 | 神一样存在
大象你好niubi  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: