使用SpringBoot 尝试写个接口

2019-02-12  本文已影响0人  阿阿潇

第一步新建一个项目

image.png
image.png
image.png

第二步配置application.properties文件

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/frist?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=112358
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5

server.port=8000
#server.session.timeout=10
server.tomcat.uri-encoding=UTF-8


# mybatis.config= classpath:mybatis-config.xml
mybatis.mapperLocations=classpath:Mapper/*.xml
# domain object's package
mybatis.typeAliasesPackage=com.lgp.SpringBoot.bean
# handler's package
# mybatis.typeHandlersPackage=
# check the mybatis configuration exists
# mybatis.check-config-location=
# mode of execution. Default is SIMPLE
# mybatis.executorType=

第三步:创建一张表

image.png

第四步:配置Mybatis-Generator自动生成实体类及dao类

下载:https://github.com/mybatis/generator/releases/

generatorConfig.xml文件配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
    <!--数据库驱动-->
    <classPathEntry    location="D:/.m2\repository\mysql\mysql-connector-java\8.0.13\mysql-connector-java-8.0.13.jar"/>
    <context id="DB2Tables"    targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--数据库链接地址账号密码-->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost/frist?serverTimezone=GMT%2B8" userId="root" password="112358">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!--生成Model类存放位置-->
        <javaModelGenerator targetPackage="com.example.generator.Model" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!--生成映射文件存放位置-->
        <sqlMapGenerator targetPackage="com.example.generator.Mapper" targetProject="src\main\resources\Mapper">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!--生成Dao类存放位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.generator.Dao" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!--生成对应表及类名-->
        <table tableName="testuser" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
    </context>
</generatorConfiguration>

使用命令

java -jar mybatis-generator-core-1.3.7.jar -configfile generatorConfig.xml -overwrite

成功生成三个文件


image.png

第四步:创建Vo类

package com.example.generator.Vo;

public class UserVo {
    private String user;

    private String pwd;

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
}

第五步:创建control类

@RestController
public class LoginController {
    @Autowired
    private LoginService loginService;

   @PostMapping("/register")
    public HashMap register (UserVo userVo){
       return loginService.insertSelective(userVo);
   }
   @GetMapping("/selectUser")
    public HashMap selectUser (String name){
       return loginService.selectUser(name);
   }
}

第六步:创建service类并编写具体逻辑方法

public interface LoginService {
    public HashMap insertSelective(UserVo userVo);
    public HashMap selectUser(String name);
}
@Service
public class LoginServiceimpl implements LoginService {
    @Resource
    private UserMapper userMapper;

    @Override
    public HashMap insertSelective(UserVo userVo) {
        HashMap messageMap = new HashMap();
        HashMap bodyMap = new HashMap();
        if ("".equals(userVo.getPwd()) || "".equals(userVo.getUser()) || null == userVo.getUser() || null == userVo.getPwd()) {
            messageMap.put("status", "失败");
            bodyMap.put("result", "用户名或密码不能为空");
            messageMap.put("body", bodyMap);
        } else if (userMapper.selectByUser(userVo.getUser()).size() != 0) {
            messageMap.put("status", "失败");
            bodyMap.put("result", "换个用户名");
            messageMap.put("body", bodyMap);
        } else if (userVo.getPwd().length() < 6) {
            messageMap.put("status", "失败");
            bodyMap.put("result", "密码最小6位");
            messageMap.put("body", bodyMap);
        } else {
            try {
                User user = new User();
                user.setUser(userVo.getUser());
                user.setPwd(userVo.getPwd());
                userMapper.insertSelective(user);
                messageMap.put("status", "成功");
                bodyMap.put("result", userVo);
                messageMap.put("body", bodyMap);
            } catch (Exception e) {
                messageMap.put("status", "失败");
                bodyMap.put("result", "我也不晓得怎么回事");
                messageMap.put("body", bodyMap);
            }
        }

        return messageMap;
    }

    @Override
    public HashMap selectUser(String name) {
        HashMap messageMap = new HashMap();
        HashMap bodyMap = new HashMap();
        if (userMapper.selectByUser(name).size() != 0) {
            messageMap.put("status", "成功");
            bodyMap.put("result", "用户名已存在");
            messageMap.put("body", bodyMap);
        } else {
            messageMap.put("status", "成功");
            bodyMap.put("result", "用户名不存在");
            messageMap.put("body", bodyMap);
        }
        return messageMap;
    }
}

第七步:运行服务


image.png
image.png
image.png
上一篇下一篇

猜你喜欢

热点阅读