# application.xml spring配置文件内容:
<!-- 引入jdbc配置文件 -->
<context:property-placeholder location="classpath:config/jdbc.properties" />
<!-- 可通过注解控制事务
<tx:annotation-driven />-->
<!-- 扫描注解类,否则找不到bean -->
<context:component-scan base-package="com.zj.service"></context:component-scan>
# springmvc-servlet.xml 文件内容:
<!-- 启用spring mvc 注解
<context:annotation-config />-->
<!-- 设置使用注解的类所在的jar包 -->
<context:component-scan base-package="com.zj.controller"></context:component-scan>
<!--创建jdbc数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${uname}" />
<property name="password" value="${pwd}" />
</bean>
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:com/zj/dao/*.xml"></property>
</bean>
<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.zj.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- 完成请求和注解POJO的映射 -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
# LoginController.java
package com.zj.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.zj.entity.UserInfo;
import com.zj.service.LoginService;
@Controller
public class LoginController {
private LoginService loginService;
public LoginService getLoginService() {
return loginService;
}
@Autowired
public void setLoginService(LoginService loginService) {
this.loginService = loginService;
}
@RequestMapping("/login")
public ModelAndView login(String username,String password,
HttpServletRequest request){
return new ModelAndView("login");
}
@RequestMapping("/login2")
public ModelAndView testLogin2(String username, String password){
UserInfo ui = new UserInfo();
UserInfo result = this.loginService.find(ui);
System.out.println("用户名:"+result.getUsername());
// request和response不必非要出现在方法中,如果用不上的话可以去掉
// 参数的名称是与页面控件的name相匹配,参数类型会自动被转换
if (result==null) {
return new ModelAndView("loginError");
// 手动实例化ModelAndView完成跳转页面(转发),效果等同于上面的方法返回字符串
}else{
return new ModelAndView("loginSuccess");
}
// 采用重定向方式跳转页面 // 重定向还有一种简单写法
// return new ModelAndView("redirect:../index.jsp"); }
}
}
# LoginDao.java
package com.zj.dao;
import com.zj.entity.UserInfo;
public interface LoginDao {
public UserInfo getUserInfo(UserInfo UserInfo);
// public void addUserInfo(UserInfo UserInfo);
//
// public void updateUserInfo(UserInfo UserInfo);
//
// public void deleteUserInfo(int UserInfoId);
}
# LoginDao.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">
SELECT * FROM userinfo WHERE username=#{username} AND password=#{password}
业务实现类LoginServiceImpl.java
package com.zj.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zj.dao.LoginDao;
import com.zj.entity.UserInfo;
import com.zj.service.LoginService;
@Service("loginService")
public class LoginServiceImpl implements LoginService{
private LoginDao loginDao;
public LoginDao getUserDao() {
return loginDao;
}
@Autowired
public void setUserDao(LoginDao userDao) {
this.loginDao = userDao;
}
public UserInfo find(UserInfo userInfo) {
return this.loginDao.getUserInfo(userInfo);
}
}
# web.xml
<?xml version="1.0" encoding="UTF-8"?>
pro
<!-- 不拦截静态文件 -->
default
/js/*
/css/*
/imgs/*
/fonts/*
<!-- Spring配置 -->
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:config/applicationContext.xml
<!-- Spring MVC配置 -->
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:config/springmvc-servlet.xml
1
springmvc
/
<!-- 配置字符集 -->
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
encodingFilter
/*
30
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
最后是错误信息:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.zj.dao.LoginDao] 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)}