【SpingBoot】 测试如何使用SpringBoot搭建一个简单后台1

很久没写博客了,最近接到一个组内的测试开发任务是做一个使用SpringBoot 开发一个后台程序(还未完成),特写感想记录一下

1. 为什么选择SpringBoot ?

  首先是目前很多公司的后台还是JAVA编写的,或者直接就是MVC的一个后台,大部分测试其实会采用python 作为一个测试语言,易上手然后见效快。

  但是我们可以想见,其实在传统行业,领导更希望你能够使用同一种语言来进行一些程序或者代码的编写。

  PS: 其实是因为我自己报的,我说了python或者springboot都可以,领导给我选了springboot 

2. 使用springboot 你需要什么样的技术栈? 你需要了解什么才能启动起来?

  一般默认的是: springboot+mybatis+(MVC三件套: mapper,entity,service,controller) 等等,你需要了解基本的java语法,以及一些sql语法

  最重要的,你需要一个前端童鞋配合你,调用你的后台接口(全栈大神可以忽略)

3. 作为一个测试,你可能遇到什么问题?或者说我已经遇到了什么问题?

  3.1 首先是我之前基本就很少接触过springboot, 当然对JAVA还算熟悉,开发过appium为框架下的自动化测试程序。那么你需要一个自学springboot的过程

  3.2 其次是业务逻辑的学习,我需要首先把Jira 的数据拉取下来,那么需要了解Jira本身提供的那些api怎么使用

  3.3 如何与前端联调,加强自己的后台开发能力  

进入正题:

---------------------------------------------------------------------------------------------------------------------------------------------------------

说下我目前的开发顺序和进度(大家可以规避我的误区,直接选择新建一个SpringBoot 项目去开启):

了解需求---翻看JIRA的API---参考了博主“天外飞云”的JIRA程序---启动一个JAVA程序去调用JIRA的API---调通了之后思考如何引入SpringBoot(翻车,重新来)

---另外启动一个全新的SpringBoot程序---开始参考别人的SpringBoot入门博客---编写SpringBoot后台---调试自己完成的接口---联系前端调用----满足前端要求新增接口

分享一:

application.yml的写法 (单数据库源)

server:
  port: 8080

spring:
    datasource:
        name: XXXX
        url: jdbc:mysql://127.0.0.1:3306/XXXX?useUnicode=true&characterEncoding=utf8&autoReconnect=true&serverTimezone=GMT%2B8
        username: root
        password: 123456
        driver-class-name: com.mysql.jdbc.Driver
    jpa:
      database-platform: org.hibernate.dialect.MySQLDialect
mybatis:
  mapper-locations: classpath:mapper/*.xml  #注意:一定要对应mapper映射xml文件的所在路径
  type-aliases-package: com.ctyun.springboot.entity  # 注意:对应实体类的路径

分享二:

如果需要操作数据库,可以使用generatorConfig.xml去获取 ; 或者是自己先写好实体类entity ,然后自动生成数据库表。

这里我使用的是第一种,先在数据库建表,然后使用generatorConfig 提取到SpringBoot程序中

顺序:  1. 起SpringBoot  程序   2. 启动下面的generator

<?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="C:\xxxx\mysql\mysql-connector-java\5.1.45\mysql-connector-java-5.1.45.jar"/>
    <context id="DB2Tables"  targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="false"/>
        </commentGenerator>
        <!--数据库连接驱动类,URL,用户名、密码 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/xxxx?characterEncoding=utf8" userId="root" password="123456">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!-- 生成(实体)模型的包名和位置-->
        <javaModelGenerator targetPackage="com.ctyun.springboot.entity" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!-- 生成XML映射文件的包名和位置-->
        <sqlMapGenerator targetPackage="resources.mapper" targetProject="src/main">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!-- 生成DAO接口的包名和位置-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.ctyun.springboot.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
        <!--<table tableName="xxx" domainObjectName="xxx" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->
        <!--<table tableName="xxx" domainObjectName="menus" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->
        <table tableName="xxx" domainObjectName="Menusconfig" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
    </context>
</generatorConfiguration>

分享三:

后台可能有两种情况:

1. 不需要数据库,直接提供service+entity+controller 调用

2. 需要连接数据库,那么后台需要编写: mapper.xml --- mapper --- entity ---service ---controller 这么几层  【需要注意的是entity 相当于VO层, mapper 相当于DAO层,不用重复】

下面放一下第二种,mapper.xml的写法

下面是一个简单的用户表, 在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.ctyun.springboot.mapper.UserMapper">
    <resultMap id="BaseResultMap" type="com.ctyun.springboot.entity.User">
        <id column="id" property="id" jdbcType="INTEGER"/>
        <result column="username" property="username" jdbcType="VARCHAR"/>
        <result column="password" property="password" jdbcType="VARCHAR"/>
        <result column="nickname" property="nickname" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="Base_Column_List">
        id,
        username,
        password,
        nickname
    </sql>
    <!--用户登录验证-->
    <select id="userlogin" parameterType="User" resultType="com.ctyun.springboot.entity.User">
        SELECT
        <include refid="Base_Column_List"/>
        FROM user WHERE username = #{username} AND password = #{password}
    </select>

    <!--新用户注册  方式1-->
    <insert id="adduser" parameterType="user" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO user (username,password,nickname) VALUES (#{username},#{password},#{nickname})
    </insert>
    <!--新用户注册  方式2-->
    <insert id="adduser1" parameterType="user">
        <selectKey keyProperty="id" resultType="int">
            select LAST_INSERT_ID()
        </selectKey>
        INSERT INTO user (username,password,nickname) VALUES (#{username},#{password},#{nickname})
    </insert>
    <!--查询所有用户-->
    <select id="queryAllUser" resultType="map">
        SELECT
        <include refid="Base_Column_List"/>
        FROM user
    </select>

    <!--查询数据库里面的username-->
    <select id="queryUserName" resultType="java.lang.String">
        SELECT
        username
        FROM user
    </select>
</mapper>

猜你喜欢

转载自www.cnblogs.com/Ronaldo-HD/p/11395901.html