文件一:login.jsp
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/jsp/taglibs.jsp" %>
<html>
<head>
<title>${basePath}</title>
</head>
<body>
<%--点击登录之后,如果成功,跳转到首页,如果失败跳回登录页面,并提示失败原因--%>
<%--表单里面有地址action、传输方式post、有参数username、password--%>
<%--触发表单提交,--%>
<form style="width: 300px;margin: 200px auto 0;" method="post" action="${basePath}/logintest">
账号:<input type="text" name="username" value="${user.username}">
<br>
密码:<input type="password" name="password" value="${user.password}">
<br>
<input type="submit" value="登录">
<br>
<span style="color: red;">${msg}</span>
</form>
</body>
</html>
文件二:UserController.java
截取部分关键代码
@RequestMapping("/logintest")
public String loginTest(Model model, @RequestParam("username") String name, @RequestParam("password") String password) {
User user = userDao.getUserByName(name);
if (user == null) {
model.addAttribute("msg", "不存在该用户");
return "login";
}
if (!user.getPassword().equals(password)) {
model.addAttribute("msg", "账号或者密码错误");
return "login";
}
return "index";
}
特别注意第二个判断语句,因为要判断的是账号或密码错误的情况,所以equals函数进行比较后,结果应该取非。
文件三:UserMapper.xml
数据库操作
<select id="getUserByName" resultType="User">
select * from tb_user where `username` = #{name}
</select>
特别注意,username最好加·username·,小的左斜杠。
文件四:IUserDao.java
接口
package com.zhongruan.web_demo.dao;
import com.zhongruan.web_demo.entity.User;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
public interface IUserDao {
List<User> getUsers();
User getUserByName(@Param("name") String username);
User getUserById2(String username);
int updateUser(User user);
int addUser(User user);
int deleteUser(User user);
}
文件五: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"
xsi:schemaLocation="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.xsd
">
<!-- 1.注解扫描位置-->
<context:component-scan base-package="com.zhongruan.web_demo.controller" />
<!-- 2.配置映射处理和适配器-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
<!-- 3.视图的解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
文件六: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"
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.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">
<!-- 1.配置数据库相关参数properties的属性:${url} -->
<context:property-placeholder location="classpath:properties/db.properties"/>
<!-- 2.配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxPoolSize" value="30"/>
<property name="minPoolSize" value="2"/>
</bean>
<!-- 3.配置SqlSessionFactory对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 扫描bean包 使用别名 -->
<property name="typeAliasesPackage" value="com.zhongruan.web_demo"/>
<!--配置加载映射文件 UserMapper.xml-->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
<!--配置mybatis配置文件位置-->
<!-- <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"/>-->
</bean>
<!-- 自动生成dao,mapper-->
<!-- 4.配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 给出需要扫描Dao接口包 -->
<property name="basePackage" value="com.zhongruan.web_demo.dao"/>
<!-- 注入sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<!--自动扫描-->
<context:component-scan base-package="com.zhongruan.web_demo"/>
<!-- 配置事务-->
<!-- 5.配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 6.开启事务注解-->
<tx:annotation-driven/>
</beans>
其中大部分代码都是在仿照老师或跟随老师进行编写的,还需要进一步的消化。