同样数据库中先建表
DROP TABLE IF EXISTS `admin`;
CREATE TABLE `admin` (
`adminPassword` varchar(20) DEFAULT NULL,
`adminName` varchar(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
下图为整体结构:
要使用的包请自行百度
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 service.AdminService;@Controller
public class AdminController {
@Autowired
private AdminService adminService;
@RequestMapping(value="/login.do")
public ModelAndView login(Admin admin, HttpSession session) {
ModelAndView mv = new ModelAndView();
//登录
Admin a = adminService.login(admin);
//登录验证
if(a != null) { //成功
session.setAttribute("admin", a);
mv.setViewName("/success.jsp");
} else { //失败
String msg = "用户名或密码错误";
mv.addObject("msg",msg);
mv.setViewName("/login.jsp");
}
return mv;
}
@RequestMapping(value="/register.do")
public ModelAndView register(Admin admin, HttpSession session) {
ModelAndView mv = new ModelAndView();
//登录
int a = adminService.register(admin);
if(a==1){
session.setAttribute("admin",admin);
mv.setViewName("/regsucce.jsp");
}else {
String msg = "注册失败,请重试!";
mv.addObject("msg",msg);
mv.setViewName("/reg.jsp");
}
return mv;
}
@RequestMapping(value="/logout.do")
public ModelAndView logout(HttpSession session) {
ModelAndView mv = new ModelAndView();
session.removeAttribute("admin");
mv.setViewName("/login.jsp");
return mv;
}
}
//登录
Admin findAdminByNameAndPwd(Admin admin);
//注册
int register(Admin admin);
}
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--
使用Mapper代理方式进行开发,namespace要和Mapper接口的全类名一致
-->
<mapper namespace="mapper.AdminMapper">
<select id="findAdminByNameAndPwd" parameterType="pojo.Admin" resultType="admin">
select * from admin where adminName=#{adminName} and adminPassword=#{adminPassword}
</select>
<insert id="register" parameterType="pojo.Admin">
<!--
keyProperty:返回的主键存放在pojo对象的什么位置
order="AFTER" 在插入操作运行完成之后执行
resultType:返回的主键的类型
-->
<selectKey keyProperty="id" order="AFTER" resultType="int">
select LAST_INSERT_ID()
</selectKey>
insert into admin(adminName,adminPassword) values(#{adminName},#{adminPassword})
</insert>
</mapper>
Admin login(Admin admin);
//注册
int register(Admin admin);
}
import org.springframework.stereotype.Service;
import service.AdminService;
@Service(value="adminService")
public class AdminServiceImpl implements AdminService{
@Autowired
private AdminMapper adminMapper;
public Admin login(Admin admin) {
return adminMapper.findAdminByNameAndPwd(admin);
}
public int register(Admin admin) {
return adminMapper.register(admin);
}
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driverName}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="acquireIncrement" value="${acquireIncrement}"></property>
<property name="initialPoolSize" value="${initialPoolSize}"></property>
<property name="minPoolSize" value="${minPoolSize}"></property>
<property name="maxPoolSize" value="${maxPoolSize}"></property>
</bean>
<!-- 配置sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:SqlMapConfig.xml"></property>
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--
使用包扫描的方式批量引入Mapper,扫描后引用的时候可以使用类名,首字母小写
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指定要扫描的包的全路径名称,如果有多个包用英文状态下的逗号分隔 -->
<property name="basePackage" value="mapper"></property>
</bean>
<!--
开启注解扫描,扫描所有service包下的注解
-->
<context:component-scan base-package="service"></context:component-scan>
</beans>jdbc.driverName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/admin
jdbc.username=用户名
jdbc.password=密码
acquireIncrement=3
initialPoolSize=10
minPoolSize=2
maxPoolSize=10
log4j.rootLogger=DEBUG,stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.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.xsd">
-->
<context:component-scan base-package="controller"></context:component-scan>
</beans>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<package name="pojo"/>
</typeAliases>
</configuration>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login</title>
</head>
<body>
${msg}
<form action="${pageContext.request.contextPath}/admin/login.do" method="post">
用户名<input type="text" name="adminName" /><br/>
密码<input type="password" name="adminPassword" /><br/>
<input type="submit" value="登录" />
<a href="${pageContext.request.contextPath}/reg.jsp">注册</a>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>register</title>
<script language="javascript" type="">
function RegSubmit(){
var user = document.reg.adminName.value;
var pwd = document.reg.adminPassword.value;
if(user==null||user==""){
alert("请填写用户名");
document.getElementById("adminName").focus;
}else if(pwd==null||pwd==""){
alert("密码不能为空");
}else document.reg.submit();
}
</script>
</head>
<body>
${msg}
<form name="reg" action="${pageContext.request.contextPath}/admin/register.do" method="post">
用户名<input type="text" name="adminName" /><br/>
密码<input type="password" name="adminPassword" /><br/>
<input type="button" value="注册" onclick="RegSubmit()"/>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
恭喜您,注册成功!<br/>
<a href="${pageContext.request.contextPath}/login.jsp">登录</a>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
恭喜您,登录成功!
欢迎${sessionScope.admin.adminName}<br/>
当前您的用户信息为:<br/>
用户名:${sessionScope.admin.adminName}
密码:${sessionScope.admin.adminPassword}
<form action="${pageContext.request.contextPath}/admin/logout.do" method="post">
<input type="submit" value="注销" />
</form>
</body>
</html>