Struts2+Spring3.0+MyBatis3.0平台搭建
2011-08-05 17:44
176 查看
Struts2采用的版本是2.2.3
Spring采用的版本是3.0.5
MyBatis,即是以前的iBatis。采用的版本是3.0.5
我的习惯呢是一个一个来,先让struts在系统上跑起来,Struts2的文档让偶有点生气了,因为在以前的Struts2版本中
![](http://img1.51cto.com/attachment/201108/164332904.gif)
要这5个jar包就可以了,现在在新版中要的jar包更多了,每次更具服务器启动的错误信息去找需要的jar包,不过还好没多久就找全了。将commons-io,commons-lang,commons-fileupload,javassist。加上就差不多可以了。那些个配置文件还都是一样的使用,网上都是很多的,就不多说了。
接着就是加入spring了。这个就先将那些aop,core,orm,jdbc,beans,context,当然还有struts-spring-plugin这个插件jar包加入进来,spring的配置文件当然也不赘述了,启动服务器吧,出了错就一般应该就是没有jar包,挨个挨个找,加进来就可以了。有些人可能就觉得烦了,让我自己一个一个试还看这个干啥。我觉得凡事都要自己亲自动手去坐坐,现在时几个比较常用的框架在网上很容易就能找到,如果是很少人用的呢,又很缺少文档呢,还是得靠自己的慢慢摸索。
配置spring当然在web.xml中少不了这个监听器
![](http://img1.51cto.com/attachment/201108/165501290.jpg)
有了spring当然可以将struts中配置action时需要的对象在spring容器中管理了。
在struts配置action时如下
![](http://img1.51cto.com/attachment/201108/171238849.jpg)
在spring中配置这个action实例,但是请注意,action的scope是prototype,但是在这里就不能偷懒不配置action中调用的service实例
![](http://img1.51cto.com/attachment/201108/171444531.jpg)
再下来就是在这个平台中加入MyBatis了。我也没有接触过这个框架,只是想试试,但是用了之后感觉真得很不错,自己写SQL,但是自己不用去将查询出来的ResultSet封装成对象。
看它官方的文档spring在和它集成是用的是Annotation,但是没看明白,自己比较笨吧,还是使用的是xml文件去配置的。将mybatis.jar和mybatis-spring.jar加入系统就可以了。spring中配置的数据源使用的是DBPooL。spring的配置文件如下:
![](http://img1.51cto.com/attachment/201108/172358382.jpg)
在Configuration.xml文件中只需要配置<mapper></mapper>就可以了。
![](http://img1.51cto.com/attachment/201108/172546148.jpg)
在dao层的类里面为了图方便就直接引用了sqlSessionFactory。上面说到的AccountMapper.xml配置如下
而在dao类中的代码如下:
这里还有个很方便的事情就是不用去配置什么对象属性和表结构的映射关系。
整个系统搭建好了,引入的jar包如下图(里面可能会有多余的jar包,因为在缺少包时往里加包时,肯能往删除了):
![](http://img1.51cto.com/attachment/201108/174225522.jpg)
第一次这样自己写博客,写得不好之处请多见谅。
本文出自 “三毛” 博客,请务必保留此出处http://chnhuang.blog.51cto.com/1923467/632134
Spring采用的版本是3.0.5
MyBatis,即是以前的iBatis。采用的版本是3.0.5
我的习惯呢是一个一个来,先让struts在系统上跑起来,Struts2的文档让偶有点生气了,因为在以前的Struts2版本中
![](http://img1.51cto.com/attachment/201108/164332904.gif)
要这5个jar包就可以了,现在在新版中要的jar包更多了,每次更具服务器启动的错误信息去找需要的jar包,不过还好没多久就找全了。将commons-io,commons-lang,commons-fileupload,javassist。加上就差不多可以了。那些个配置文件还都是一样的使用,网上都是很多的,就不多说了。
接着就是加入spring了。这个就先将那些aop,core,orm,jdbc,beans,context,当然还有struts-spring-plugin这个插件jar包加入进来,spring的配置文件当然也不赘述了,启动服务器吧,出了错就一般应该就是没有jar包,挨个挨个找,加进来就可以了。有些人可能就觉得烦了,让我自己一个一个试还看这个干啥。我觉得凡事都要自己亲自动手去坐坐,现在时几个比较常用的框架在网上很容易就能找到,如果是很少人用的呢,又很缺少文档呢,还是得靠自己的慢慢摸索。
配置spring当然在web.xml中少不了这个监听器
![](http://img1.51cto.com/attachment/201108/165501290.jpg)
有了spring当然可以将struts中配置action时需要的对象在spring容器中管理了。
在struts配置action时如下
![](http://img1.51cto.com/attachment/201108/171238849.jpg)
在spring中配置这个action实例,但是请注意,action的scope是prototype,但是在这里就不能偷懒不配置action中调用的service实例
![](http://img1.51cto.com/attachment/201108/171444531.jpg)
再下来就是在这个平台中加入MyBatis了。我也没有接触过这个框架,只是想试试,但是用了之后感觉真得很不错,自己写SQL,但是自己不用去将查询出来的ResultSet封装成对象。
看它官方的文档spring在和它集成是用的是Annotation,但是没看明白,自己比较笨吧,还是使用的是xml文件去配置的。将mybatis.jar和mybatis-spring.jar加入系统就可以了。spring中配置的数据源使用的是DBPooL。spring的配置文件如下:
![](http://img1.51cto.com/attachment/201108/172358382.jpg)
在Configuration.xml文件中只需要配置<mapper></mapper>就可以了。
![](http://img1.51cto.com/attachment/201108/172546148.jpg)
在dao层的类里面为了图方便就直接引用了sqlSessionFactory。上面说到的AccountMapper.xml配置如下
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="org.entity.Account"> <select id="selectAccount" parameterType="String" resultType="org.entity.Account"> select * from account where username =#{username} </select> <insert id="insertAccount"> insert account (username,password) values (#{username},#{password}) </insert> </mapper>
而在dao类中的代码如下:
private SqlSessionFactory sqlSessionFactory; public final void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) { this.sqlSessionFactory = sqlSessionFactory; } @Override public void selectAccount(String username) { SqlSession sqlSession = sqlSessionFactory.openSession(); Account account = (Account) sqlSession.selectOne("org.entity.Account.selectAccount", "sanmao"); System.out.println(account); sqlSession.close(); }
这里还有个很方便的事情就是不用去配置什么对象属性和表结构的映射关系。
整个系统搭建好了,引入的jar包如下图(里面可能会有多余的jar包,因为在缺少包时往里加包时,肯能往删除了):
![](http://img1.51cto.com/attachment/201108/174225522.jpg)
第一次这样自己写博客,写得不好之处请多见谅。
本文出自 “三毛” 博客,请务必保留此出处http://chnhuang.blog.51cto.com/1923467/632134
相关文章推荐
- Struts2+Spring3.0+MyBatis3.0平台搭建
- Struts2+Spring3.0+MyBatis3.0平台搭建
- Struts2.0+Hibernate2.5+Spring3.0搭建JavaEE项目要用的jar
- MyEclipse搭建SSH框架 Struts2.1+Spring3.0+hibernate3.3
- MyEclipse搭建SSH框架 Struts2.1+Spring3.0+hibernate3.3
- Struts2.1+Spring3.0+Hebernate3.3 MyEclipse搭建SSH
- 如何搭建struts平台
- Myeclipse10下搭建SSH框架(图解)Struts2.1+Spring3.0+Hibernate3.3
- Struts2.1+Hibernate3.1+Spring3.0搭建
- Myeclipse10下搭建SSH框架(图解)Struts2.1+Spring3.0+Hibernate3.3
- Myeclipse10下搭建SSH框架(图解)Struts2.1+Spring3.0+Hibernate3.3
- struts2.1.8+mybatis3.0+spring3.0(全注解)小例子
- Myeclipse10下搭建SSH框架(图解)Struts2.1+Spring3.0+Hibernate3.3
- MyEclipse搭建SSH框架 Struts2.1+Spring3.0+hibernate3.3
- Struts2+Spring3.0+Hibernate3.2框架搭建全程
- Myeclipse8.5下搭建SSH框架(图解)Struts2.1+Spring3.0+Hibernate3.3
- Myeclipse10下搭建SSH框架(图解)Struts2.1+Spring3.0+Hibernate3.3
- struts2.1.8+mybatis3.0+spring3.0(全注解)小例子
- Myeclipse10下搭建SSH框架(图解)Struts2.1+Spring3.0+Hibernate3.3
- Myeclipse10下搭建SSH框架(图解)Struts2.1+Spring3.0+Hibernate3.3