道:
@Repository
public interface UserDao {
int searchAccount(String account);
int getPurview(User user);
}
服务:
public interface LoginService {
int judgeForUser(User user);
boolean searchAccount(String account);
}
ServiceImpl:
@Service
public class LoginServiceImp implements LoginService {
@Resource
private UserDao userDao;
@Override
public boolean searchAccount(String account) {
if (userDao.searchAccount(account) == 1){
return true;
}
return false;
}
@Override
public int judgeForUser(User user) {
return userDao.getPurview(user);
}
}
映射:
<?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="dao.UserDao">
<select id="searchAccount" parameterType="String" resultType="int">
select 1 from users where account = #{account} limit 1
</select>
<select id="getPurview" parameterType="model.User">
select purview from users where account = #{account} and password = #{password}
</select>
</mapper>
的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mybatis.xml</param-value>
</context-param>
<!-- Spring MVC servlet -->
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:mvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<session-config>
<session-timeout>15</session-timeout>
</session-config>
</web-app>
MVC-servlet.xml中:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="controller"/>
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
<bean id="viewResolver" class="org.thymeleaf.spring4.view.ThymeleafViewResolver"
p:templateEngine-ref="templateEngine"
p:characterEncoding="UTF-8"/>
<bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolvers">
<set>
<ref bean="templateResolver"/>
</set>
</property>
</bean>
<bean id="templateResolver" class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver"
p:prefix="/WEB-INF/pages/"
p:templateMode="HTML"/>
<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine"/>
<property name="characterEncoding" value="UTF-8"/>
</bean>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonHttpMessageConverter"/>
</list>
</property>
</bean>
<bean id="jsonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
p:maxInMemorySize="3000000"/>
</beans>
弹簧mybatis.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="dao"/>
<context:component-scan base-package="mapping"/>
<context:component-scan base-package="service"/>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:dbconfig.properties"/>
</bean>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
p:password="${jdbc.password}"
p:filters="stat"
p:maxActive="8"
p:initialSize="1"
p:maxWait="60000"
p:minIdle="1"
p:timeBetweenEvictionRunsMillis="60000"
p:minEvictableIdleTimeMillis="30000"
p:validationQuery="SELECT 'x'"
p:testWhileIdle="true"
p:testOnBorrow="false"
p:testOnReturn="false"/>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:mapping/*.xml"/>
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<value>
helperDialect=mysql
</value>
</property>
</bean>
</array>
</property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
p:basePackage="dao"
p:sqlSessionFactoryBeanName="sqlSessionFactory"/>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="dataSource"/>
</beans>
我跑的时候。它显示了这个错误,我不知道它为什么不能创建bean?!
错误:
org.springframework.web.context.ContextLoader.initWebApplicationContext 上下文初始化失败 org.springframework.beans.factory.BeanCreationException:错误 创建名为&#39; listViewServiceImp&#39;:注入资源的bean 依赖失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:错误 创建名为&#39; IListViewDao&#39;在文件中定义 [/Users/Mario.Hu/Documents/mybatis/target/mybatis-1.0-SNAPSHOT/WEB-INF/classes/dao/IListViewDao.class]: 无法解析对bean&#39; sqlSessionFactory&#39;的引用在设置 bean属性&#39; sqlSessionFactory&#39 ;;嵌套异常是 org.springframework.beans.factory.BeanCreationException:错误 创建名为&#39; sqlSessionFactory&#39;的bean在类路径中定义 resource [spring-mybatis.xml]:bean的初始化失败;嵌套 异常是org.springframework.beans.TypeMismatchException:失败 转换类型&#39; java.lang.String&#39;的属性值要求的类型 &#39; org.springframework.core.io.Resource []&#39;对于财产 &#39; mapperLocations&#39 ;;嵌套异常是 java.lang.IllegalArgumentException:无法解析资源 location pattern [classpath:mapping / .xml]:类路径资源 [mapping /]无法解析为URL,因为它不存在于 org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:321) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264) 引起:org.springframework.beans.factory.BeanCreationException: 创建名称为&#39; IListViewDao&#39;的bean时出错在文件中定义 [/Users/Mario.Hu/Documents/mybatis/target/mybatis-1.0-SNAPSHOT/WEB-INF/classes/dao/IListViewDao.class]: 无法解析对bean&#39; sqlSessionFactory&#39;的引用在设置 bean属性&#39; sqlSessionFactory&#39 ;;嵌套异常是 org.springframework.beans.factory.BeanCreationException:错误 创建名为&#39; sqlSessionFactory&#39;的bean在类路径中定义 resource [spring-mybatis.xml]:bean的初始化失败;嵌套 异常是org.springframework.beans.TypeMismatchException:失败 转换类型&#39; java.lang.String&#39;的属性值要求的类型 &#39; org.springframework.core.io.Resource []&#39;对于财产 &#39; mapperLocations&#39 ;;嵌套异常是 java.lang.IllegalArgumentException:无法解析资源 location pattern [classpath:mapping / .xml]:类路径资源 [mapping /]无法解析为URL,因为它不存在于 org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359) 引起:org.springframework.beans.factory.BeanCreationException: 创建名为&#39; sqlSessionFactory&#39;的bean时出错在课堂上定义 path资源[spring-mybatis.xml]:bean的初始化失败; 嵌套异常是org.springframework.beans.TypeMismatchException: 无法转换类型&#39; java.lang.String&#39;的属性值至 必需的类型&#39; org.springframework.core.io.Resource []&#39;对于财产 &#39; mapperLocations&#39 ;;嵌套异常是 java.lang.IllegalArgumentException:无法解析资源 location pattern [classpath:mapping / .xml]:类路径资源 [mapping /]无法解析为URL,因为它不存在于 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564) 引起:org.springframework.beans.TypeMismatchException:失败 转换类型&#39; java.lang.String&#39;的属性值要求的类型 &#39; org.springframework.core.io.Resource []&#39;对于财产 &#39; mapperLocations&#39 ;;嵌套异常是 java.lang.IllegalArgumentException:无法解析资源 location pattern [classpath:mapping / .xml]:类路径资源 [mapping /]无法解析为URL,因为它不存在于 org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:610) 引起:java.lang.IllegalArgumentException:无法解决 资源位置模式[classpath:mapping / * .xml]:类路径 资源[mapping /]无法解析为URL,因为它没有 存在于 org.springframework.core.io.support.ResourceArrayPropertyEditor.setAsText(ResourceArrayPropertyEditor.java:116) 2017年5月27日23:35:23.398严重[RMI TCP连接(2)-127.0.0.1] org.apache.catalina.core.StandardContext.startInternal一个或多个 听众未能开始。完整的详细信息将在 适当的容器日志文件27-May-2017 23:35:23.420严重[RMI TCP 连接(2)-127.0.0.1] org.apache.catalina.core.StandardContext.startInternal Context [] 由于以前的错误启动失败
为什么呢?我跟随其他人来构建这些代码!但它对我不起作用!
答案 0 :(得分:0)
从错误消息判断,似乎无法在类路径中找到资源目录映射。
或者在spring-mybatis.xml中使用以下内容(注意额外的*)
<property name="mapperLocations" value="classpath*:mapping/*.xml"/>
如果未找到,则不会返回异常。请查看此link
的更多信息