初学SSM试着做了个登录注册实例检验一下,顺便把代码搬上来了
废话不多说直接撸
数据库版本:mysql-8.0.13(版本较高,jar包和一些语法会有出入)
开发工具:Eclipse
整体结构
libs
不是全部的包都要用到,当时图省事就全弄进来了
数据表
俩字段,很简单,只是方便测试
CREATE TABLE test (
name varchar(20) NOT NULL,
pwd varchar(20) DEFAULT NULL,
PRIMARY KEY (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>b8_c301</display-name>
<!-- 加载Spring -->
<context-param>
<param-name>
contextConfigLocation
</param-name>
<param-value>
classpath:applicationContext.xml
</param-value>
</context-param>
<!-- Listener监听 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 加载SpringMvc -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<!-- 配置SpringMVC编码过滤器 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
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:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 开启IOC扫描 -->
<context:component-scan
base-package="com.cn.service">
</context:component-scan>
<!-- 配置数据源 -->
<bean id="dataSource"
class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName"
value="com.mysql.cj.jdbc.Driver" />//注意数据库版本
<property name="url"
value="jdbc:mysql://localhost:3306/library_sys?useSSL=false&serverTimezone=UTC" />//注意数据库版本,8.0版本中url要设置时区,并且xml配置文件中要使用“&”来代替“&”
<property name="username" value="root" />
<property name="password" value="fknoobq3q" />
<!-- Max连接数 -->
<property name="maxTotal" value="30"></property>
<!-- Max空闲连接数 -->
<property name="maxIdle" value="10"></property>
<!-- 初始化连接数 -->
<property name="initialSize" value="5"></property>
</bean>
<!-- sqlSessionFactory -->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 关联连接池 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 加载sql映射文件 -->
<property name="mapperLocations"
value="classpath:com/cn/dao/UserMapper.xml"></property>//直接加载的sql映射文件,省去配置mybatis-config.xml的步骤
</bean>
<!-- Mapper接口扫描 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.cn.dao"></property>
</bean>
<!-- 开启Spring事务 -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 开启事务注解 -->
<tx:annotation-driven
transaction-manager="txManager" />
</beans>
springmvc-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 扫描Controller -->
<context:component-scan
base-package="
com.cn.controller ">
</context:component-scan>
<!-- 注解驱动 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 视图解析器:简化在Controller类编写的视图路径 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 后缀 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>
UserController
package com.cn.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.cn.entiy.User;
import com.cn.service.UserService;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/golog")
public String golog() {
return "login";
}
@RequestMapping("/goreg")
public String goreg() {
return "register";
}
@RequestMapping("/register")
public String login(User user) {
userService.saveUser(user);
return "succ";
}
@RequestMapping("/login")
public String register(String name,String pwd) {
User user=userService.findUser(name, pwd);
if(user!=null) {
return "succ";
}else {
return "fail";
}
}
}
UserMapper
package com.cn.dao;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import com.cn.entiy.User;
@Repository
@Mapper
public interface UserMapper {
void saveUser(User user);
User findUser(@Param("name")String name,@Param("pwd")String pwd);
}
UserMapepr.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cn.dao.UserMapper">
<!-- saveUser()方法实现 -->
<insert id="saveUser" parameterType="com.cn.entiy.User">
insert into test (name,pwd) values (#{name},#{pwd})
</insert>
<!-- findUser()方法实现 -->
<select id="findUser" resultType="com.cn.entiy.User">
select * from test where name=#{name} and pwd=#{pwd}
</select>
</mapper>
User
package com.cn.entiy;
public class User {
private String name;
private String pwd;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "User [name=" + name + ", pwd=" + pwd + "]";
}
}
UserService
package com.cn.service;
import org.springframework.stereotype.Service;
import com.cn.entiy.User;
public interface UserService {
void saveUser(User user);
User findUser(String name, String pwd);
}
UserServiceImpl
package com.cn.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.cn.dao.UserMapper;
import com.cn.entiy.User;
@Service("userService")
@Transactional
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapepr;
@Override
public void saveUser(User user) {
// TODO Auto-generated method stub
userMapepr.saveUser(user);
}
@Override
public User findUser(String name, String pwd) {
// TODO Auto-generated method stub
return userMapepr.findUser(name, pwd);
}
}
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/user/login.action"
method="post">
用户名:<input type="text" name="name"> 密码:<input type="password"
name="pwd"> <input type="submit" value="提交">
</form>
</body>
</html>
register.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册</title>
</head>
<body>
<form
action="${pageContext.request.contextPath}/user/register.action"
method="post">
用户名:<input type="text" name="name">
密码:<input type="text" name="pwd">
<input type="submit" value="提交">
</form>
</body>
</html>
succ.jsp和fail.jsp随便写点测试文字就行了,我就不沾上来了
将项目部署到tomcat上发布,然后在浏览器中输入以下地址,会有以下注册界面(密码框的格式为text,所以是明文)出现,点击提交会返回succ.jsp
在浏览器中输入以下地址,会有如下登录界面,点击提交如果匹配数据库返回succ.jsp,未匹配则返回fail.jsp
最后贴一个为什么要在spring中扫描service,在springmvc中扫描controller的贴
2019年11月22日更新