當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring整合Mybatis-完成用户登录
生活随笔
收集整理的這篇文章主要介紹了
Spring整合Mybatis-完成用户登录
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
①導入的jar包:
②在src下創建并配置applicationcontext.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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><!--配置DataSource的bean對象:存儲數據庫連接參數--><bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="url" value="jdbc:mysql://localhost:3306/spring4?characterEncoding=utf8"></property><property name="driverClassName" value="com.mysql.jdbc.Driver"></property><property name="username" value="root"></property><property name="password" value="root"></property></bean><!--配置SQLSessionFactory的bean對象:生產SQLSession--><bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="datasource"></property></bean><!--配置Mapper掃描的bean對象:使用SQLSession掃描mapper包獲取Mapper接口的實例化對象--><bean id="a" class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="sqlSessionFactory" ref="factory"></property><property name="basePackage" value="com.java.mapper"></property></bean><!--配置service對象--><bean id="us" class="com.java.service.UserServiceImpl"><property name="um" ref="userMapper"></property></bean> </beans>③在web.xml文件中配置Spring容器對象的配置文件路徑
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"version="3.0"><!--配置全局參數:被項目中所有servlet共享--><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationcontext.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener> </web-app>④在Servlet的init方法中完成初始化資源的加載(從Spring容器對象中獲取業務層對象)
package com.java.controller;import com.java.pojo.User; import com.java.service.UserService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.request.async.WebAsyncUtils; import org.springframework.web.context.support.WebApplicationContextUtils;import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException;@WebServlet(value = "/user", loadOnStartup = 1) public class UserServlet extends HttpServlet {UserService userService;//在初始化方法中完成Spring容器資源的加載@Overridepublic void init() throws ServletException {//獲取spring容器對象ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());//獲取業務層對象userService = (UserService) ac.getBean("us");}@Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {req.setCharacterEncoding("utf-8");resp.setContentType("text/html;charset=utf-8");resp.setCharacterEncoding("utf-8");String uname = req.getParameter("uname");String pwd = req.getParameter("pwd");//處理請求//調用業務層方法User user = userService.userLoginService(uname, pwd);//響應結果//直接響應if (user != null) {resp.getWriter().write("登錄成功");} else {resp.getWriter().write("登錄失敗");}} }⑤在業務層中聲明mapper層的屬性,并聲明對應的get/set方法
package com.bjsxt.service; import com.bjsxt.mapper.UserMapper; import com.bjsxt.pojo.User;public class UserServiceImpl implements UserService{private UserMapper um;public UserMapper getUm() {return um;}public void setUm(UserMapper um) {this.um = um;}@Overridepublic User userLoginService(String uname, String pwd) {User user = um.userLoginMapper(uname, pwd);return user;} }⑥正常完成功能開發即可
總結
以上是生活随笔為你收集整理的Spring整合Mybatis-完成用户登录的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 做设计电脑配置什么好?
- 下一篇: SpringIOC的创建对象的单例多例模