springmvc+mybatis

Love_Song 发布于 2014/11/28 18:54
阅读 619
收藏 0
十一月 28, 2014 10:19:54 上午 org.apache.catalina.core.AprLifecycleListener init
信息: Loaded APR based Apache Tomcat Native library 1.1.31 using APR version 1.4.8.
十一月 28, 2014 10:19:54 上午 org.apache.catalina.core.AprLifecycleListener init
信息: APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
十一月 28, 2014 10:19:55 上午 org.apache.tomcat.util.digester.SetPropertiesRule begin
警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:qingketong' did not find a matching property.
十一月 28, 2014 10:19:55 上午 org.apache.catalina.core.AprLifecycleListener initializeSSL
信息: OpenSSL successfully initialized (OpenSSL 1.0.1h 5 Jun 2014)
十一月 28, 2014 10:19:55 上午 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["http-apr-8080"]
十一月 28, 2014 10:19:55 上午 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["ajp-apr-8009"]
十一月 28, 2014 10:19:55 上午 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 1452 ms
十一月 28, 2014 10:19:55 上午 org.apache.catalina.core.StandardService startInternal
信息: Starting service Catalina
十一月 28, 2014 10:19:55 上午 org.apache.catalina.core.StandardEngine startInternal
信息: Starting Servlet Engine: Apache Tomcat/7.0.56
十一月 28, 2014 10:19:59 上午 org.apache.catalina.core.ApplicationContext log
信息: No Spring WebApplicationInitializer types detected on classpath
十一月 28, 2014 10:19:59 上午 org.apache.catalina.core.ApplicationContext log
信息: Initializing Spring root WebApplicationContext
log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.
十一月 28, 2014 10:20:01 上午 org.apache.catalina.core.StandardContext listenerStart
严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'userService' is defined
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:306)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:384)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4994)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5492)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1575)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1565)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'userService' is defined
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:529)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1095)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:277)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:442)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:416)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:549)
    at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:159)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:303)
    ... 22 more

十一月 28, 2014 10:20:01 上午 org.apache.catalina.core.StandardContext startInternal
严重: Error listenerStart
十一月 28, 2014 10:20:01 上午 org.apache.catalina.core.StandardContext startInternal
严重: Context [/qingketong] startup failed due to previous errors
十一月 28, 2014 10:20:01 上午 org.apache.catalina.core.ApplicationContext log
信息: Closing Spring root WebApplicationContext
十一月 28, 2014 10:20:01 上午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-apr-8080"]
十一月 28, 2014 10:20:01 上午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["ajp-apr-8009"]
十一月 28, 2014 10:20:01 上午 org.apache.catalina.startup.Catalina start
信息: Server startup in 5478 ms

______________________________________________________________________

上面是我的错误信息;

web.xml :

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">

<servlet>
        <servlet-name>ActionServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/META-INF/spring/spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>ActionServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:META-INF/spring/spring-*.xml **/spring-*.xml</param-value>
    </context-param>
    
    <welcome-file-list>
        <welcome-file>ftl/index.jsp</welcome-file>
    </welcome-file-list>

</web-app>

spring-mvc.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-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/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd">


    <!-- 开启注解 -->
    <context:annotation-config base-package="com.dlyicai.action" />
    
    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

    <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->     
       <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"     
          p:prefix="/ftl/" p:suffix=".jsp" />     
           
       <bean id="multipartResolver"     
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver"     
          p:defaultEncoding="utf-8" />     

</beans>


spring-comm.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/tx  
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">


   <context:component-scan base-package="com.dlyicai.action" />

    <!-- 配置DataSource数据源 -->
    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <!-- <value>WEB-INF/mail.properties</value> -->
                <value>META-INF/properties/*.properties</value>
                <!-- <value>WEB-INF/oscache.properties</value> -->
            </list>
        </property>
    </bean>
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">
        <property name="driverClass">
            <value>${datasource.driverClassName}</value>
        </property>
        <property name="jdbcUrl">
            <value>${datasource.url}</value>
        </property>
        <property name="user">
            <value>${datasource.username}</value>
        </property>
        <property name="password">
            <value>${datasource.password}</value>
        </property>
        <property name="acquireIncrement">
            <value>${c3p0.acquireIncrement}</value>
        </property>
        <property name="initialPoolSize">
            <value>${c3p0.initialPoolSize}</value>
        </property>
        <property name="minPoolSize">
            <value>${c3p0.minPoolSize}</value>
        </property>
        <property name="maxPoolSize">
            <value>${c3p0.maxPoolSize}</value>
        </property>
        <property name="maxIdleTime">
            <value>${c3p0.maxIdleTime}</value>
        </property>
        <property name="idleConnectionTestPeriod">
            <value>${c3p0.idleConnectionTestPeriod}</value>
        </property>
        <property name="maxStatements">
            <value>${c3p0.maxStatements}</value>
        </property>
        <property name="numHelperThreads">
            <value>${c3p0.numHelperThreads}</value>
        </property>
    </bean>

    <!-- 配置SqlSessionFactoryBean -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="/META-INF/mybatis/mybatis.xml" />
        <!-- mapper和resultmap配置路径 -->
        <property name="mapperLocations">
            <list>
                <!-- 统一管理mybatis的配置文件 -->
                <value>/META-INF/mybatis/**/mybatis-*.xml</value>
            </list>
        </property>
    </bean>

    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <!-- <property name="dataSource" ref="dataSource"></property> -->
        <constructor-arg index="0" ref="sqlSessionFactory" />
    </bean>

    <bean name="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <tx:advice id="userTxAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED" read-only="false"
                rollback-for="java.lang.Exception" />
        </tx:attributes>
    </tx:advice>

    <aop:config>
        <aop:pointcut id="pc"
            expression="execution(public * com.dlyicai.service.*.*(..))" /> <!--把事务控制在Service层 -->
        <aop:advisor pointcut-ref="pc" advice-ref="userTxAdvice" />
    </aop:config>

