第一范文网 - 专业文章范例文档资料分享平台

建立SpringMvc工程 (2)

来源:用户分享 时间:2020-06-28 本文由云雾渐消散 分享 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

在该配置文件,我们需要配置数据源,Mybatis,事务管理器,拦截方式,注解方式,配置druid这些文件。

在配置Spring-mybatis.xml时出现: cvc-elt.1: Cannot find the declaration of element 'beans'错误。 出现此问题 是spring的jar包与applicationContext.xml中xsd的 版本不同 造成的。 版本不同在联网的情况下不会出现此异常,但当网络断开或者使用内部网络时就会出现此异常。

今天在写spring aop示例的时候,在spring.xml文件中添加spring aop的schema后出现红叉,spring配置文件如下:

[html] view plaincopy

1. 2. 3. 13. 14.

提示说:cvc-elt.1: Cannot find the declaration of element 'beans',

从网上搜了一些,有的说是因为网络原因访问不到xsd文件,因为访问不到网络上的xsd文件,我们可以访问本地的啊,在引入的spring的包中spring-beans-3.2.2.RELEASE.jar中有spring-beans-3.0.xsd文件,其他的xsd文件也都能在相应的包中找到,这样就好说了,把xsd文件的引用全部改为本地文件:

[html] view plaincopy

1. 2. 3. 4. 5. 6. 7. 8. 9. 14. 15.

整合完Spring与Mybatis之后我们先测试一下,我们首先写service接口interface,命名为User,假设我们来查询一个用户,查询用户我们肯定返回一个User,然后写入 Public User getUserById(String idf);

同时我们建立service的实现类UserServiceimpl ,建立UserServiceimpl的时候实现User接口,new—class—interface的Add--找到对应接口。

实现之后,类的代码变成: package com.test.service.impl; import com.test.service.User;

public class UserServiceimpl implements User { public User getUserById(String idf) { // TODO Auto-generated method stub return null; } }

我们这个service是要处理一些业务数据后处理数据库的,在mybatis中处理数据库要调用dao,dao就是mapper,dao只写interface不写实现类,

定义一个usermapper的属性,出现错误提醒,这个时候要将对应的包import进来。

要使用mapper那么就需要Spring注入,这个时候我们需要genertor getters and setters,点击source/Genertor Getters and Setters或者按快捷键Alt+Shift+S就会出现Genertor Getters and Setters选项。

生成如下代码:

public void setUsermapper(UserMapper usermapper) { this.usermapper = usermapper; } 我们如何将usermapper注入到service里呢?

遇到这种错误Autowired cannot be resolved to a type

3.3Junit测试Spring和Mybatis是否整合成功

在src/test/java下建立一个TestMybatis的class来做一个junit的测试,测试Spring和Mybatis是否整合成功,我们要通过junit测试,其实Spring也提供了一个测试的jar包,通过这个jar包可以很方便的测试Spring,

通过new一个classpath来获得一个Spring的上下文,在这里面填写一个Spring的配置文件,因为我把Spring的配置文件分为Spring.xml和Spring-mybatis.xml两个配置文件, 老式的测试方法写法如下: @test

Public void test1(){ ApplicationContext

ac=new ClasspathXmlApplicationContext(new String[]{“Spring.xml”,”Spring-mybatis”}) UserService userService=(UserServiceI)ac.getBean(“userService”); User u=userService.getUserById(“”); System.out.println(u); }

这个时候运行JUnit出现错误,需要将aspectj.jar这个jar包添加进去,有一个测试写一个,

有几个得写几个,每个都需要获得Spring的上下文,那么速度很慢,因为Spring在初始化的过程中很慢,为了改进这种办法,在JUnit配置文件中有一个@Before这样一个注解,这个before类是在所有的测试方法执行之前执行的一个类,那么就可以将所有初始化的东西放到前面,这样执行的时候只会执行一次 @Before

public void before(){ }

错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': Injection of autowired dependencies failed; nested exception is

org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.test.service.impl.UserServiceImpl.setUserMapper(com.test.dao.inter.UserMapper); 说明我们的注解驱动没有扫描进去,那么我们在Spring.xml文件中更改一下扫描路径,可能出现问题,我的问题是:

继续运行,出现错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name

'sqlSessionFactory' defined in class path resource [spring-mybatis.xml]: Invocation of init method failed; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file

3.4SpringMVC配置

测试Spring与Mybatis整合成功之后,导入SpringMvc的jar包,也就是Spring-webmvc.jar包,导入这个jar包之后,肯定有一个spring-mvc的配置文件。

在com.test.controller建立的类,只要再最上面加上controller注解,就可以将该类加入到spring-mvc的上下文中,并知道下面的类为控制器类。

在controller类中光右controller注解是不够的,我们还必须要知道url,controller访问需要加入@RequestMapping(“”);这个东西就是一个访问地址的

问题集锦:

1,遇到The method of type must override a superclass method,提示的是实现类必须实现接口的方法。实现类里面使用了 @Override在1.5下要使用@Override 这个annotation 必须保证 被标注方法来源于class 而不是interface 解决方法:

将项目的jdk从1.5改为1.6及以上

import com.deliver.tools.internal.ws.processor.model.Response; ( package com.deliver.company.filter;)

搜索“diyifanwen.net”或“第一范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,第一范文网,提供最新初中教育建立SpringMvc工程 (2)全文阅读和word下载服务。

建立SpringMvc工程 (2).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/wenku/1106429.html(转载请注明文章来源)
热门推荐
Copyright © 2018-2022 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top