后端
1:entuty
package com.woniu.community.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private Integer id;
private String userName;
private String password;
private int type;
private String remarks;
}
2:userMapper
package com.woniu.community.mapper;
import com.woniu.community.entity.User;
import java.util.List;
public interface UserMapper {
User selectByNameAndPwd(String userName, String password);
List<User> selectAll(int start,int size);
int selectCount();
int insertUser(User user);
int updateUser(User user);
int deleteUser(int id);
User selectById(int id);
}
3:IUserService
package com.woniu.community.service;
import com.woniu.community.entity.HttpResult;
import com.woniu.community.entity.User;
import java.util.List;
public interface IUserService {
/**
* 用户登录
* @param userName
* @param password
* @return
*/
HttpResult selectByNameAndPwd(String userName, String password);
HttpResult selectAll(int pageIndex, int pageSize);
/**
* 新增
* @param user
* @return
*/
HttpResult addUser(User user);
/**
* 删除
* @param id
* @return
*/
HttpResult removeUser(int id);
/**
* 修改
* @param id
* @return
*/
HttpResult updateUser(User user);
/**
* 根据id查询
* @param id
* @return
*/
HttpResult getUserInfo(int id);
}
4:UserServiceImpl
package com.woniu.community.service.impl;
import com.woniu.community.entity.HttpResult;
import com.woniu.community.entity.User;
import com.woniu.community.mapper.UserMapper;
import com.woniu.community.service.IUserService;
import org.apache.catalina.filters.ExpiresFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements IUserService {
@Autowired(required = false)
private UserMapper userMapper;
/**
* 用户登录
*
* @param userName
* @param password
* @return
*/
@Override
public HttpResult selectByNameAndPwd(String userName, String password) {
HttpResult result=null;
User user = userMapper.selectByNameAndPwd(userName, password);
if (user!=null){
result=new HttpResult(user,0,200,"登录成功");
}else{
result=new HttpResult(null,0,200,"登录失败");
}
return result;
}
@Override
public HttpResult selectAll(int pageIndex, int pageSize) {
HttpResult result=null;
List<User> users = userMapper.selectAll((pageIndex - 1) * pageSize, pageSize);
int count = userMapper.selectCount();
if (users!=null&&users.size()>0){
result=new HttpResult(users,count,200,null);
}else{
result=new HttpResult(null,0,200,"没有更多数据");
}
return result;
}
/**
* 新增
*
* @param user
* @return
*/
@Override
public HttpResult addUser(User user) {
HttpResult result=null;
int count = userMapper.insertUser(user);
if (count>0){
result=new HttpResult(null,count,200,"添加成功");
}else{
result=new HttpResult(null,count,500,"添加失败");
}
return result;
}
/**
* 删除
*
* @param id
* @return
*/
@Override
public HttpResult removeUser(int id) {
HttpResult result=null;
int count = userMapper.deleteUser(id);
if (count>0){
result=new HttpResult(null,count,200,"删除成功");
}else{
result=new HttpResult(null,count,500,"删除失败");
}
return result;
}
/**
* 修改
*
* @param user
* @return
*/
@Override
public HttpResult updateUser(User user) {
HttpResult result=null;
int count = userMapper.updateUser(user);
if (count>0){
result=new HttpResult(null,count,200,"修改成功");
}else{
result=new HttpResult(null,count,500,"修改失败");
}
return result;
}
/**
* 根据id查询
*
* @param id
* @return
*/
@Override
public HttpResult getUserInfo(int id) {
HttpResult result=null;
User user = userMapper.selectById(id);
if (user!=null){
result=new HttpResult(user,0,200,null);
}else{
result=new HttpResult(null,0,500,"没有更多数据");
}
return result;
}
}
5:UserController
package com.woniu.community.controller;
import com.woniu.community.entity.HttpResult;
import com.woniu.community.entity.User;
import com.woniu.community.entity.vo.UserVO;
import com.woniu.community.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/user")
@CrossOrigin(origins = "*")
public class UserController {
@Autowired
private IUserService iUserService;
@RequestMapping("/login")
public HttpResult selectByNameAndPwd(@RequestBody UserVO vo) {
return iUserService.selectByNameAndPwd(vo.getUserName(),vo.getPassword());
}
@RequestMapping("list")
public HttpResult selectAll(int pageIndex, int pageSize) {
return iUserService.selectAll(pageIndex, pageSize);
}
@PostMapping("/add")
HttpResult addUser(@RequestBody UserVO vo){
User user = new User();
user.setUserName(vo.getUserName());
user.setPassword(vo.getPassword());
return iUserService.addUser(user);
}
@RequestMapping("/remove")
HttpResult removeUser(int id){
return iUserService.removeUser(id);
}
@PostMapping("/update")
HttpResult updateUser(@RequestBody User user){
return iUserService.updateUser(user);
}
@RequestMapping("/info")
HttpResult getUserInfo(int id){
return iUserService.getUserInfo(id);
}
}
6:userMapper.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.woniu.community.mapper.UserMapper">
<resultMap id="userResult" type="User">
<result column="id" property="id"/>
<result column="username" property="userName"/>
<result column="password" property="password"/>
<result column="type" property="type"/>
<result column="remarks" property="remarks"/>
</resultMap>
<select id="selectByNameAndPwd" resultMap="userResult">
select* from userinfo where username=#{userName}and password=#{password}
</select>
<select id="selectAll" resultMap="userResult">
select* from userinfo limit #{start},#{size}
</select>
<select id="selectCount" resultType="int">
select count(id) from userinfo
</select>
<insert id="insertUser" >
insert into userinfo(username,password) values (#{userName},#{password})
</insert>
<update id="updateUser">
update userinfo set username=#{userName},password=#{password} where id=#{id}
</update>
<delete id="deleteUser">
delete from userinfo where id=#{id}
</delete>
<select id="selectById" resultMap="userResult">
select * from userinfo where id=#{id}
</select>
</mapper>
二前端
<!DOCTYPE html>
<html lang="en">
<head>
<link href="assets/bootstrap-3.3.7-dist/css/bootstrap.min.css" rel="stylesheet">
<script src="assets/jquery-3.5.1.min.js"></script>
<script src="assets/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
<script src="assets/vue.min-v2.5.16.js"></script>
<script src="assets/axios.min.js"></script>
<meta charset="UTF-8">
<title>login</title>
</head>
<body>
<div class="container" id="app">
<div class="row">
<div class="col-md-6 col-md-offset-3" style="border: 1px solid black;height: 310px;margin-top: 80px">
<div class="row">
<div class="col-md-5" style="height: 310px;background-color: rgb(43,136,233);font-size: 18px;color: #b9def0;font-weight: bold;text-align: center;line-height: 310px">
智慧社区管理系统
</div>
<div class="col-md-7">
<div class="row" style="text-align: center;margin-top: 30px">
<h4>登录</h4>
</div>
<div class = "row" style="margin-top: 20px;height: 60px">
<div class="col-md-8 col-md-offset-2">
<label>用户名:</label>
<input type="text" class="form-control" style="border-radius: 5px" v-model="userName"/>
</div>
</div>
<div class = "row" style="margin-top: 10px;height: 60px" />
<div class="col-md-8 col-md-offset-2">
<label>密码:</label>
<input type="password" class="form-control" style="border-radius: 5px" v-model="password"/>
</div>
</div>
<div class = "row" style="margin-top: 10px;height: 60px ;text-align: center">
<div class="col-md-8 col-md-offset-2">
<button class="btn btn-primary" style="margin-right: 8px" @click = "doLogin">登录</button>
<button class="btn btn-default" @click = "doReset"/>重置</button>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
new Vue({
el : '#app',
data : {
userName : null,
password : null,
},
methods : {
doLogin(){
//var url="http://localhost:8080/user/login?userName="+this.userName+"&password="+this.password;
var url="http://localhost:8080/user/login"
axios.post(url,{
userName:this.userName,
password:this.password
}).then(response=>{
if (response.data.code==200){
window.location.href="main.html";
}else{
alert(response.data.msg)
}
})
},
doReset(){
this.userName = null;
this.password = null;
}
},
created : function(){
}
})
</script>
</body>
</html>