小弟刚学习ssm,整合时候遇到困难,始终没有头绪,望各位大佬指点迷津,真诚地感谢

村里老人组的希望 2018-01-31 02:13:45
package top.airprogram.controller;


import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@Controller
@RequestMapping("user") //请求路径一旦带有user就将请求分发到UserController
public class UserController {


@Autowired
private StuService stuService;

@RequestMapping("/text")
public String testUser(@RequestBody student student)
{
stuService.createStu(student);
return "user";
}


@RequestMapping("/demo")
public String UserDemo()

{
return "demo";
}

}

---------------------------------------------------------------------------------------------------
拦截器
package top.airprogram.interceptor;

public class SpringMvcInterceptor implements HandlerInterceptor {
@Override //请求往controller发之前执行,false则请求打住不发往Controller,
// 只有返回true postHandle方法和afterCompletion方法才会执行,请求才会发到controller
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
return true;
}

@Override//本方法Controller执行后执行
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
System.out.println("请求执行了");

}

@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
System.out.println("请求处理完了");
}
}
----------------------------------------------------------------------------------------------------
jdbc.properties


jdbc.driver=com.mysql.jdbc.Driver
validationQuery = SELECT 1
jdbc.url= jdbc:mysql://localhost:3306/mysql_demo?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
jdbc.username=root
jdbc.password=1234

---------------------------------------------------------------------------------------------
spring配置文件
<?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: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-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"
>

<aop:aspectj-autoproxy proxy-target-class="true"/><!--开启切面aop-->

<context:annotation-config/><!--对spring的注解支持-->

<context:component-scan base-package="top.airprogram"/>

<!--声明jdbc.properties-->
<context:property-placeholder location="classpath:top/airprogram/resource/jdbc.properties"/>

<!--把mybatis配置到spring配置中-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/> <!--引用数据源是阿里druid-->



<!--声明mapper.xml位置-->
<property name="mapperLocations" value="classpath:top/airprogram/dao/**.xml"/>
</bean>

<!--扫描mapper接口-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="top.airprogram.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>

<!--管理事务-->
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><!--事务管理器-->
<property name="dataSource" ref="dataSource"/>
</bean>




<!--数据库设置-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close" init-method="init">
<property name="url" value="${jdbc_url}"/>
<property name="username" value="${jdbc_username}"/>
<property name="password" value="${jdbc_password}"/>
<!-- 初始化连接大小 -->
<property name="initialSize" value="5"/>
<!-- 连接池最大使用连接数量 -->
<property name="maxActive" value="20"/>
<!-- 连接池最小空闲 -->
<property name="minIdle" value="0"/>
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="60000"/>


<!-- 试探数据库是否开启,避免数据库因为长时间没有用而关闭产生异常 -->
<property name="validationQuery" value="${validationQuery}"/>



<property name="testOnBorrow" value="false"/>
<property name="testOnReturn" value="false"/>
<property name="testWhileIdle" value="true"/>
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000"/>
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="25200000"/>
<!-- 打开removeAbandoned功能 -->
<property name="removeAbandoned" value="true"/>
<!-- 1800秒,也就是30分钟 -->
<property name="removeAbandonedTimeout" value="1800"/>
<!-- 关闭abanded连接时输出错误日志 -->
<property name="logAbandoned" value="true"/>
<!-- 监控数据库 -->
<!-- <property name="filters" value="stat" /> -->
<property name="filters" value="mergeStat"/>
</bean>


</beans>
-----------------------------------------------------------------------------------------
springMVC配置
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:contxt="http://www.springframework.org/schema/context"
xsi:schemaLocation=" http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<!--阿里巴巴fastJson需在此配置-->
<mvc:annotation-driven>
<mvc:message-converters>
<bean id ="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<mvc:annotation-driven/> <!--开启注解支持要使用spring mvc中的@Controller注解,就必须要配置<mvc:annotation-driven,-->

<contxt:component-scan base-package="top.airprogram.**"/>

<!--springmvc拦截器配置-->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/user/demo"/> <!--拦截deno-->
<bean class="top.airprogram.interceptor.SpringMvcInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
<bean id = "viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"/> <!--前缀-->
<property name="suffix" value=".jsp"/> <!--后缀-->
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>

</bean>


<!--2 知识点二 文件上传下载springMVC文件上传下载配置-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="50000"/>
<property name="maxInMemorySize" value="50000"/>
<property name="defaultEncoding" value="utf-8"/>

</bean>
</beans>
---------------------------------------------------------------------
service层
接口
package top.airprogram.service;

import org.springframework.stereotype.Service;
import top.airprogram.entity.student;

public interface StuService {
int createStu(student student);
}


实现类
package top.airprogram.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.airprogram.entity.student;
import top.airprogram.dao.*;


@Service("stuserviceimpl") //标注service层并命名
public class StuServiceImpl implements StuService {
@Autowired
private studentMapper studentMapper;


@Override
public int createStu(student student) {
studentMapper.insertSelective(student);
return 0;
}
}

字数限制,dao,entity包没有贴出来,应该不关他们事吧,嘎嘎,然后基于以上一直出现以下异常,网上多是说缺注解或者包路径错,小弟不才找不出来,希望各位大牛教教谢谢。。。
.BeanCreationException: Error creating bean with name 'stuserviceimpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private top.airprogram.dao.studentMapper top.airprogram.service.StuServiceImpl.studentMapper; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [top.airprogram.dao.studentMapper] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
BeanCreationException: Could not autowire field: private top.airprogram.dao.studentMapper top.airprogram.service.StuServiceImpl.studentMapper; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [top.airprogram.dao.studentMapper] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
...全文
516 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
oO临时工Oo 2018-02-01
  • 打赏
  • 举报
回复
应该是你有个类叫studentMapper,这个类上没有加@Service("studentMapper")
  • 打赏
  • 举报
回复
加上也错
qq_41228556 2018-01-31
  • 打赏
  • 举报
回复
studentMapper 是不是没有写注解@Repository,这个类提示有问题

51,412

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