1、首先贴出整体的文件架构:(idea开发)
2、controller层
package com.yuanfeng.controller;/** * Created by yuanfeng on 2019/7/9 16:59 */ import com.sun.deploy.net.HttpResponse; import com.yuanfeng.pojo.SysUser; import com.yuanfeng.service.SysUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** *@ClassName SysUserController *@Description T0D0 *@Author yuanfeng *@Date 2019/7/9 16:59 *@Version 1.0 **/ @Controller @Scope("prototype") @RequestMapping("/user") public class SysUserController { @Autowired private SysUserService sysUserService; @RequestMapping("/login") public void login(SysUser sysUser, HttpSession session, HttpServletResponse response, HttpServletRequest request) throws Exception{ SysUser user = sysUserService.login(sysUser); if(user!=null){ session.setAttribute("user", user); response.getWriter().write("<script>location.href='"+request.getContextPath()+"/index.jsp';</script>"); }else{ response.getWriter().write("<script>alert('账号或密码错误!');location.href='"+request.getContextPath()+"/login.jsp';</script>"); } } }
3、service层的实现类
package com.yuanfeng.service.impl;/** * Created by yuanfeng on 2019/7/9 16:58 */ import com.yuanfeng.mapper.SysUserMapper; import com.yuanfeng.pojo.SysUser; import com.yuanfeng.service.SysUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; /** *@ClassName SysUserServiceImpl *@Description T0D0 *@Author yuanfeng *@Date 2019/7/9 16:58 *@Version 1.0 **/ @Service @Transactional public class SysUserServiceImpl implements SysUserService { @Autowired private SysUserMapper sysUserMapper; @Override public SysUser login(SysUser sysUser) { return sysUserMapper.login(sysUser); } }
4、Mapper层
5、applicatioContext.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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring" xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <!--扫描包--> <context:component-scan base-package="com.yuanfeng"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:exclude-filter> </context:component-scan> <!--加载配置文件--> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <!--忽略没有找到的资源文件--> <property name="ignoreResourceNotFound" value="false"></property> <!--配置数据库资源文件--> <property name="locations"> <list> <value>classpath:db.properties</value> </list> </property> </bean> <!--配置数据源--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="${jdbc.driver}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="user" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!--配置session工厂--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!--加载mybatis主配置文件--> <property name="configLocation" value="classpath:mybatis-config.xml"></property> <!--加载Mapper的配置文件--> <property name="mapperLocations" value="classpath:com/yuanfeng/mapper/*.xml"></property> <!--给pojo层所有bean取别名--> <property name="typeAliasesPackage" value="com.yuanfeng.pojo"></property> </bean> <!--Spring框架会自动为Mapper层自动生成代理,也就是说Mapper自动创建对象--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.yuanfeng.mapper"></property> </bean> <!--配置事务管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!--开启注解支持--> <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven> </beans>
6、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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 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.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 配置扫描控制层包 --> <context:component-scan base-package="com.yuanfeng.controller" /> <mvc:annotation-driven/> <!-- 放开静态资源 方式1: <mvc:resources location="/media/" mapping="/media/**"></mvc:resources> --> <!-- 放开静态资源 方式2: --> <mvc:default-servlet-handler/> </beans>
耐心整理,且赞且珍惜!