配置文件
Spring文件配置
<?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">
<!--加载外部的properties文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--Spring组件扫描-->
<context:component-scan base-package="com.group3">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--配置数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<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>
<!--配置mybatis 到 Spring容器中 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:sqlMapconfig.xml"></property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.group3.mapper"></property>
</bean>
</beans>
Spring Mvc 文件配置
<?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:mvc="http://www.springframework.org/schema/mvc"
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/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.group3">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--配置内部资源视图解析器-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- /jsp/success.jsp -->
<property name="prefix" value="/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--mvc的注解驱动-->
<mvc:annotation-driven conversion-service="conversionService"/>
<!--开发资源的访问
<mvc:resources mapping="/js/**" location="/js/"/>-->
<mvc:default-servlet-handler/>
<!-- 设置mvc全局编码-->
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!--声明转换器-->
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
</list>
</property>
</bean>
<!--开发静态资源访问权限-->
<mvc:default-servlet-handler></mvc:default-servlet-handler>
</beans>
mybatis文件配置
<?xml version="1.0" encoding="UTF-8" ?><!--自定义别名-->
<typeAliases>
<package name="com.group3.domain"></package>
</typeAliases>
这里只需要定义别名就行,其他的交给Spring来处理
Pom配置文件
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<!--servlet和jsp-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
</dependency>
<!--mybatis相关-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--json部分 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.3</version>
</dependency>
配置文件都已经完成,后面是后端代码
首先是Dao层,使用mybatis的时候是使用map映射
public interface UserMapper {
@Select("select * from user where username=#{username} and password=#{password}")
//验证密码账户
public User get_user(User user);
@Select("select * from user")
//查找全部用户信息
public List<User> findAll();
}
Pojo类构造
public class User {
int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
pojo用户数据库和客户端数据交互作为容器存放数据
Service层
Service接口
public interface UserService {
public boolean get_id(User user);//用于密码和账户验证
public List<User> findAll();//查找所有用户信息
public User findByid(int id);//通过id查找用户信息
}
Service的实现类
@Service("userservice")
public class UserServiceimpl implements UserService {
@Autowired
private UserMapper userMapper;//导入mapper对象
@Override
public boolean get_id(User user) {
if (userMapper.get_user(user)!=null) //通过mapper方法查找账号密码
{
return true;
}
else
{
return false;
}
}
@Override
public List<User> findAll() {
return userMapper.findAll();
}
@Override
public User findByid(int id) {
return null;
}
}
Controller类
@Controller
@RequestMapping("/user")
public class userconctroller {
@Autowired
UserService userService;
@RequestMapping(value = "/test")
@ResponseBody
public String test(@RequestBody List<User> userList)
{
User temp=new User();
for(User user:userList)
{
temp.setPassword(user.getPassword());
temp.setUsername(user.getUsername());
}
if(userService.get_id(temp))
{
return "success";
}
return null;
}
}
登录Jsp文件
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<script src="${pageContext.request.contextPath}/js/jquery-3.3.1.js"></script>
<script language="javascript">
//通过ajax获取text中的值以json格式化传输过去
$(function () {
$("#test").click(function () {
var userList=new Array();
userList.push({username:$("#username").val(),password:$("#password").val()});
$.ajax({
type:"POST",
url:"${pageContext.request.contextPath}/user/test",
data:JSON.stringify(userList),
contentType:"application/json;charset=utf-8",
success:function (result) {
if(result=="success")
{
window.location.href="${pageContext.request.contextPath}/main.jsp"
}
else
{
window.location.href="${pageContext.request.contextPath}/login.jsp"
}
}
});
});
});
</script>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/login.css">
<title>Document</title>
</head>
<body>
<div class="container">
<div class="login-form">
<div class="text">登录</div>
<div class="form-item">
<input type="text" placeholder="用户名" name="username" id="username">
<input type="password" placeholder="密码" name="password" id="password">
</div>
<div >
<button class="btn" id="test">登录</button>
</div>
<div class="btn-back"></div>
<div class="other">
没有账号?<a href="#">去注册</a>
</div>
</div>
</div>
</body>
</html>