设为首页 收藏本站
查看: 1032|回复: 0

[经验分享] SpringMVC与Mybatis整合环境搭建

[复制链接]

尚未签到

发表于 2016-11-26 07:37:06 | 显示全部楼层 |阅读模式
Spring与Mybatis整合环境搭建  
本文用的jar包如下:
DSC0000.jpg
1、下载jar包
springMVC:http://www.springsource.org/download/community
mybatis:http://code.google.com/p/mybatis/wiki/Downloads
准备工作:在MYSQL里新增数据库test,表user
CREATE TABLE user (userid int(4) NOT NULL AUTO_INCREMENT, username varchar(50), password varchar(50), PRIMARY KEY (userid)) ENGINE=InnoDB DEFAULT CHARSET=utf8;


2、Spring mvc的基本配置
a---> web.xml

<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>


b---> 配置数据源文件 database.properties

mysql.database.driver=com.mysql.jdbc.Driver
mysql.database.url=jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=utf-8
mysql.database.user=root
mysql.database.password=******


c---> Spring 核心配置页 applicationContext.xml

<!-- 配置数据库 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:conf/database.properties</value>
</list>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${mysql.database.driver}" />
<property name="url" value="${mysql.database.url}" />
<property name="username" value="${mysql.database.user}" />
<property name="password" value="${mysql.database.password}" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="sqlMapClient" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource" />
</property>
<property name="configLocation" value="classpath:conf/SqlMapConfig.xml"></property>
</bean>
<!-- 把加载了 配置文件的 sqlMapClient 注入 SqlSessionTemplate模板-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlMapClient" />
</bean>
<!-- 注册Mapper:也可不指定特定mapper,而使用自动扫描包的方式来注册各种Mapper ,配置如下:-->  
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
<property name="basePackage" value="com.test.dao" />  
</bean>  

(此文没有配事务,主要还是关心Mybatis的一些配置,详细事务下次再另写文章。)
d---> Spring MVC配置 spring-servlet.xml (放WEB-INF下)

<mvc:annotation-driven/>
<!-- 注解模式 -->
<context:component-scan base-package="com">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Service" />
</context:component-scan>
<!-- 映射jsp -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>


3、Mybatis的一些配置:
a---> 核心配置SqlMapConfig.xml

<configuration>
<!-- 对应一些POJO类 然后可以在mapper中引用Emp 等就等于引用这个类类型-->
<typeAliases>
<typeAlias type="com.test.model.User" alias="User" />
</typeAliases>
<mappers>
<mapper resource="com/test/dao/UserMapper.xml" />
</mappers>
</configuration>


b---> 利用Mybatis工具生成相应的entity,dao,xml等文件,即:

User.java//实体类
UserExample.java//Example对象,Mybatis自身生成的一些基础查询都可以用它查。
UserMapper.java//DAO接口文件
UserMapper.xml//Mybatis增删改查等配置文件

具体的生成方法见博文:http://angelbill3.iyunv.com/blog/1696970
具体用法等有空再写博文吧。
4、至此所有配置+环境都可以用了,写下测试类测试下:
a---> 新建测试Service: TestServiceImpl.java

@Service
public class TestServiceImpl implements TestService{
@Resource
private UserMapper userMapper;
public List<User> getUserList(){
UserExample uEx = new UserExample();
uEx.createCriteria().andUsernameEqualTo("111");
List<User> list = userMapper.selectByExample(uEx);
return list;
}
}

//getUserList() 查询出username为‘111’的用户。(前提得自己在数据库里插入这个用户。
b---> Spring Controller类:TestController.java

@Controller
@RequestMapping("test")
public class TestController {
@Resource
private TestService testService;
@RequestMapping(value = "list")  
public ModelAndView getUserTest(HttpServletResponse response) throws Exception {  
List list = this.testService.getUserList();
System.out.println("listsize:"+list.size());
ModelAndView mav = new ModelAndView();
mav.addObject(list);
mav.setViewName("list");
return mav;  
}
}

打印结果:listsize:1
访问:项目名/test/list.html即可进入此TestController的getUserTest()方法,并将得到的list放到结果页,即项目名/webroot/web-inf/views/list.jsp中。

----------------------------------------------
遇到问题:
在用UserMapper文件的时候,报错:
java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for ......
网上看到的可能导致此类错异常的原因:
The three names have to match:
- interface = com.enlliance.inventory.mappers.SettingMapper.class
- mapper file = /com/enlliance/inventory/mappers/SettingMapper.xml
- mapper namespace = com.enlliance.inventory.mappers.SettingMapper

解决方法:
将UserMapper.xml中的namespace更改如下:
<mapper namespace="com.faj.dao.UserMapper" >
问题解决。
----------------------------------------------
参考资料:
http://blog.sina.com.cn/s/blog_93b15c4d010145i1.html
http://layznet.iyunv.com/blog/1021533
//Mybatis的教程系统文章,很全。
http://legend2011.blog.iyunv.com/3018495/980150
----------------------------------------------
相关资料:
SpringMVC 声明式事务学习以及问题解决http://angelbill3.iyunv.com/blog/1896502
使用mybatis-generator自动生成Mybatis相关代码http://angelbill3.iyunv.com/blog/1696970

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-305610-1-1.html 上篇帖子: MyBatis报Error setting null parameter 的解决方法 下篇帖子: 使用MyBatis_Generator生成Dto、Dao、Mapping
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表