介绍
如何在springmvc中使用弹簧与mybatis实现一个用户登录功能?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
web。xml
& lt; !——, Spring MVC 核心控制器,DispatcherServlet 配置,——比; ,& lt; servlet> & lt;才能servlet-name> dispatcher</servlet-name> & lt;才能servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class> & lt;才能init-param> ,,& lt; !——用于标明spring-mvc.xml配置的位置,我是存放在配置文件夹下——比; ,,& lt; param-name> contextConfigLocation ,,& lt; param-value> classpath *:配置/spring-mvc.xml & lt;才能/init-param> & lt;才能load-on-startup> 1 & lt;/load-on-startup> ,& lt;/servlet> ,& lt; servlet-mapping> & lt;才能servlet-name> dispatcher</servlet-name> & lt;才能!——,拦截所有* .do 的请求,交给DispatcherServlet处理,性能最好,——比; & lt;才能url-pattern> * .do ,& lt;/servlet-mapping> & lt;才能!——用于设定默认首页——比; ,& lt; welcome-file-list> & lt;才能welcome-file> login.jsp</welcome-file> ,& lt;/welcome-file-list>
配置完后,我们需要在对springmvc框架进行配置,配置文件名为spring mvc。xml,也是存放在配置文件夹下:
& lt; beans xmlns=癶ttp://www.springframework.org/schema/beans" xmlns:才能上下文=癶ttp://www.springframework.org/schema/context" xmlns:才能mvc=癶ttp://www.springframework.org/schema/mvc", xmlns: xsi=癶ttp://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/mvc ,http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"才能的在 ,& lt; !——扫描控制器,当配置了它后,春天会自动的到com.mjl.controller ,下扫描带有@controller @service @ component等注解等类,将他们自动实例化——比; ,& lt;上下文:component-scan 基础包=癱om.mjl.controller",/比; ,& lt; !——& lt; mvc: annotation-driven /祝辞,会自动注册DefaultAnnotationHandlerMapping与 ,AnnotationMethodHandlerAdapter 两个bean,它解决了一些@controllerz注解使用时的提前配置——比; ,& lt; mvc: annotation-driven /比; ,& lt; !——配置,页面控制器——比; ,& lt; bean 类=皁rg.springframework.web.servlet.view.InternalResourceViewResolver"比; & lt;才能property name=皃refix" https://www.yisu.com/zixun/, value="/"/> <属性name=value=昂笞骸sp "/> 豆> 豆>
当springmvc配置完成后,就需要编写业务啦,也就是服务包下的东西,首先编写一个接口类userservice,里面存放了我们抽象出来的登录方法登录
package com.mjl.service; import org.springframework.ui.Model;/* * ,* Created by  alvin 提醒15/9/7。 ,*/public interface  UserService { ,public boolean 登录(String  String 用户名,密码); }
然后在创建一个userservice的实现类userserviceimpl用于实现我们所抽象出来的登录方法
package com.mjl.service; import com.mjl.dao.IUserDao; import com.mjl.model.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Service; import org.springframework.ui.Model;/* * ,* Created by  alvin 提醒15/9/7。 ,*///@ service (“UserService"),注解用于标示此类为业务层组件,在使用时会被注解的类会自动由 ,//春进行注入,无需我们创建实例 @ service (“UserService") public class  UserServiceImpl implements UserService  { ,//自动注入iuserdao 用于访问数据库 ,@ autowired ,IUserDao 映射器; ,//登录方法的实现,从jsp页面获取用户名与密码 ,public boolean 登录(用户名、String String 密码),{//,,System.out.println(“输入的账号:“,+,username +,“输入的密码:“,+,密码);//对才能输入账号进行查询,取出数据库中保存对信息 User 才能;User =, Mapper.selectByName(用户名); if 才能;(user !=, null), {//,,,System.out.println(“查询出来的账号:“,+,user.getUsername(), +,“密码:“,+,user.getPassword ());//,,,System.out.println(“- - - - - - - - - -产生绯闻); ,,if (user.getUsername () .equals(用户名),,,,user.getPassword () .equals(密码)) ,,,return 真实; ,,} return 才能;假; ,} }如何在springmvc中使用弹簧与mybatis实现一个用户登录功能