</beans>

mybatis.xml :

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!-- 别名 -->
    <typeAliases>
        <!--********************************** 基础数据 ********************************-->
     
      <typeAlias type="com.dlyicai.vo.User" alias="user"/>
    </typeAliases>
    
    <mappers>
        <mapper resource="META-INF/mybatis/mybatis-user.xml" />
    </mappers>
    
</configuration>

mybatis-user.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="com.dlyicai.vo.User">
    
    <resultMap type="user" id="userMap">  
        <id property="id" column="id" />  
        <result property="userid" column="userid" />  
        <result property="uname" column="uname" />
        <result property="userpwd" column="userpwd" />  
        <result property="mail" column="mail" />  
        <result property="sex" column="sex" />
    </resultMap>
    
    
    <select id="login" parameterType="java.util.Map" resultMap="userMap">
     <![CDATA[   
        select id,userid,uname,userpwd,mail,sex from qingketongUser u
        where u.userid = #{userid} and u.userpwd = #{userpwd} and u.enable=1
       ]]>   
    </select>
    
    <insert id="addUser" parameterType="com.dlyicai.vo.User" >
    insert into qingketongUser
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="userid != null" >
        userid,
      </if>
      <if test="uname != null" >
        uname,
      </if>
      <if test="userpwd != null" >
        userpwd,
      </if>
      <if test="mail != null" >
        mail,
      </if>
      <if test="sex != null" >
        sex,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=NUMBER},
      </if>
      <if test="userid != null" >
        #{userid,jdbcType=VARCHAR},
      </if>
      <if test="uname != null" >
        #{uname,jdbcType=VARCHAR},
      </if>
      <if test="userpwd != null" >
        #{userpwd,jdbcType=NUMBER},
      </if>
      <if test="mail != null" >
        #{mail,jdbcType=VARCHAR},
      </if>
      <if test="sex != null" >
        #{sex,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
    
</mapper>

dao 类:

package com.dlyicai.dao.impl;

import java.util.Map;

import javax.annotation.Resource;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;

import com.dlyicai.dao.UserDao;
import com.dlyicai.vo.User;

@Repository("userDao")
public class UserDaoImpl implements UserDao {

    private SqlSession sqlSession;

    public SqlSession getSqlSession() {
        return sqlSession;
    }
    @Resource(name = "sqlSession")
    public void setSqlSession(SqlSession sqlSession) {
        this.sqlSession = sqlSession;
    }

    @Override
    public int saveUser(User user) {

        sqlSession.insert("addUser", user);
        return user.getId();
    }


    @Override
    public User login(Map<String, Object> map){
        return  (User) sqlSession.selectOne("login", map);
    }



}

service类:

package com.dlyicai.service.impl;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dlyicai.dao.UserDao;
import com.dlyicai.service.UserService;
import com.dlyicai.vo.User;

@Service("userService")
public class UserServiceImpl implements UserService {

    private UserDao userDao;

    public UserDao getUserDao() {
        return userDao;
    }

    @Resource(name = "userDao")
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }


    @Override
    public void addUser(User user) {
        
        this.userDao.saveUser(user);
        
    }

    @Override
    public User login(Map<String,Object> map){
        
        return this.userDao.login(map);
    }


}

Controller类:

package com.dlyicai.action;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.dlyicai.service.UserService;
import com.dlyicai.vo.User;

@Controller
@RequestMapping("/user")
public class UserController {

    private UserService userService;

    public UserService getUserService() {
        return userService;
    }
    
    @Resource(name = "userService")
    public void setUserService(UserService userService) {
        this.userService =userService;
    }
    
    

    @RequestMapping(method = RequestMethod.POST,value="/addUser")
    public String createUser(User user,HttpServletRequest request,Model model)
            throws Exception {
            
        user.setUserid( request.getParameter("userid"));
        user.setUname(request.getParameter("uname")) ;
        user.setUserpwd(request.getParameter("userpwd"));
        user.setEmail(request.getParameter("email"));
        user.setSex(request.getParameter("sex"));
    
        userService.addUser(user);
        System.out.print("11111111111111111111111111111111111111");
        return "/index";
    }
    

    @RequestMapping(method = RequestMethod.POST, value = "/login")
    public String login(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {
        
        String userId = request.getParameter("userid");
        String password = request.getParameter("userpwd");

        //String valCode  =request.getParameter("valCode");
        //String sessionValCode = request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY).toString().toLowerCase();
/*        if(!valCode.toLowerCase().equals(sessionValCode)){
            
            request.setAttribute("errorInfo", "您输入的验证码不正确。");
            request.setAttribute("username", userId);
            return "login";
        }*/
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("uid", userId);
        map.put("password", password);
        User user = userService.login(map);
        if(user==null){
            System.out.print("2222222222222222222222222222222222222222");    
            request.setAttribute("errorInfo", "您输入的用户名或密码不正确。");
            request.setAttribute("username", userId);
            return "login";
        }else{
        
            return "redirect:/index";
        }

    }
    
    

}












加载中
0
ddatsh
ddatsh

component scan

base-package 两个都在com.xx.action,service木扫到

另要注意 include 和exclude controller相关

0
L
Love_Song
有解决办法吗、
0
lgscofield
lgscofield
在spring,applicationxotext配置文件中加入service包的扫描,排除controller包
返回顶部
顶部