1.java测试平台

技术栈主要是springboot + vue element ui(单module单体应用无权限认证),后期会演进到springcloud + vue element ui + oauth2 + redis + rbac权限模型(多module分布式应用加权限验证),甚至测试平台移动端的开发。该测试平台主要包括的功能有:性能、UI自动化、接口自动化。本篇文章讲述的是测试平台的后端搭建。前端的搭建会在下一篇文章里讲到。后端搭建的主要步骤如下:

1、springboot框架搭建

访问springboot官网,填好相关信息后,点击左下角generate,生成springboot项目文件,然后直接用idea直接打开该项目,构建好springboot框架就搭建好了。
image

2、在pom.xml文件里添加Mybatis的起步依赖

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>

3、在pom.xml文件里添加数据库驱动坐标

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

4、在application.properties中添加数据库连接信息

server.port=8081
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
spring.datasource.username=rootspring.datasource.password=123456

5、在mysql数据库创建user表

-- ----------------------------
-- Table structure for `user`
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) DEFAULT NULL,
`password` varchar(50) DEFAULT NULL,
`name` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1', 'zhangsan', '123', '张三');
INSERT INTO `user` VALUES ('2', 'lisi', '123', '李四');

6、创建实体Bean

package com.example.demo.bean;
public class User {
    //主键
    private Long id;
    //用户名
    private String userName;
    //密码
    private String password;
    //姓名
    private String name;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

7、编写Mapper

@Mapper
public interface UserMapper {
    public List<User> queryUserList();
}

注意:@Mapper标记该类是一个mybatis的mapper接口,可以被spring boot自动扫描到spring上下文中

8、配置Mapper映射文件

<?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.example.demo.mapper.UserMapper">
    <select id="queryUserList" resultType="com.example.demo.bean.User">
      select * from user
    </select>
</mapper>

9、在application.properties中添加mybatis的信息

mybatis.type-aliases-package=com.example.demo.bean
mybatis.mapper-locations=classpath:mapper/*Mapper.xml

10、编写测试Controller

package com.example.demo.controller;

import com.example.demo.bean.User;
import com.example.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @RequestMapping("/queryUser")
    @ResponseBody  
public List<User> queryUser(){
        List<User> userList = userMapper.queryUserList();
        return userList;
    }
}

11、测试

image

posted @ 2022-12-28 15:33  ll=ll  阅读(102)  评论(0编辑  收藏  举报