springboot增删改查实现注册登录,(实属无聊,写的比较繁琐)

先从Controller层来,用的阿里的json

package com.lty.springboot.demo.controller;

@RestController
public class TestController {
    @Resource
    private UserService userService;
    //实现登录
    @RequestMapping(value = "/dl",method = RequestMethod.POST)
    @ResponseBody
    public JSONObject dl(String userName,String userPassWord)throws Exception{
        JSONObject jsonObject=new JSONObject();
        if (userName==null||userPassWord==null){
            return jsonObject;
        }
        List<User> userList=userService.denglu(userName,userPassWord);
        if (userList.size()==0){
            jsonObject.put("code",0);
            jsonObject.put("data",null);
            jsonObject.put("message","没有此用户");
            return jsonObject;
        }
        jsonObject.put("code",1);
        jsonObject.put("data",userList);
        jsonObject.put("message","登陆成功");
        return jsonObject;
    }
    
    //实现注册
    @RequestMapping(value = "/zhuce",method = RequestMethod.POST)
    @ResponseBody
    public JSONObject zhuce(String userName,String userPassWord)throws Exception{
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("code",0);
        jsonObject.put("data",null);
        jsonObject.put("message","注册失败");
        if (userName==null||userPassWord==null){
            return jsonObject;
        }
        User user=new User();
        user.setUserName(userName);
        user.setUserPassWord(userPassWord);
        List<User> namelist=userService.getAll(userName);
        if (namelist.size()!=0){
                jsonObject.put("code",0);
                jsonObject.put("data",namelist.size());
                jsonObject.put("message","已有此用户名,请更换");
                return jsonObject;
            }
        int result=userService.add(user);
        if(result==0){
            return jsonObject;
        }
        jsonObject.put("code",1);
        jsonObject.put("data",result);
        jsonObject.put("message","注册成功");
        return jsonObject;

    }

	//实现修改
    @RequestMapping(value = "/xiugai",method = RequestMethod.POST)
    @ResponseBody
    public JSONObject xiugai(String userPassWord,String userName)throws Exception{
        JSONObject jsonObject=new JSONObject();
        if(userPassWord==null){
            jsonObject.put("code",0);
            jsonObject.put("data",null);
            jsonObject.put("message","密码不能为空");
            return jsonObject;
        }
        String password="";
        List<User> passwordlist=userService.getAll(userName);
        for (int i = 0; i <passwordlist.size() ; i++) {
            password=passwordlist.get(i).getUserPassWord();
        }
        if (password.equals(userPassWord)){
            jsonObject.put("code",0);
            jsonObject.put("data",password);
            jsonObject.put("message","两次密码相同,请更换");
            return jsonObject;
        }
        User user=new User();
        user.setUserPassWord(userPassWord);
        user.setUserName(userName);
        int result=userService.update(user);
        if (result==0){
            return jsonObject;
        }
        jsonObject.put("code",1);
        jsonObject.put("data",result);
        jsonObject.put("message","修改成功");
        return jsonObject;

    }

	//实现删除
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public JSONObject delete(Integer id)throws Exception{
        JSONObject jsonObject=new JSONObject();
        int result=userService.delete(id);
        if (result==0){
            jsonObject.put("code",0);
            jsonObject.put("data",null);
            jsonObject.put("message","删除失败");
            return jsonObject;
        }
        jsonObject.put("code",1);
        jsonObject.put("data",result);
        jsonObject.put("message","删除成功");
        return jsonObject;
    }
}

service层

public interface UserService {
    //登录
    public List<User> denglu(String userName,String userPassWord);
    //注册
    public int add(User user);
    //修改密码
    public int update(User user);
    //删除用户
    public int delete(Integer id);
    //根据名字查询数据库
    public List<User> getAll(String userName);
}

servcie实现类

package com.lty.springboot.demo.service.impl;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;
    @Override
    public List<User> denglu(String userName, String userPassWord) {
        return userDao.denglu(userName,userPassWord);
    }

    @Override
    public int add(User user) {
        return userDao.add(user);
    }

    @Override
    public int update(User user) {
        return userDao.update(user);
    }

    @Override
    public int delete(Integer id) {
        return userDao.delete(id);
    }

    @Override
    public List<User> getAll(String userName) {
        return userDao.getAll(userName);
    }
}

dao层(Mapper)

package com.lty.springboot.demo.dao;

@Mapper
public interface UserDao {
    //根据名字和密码查询数据库
    public List<User> denglu(@Param("userName") String userName, @Param("userPassWord") String userPassWord);
    //添加
    public int add(User user);
    //修改
    public int update(User user);
    //删除
    public int delete(Integer id);
    //根据名字查询数据库
    public List<User> getAll(String userName);

}

我把sql也放到dao层了

<?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.lty.springboot.demo.dao.UserDao">
		<select id="denglu" resultType="com.lty.springboot.demo.entity.User">
			select * from usertable
			where userName=#{userName} and userPassWord=#{userPassWord}
		</select>
		<insert id="add">
			insert into usertable (userName,userPassWord) values (#{userName},#{userPassWord})
		</insert>
		<update id="update">
			update usertable set userPassWord=#{userPassWord} where userName=#{userName}
		</update>
		<update id="delete" parameterType="java.lang.Integer">
			update usertable set status=0 where userId=#{id}
		</update>
	<select id="getAll" resultType="com.lty.springboot.demo.entity.User">
		select * from usertable
		where userName=#{userName}
		and status=1
	</select>
</mapper>

entity 实体类

package com.lty.springboot.demo.entity;

/**
 * @Description  
 * @Author  lty
 * @Date 2019-04-29 
 */

@Setter
@Getter
@ToString
@Table( name ="usertable" )
public class User implements Serializable {

	private static final long serialVersionUID =  7178841592885548152L;

   	@Column(name = "userId" )
	private Integer userId;

   	@Column(name = "userName" )
	private String userName;

   	@Column(name = "userPassWord" )
	private String userPassWord;

}

配置文件yml我这里用的是阿里的druid的数据库连接池:

server:
  port: 8080

spring:
  datasource:
    url: jdbc:mysql://*****不给看不给看略略略:3306/atm?useUnicode=true&characterEncoding=utf8&allowMultiQueries=true
    username: root
    password: 不给看不给看略略略
    driver-class-name: com.mysql.cj.jdbc.Driver
    initialSize: 5
    minIdle: 10
    maxActive: 20
    removeAbandoned: true
    removeAbandonedTimeout: 10000
    timeBetweenEvictionRunsMillis: 36000
    minEvictableIdleTimeMillis: 180000
    validationQuery: select 1 from dual
    testOnBorrow: true
    testWhileIdle: true
mybatis:
  typeAliasesPackage: com.lty.springboot.demo.dao
  mapperLocations: classpath:com.lty.springboot.demo.dao/*.xml

最后是pom文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.lty.springboot</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.1</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>5.1.25</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.15</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.1.26</version>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>4.4.0</version>
        </dependency>
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>javax.persistence-api</artifactId>
            <version>2.2</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>
</project>

我曾七次鄙视自己的灵魂:
第一次,当它本可进取时,却故作谦卑;
第二次,当它空虚时,用爱欲来填充;
第三次,在困难和容易之间,它选择了容易;
第四次,它犯了错,却借由别人也会犯错来宽慰自己;
第五次,它自由软弱,却把它认为是生命的坚韧;
第六次,当它鄙夷一张丑恶的嘴脸时,却不知那正是自己面具中的一副;
第七次,它侧身于生活的污泥中虽不甘心,却又畏首畏尾。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值