好久沒寫博客了,最近接到一個組內的測試開發任務是作一個使用SpringBoot 開發一個後臺程序(還未完成),特寫感想記錄一下前端
首先是目前不少公司的後臺仍是JAVA編寫的,或者直接就是MVC的一個後臺,大部分測試其實會採用python 做爲一個測試語言,易上手而後見效快。java
可是咱們能夠想見,其實在傳統行業,領導更但願你可以使用同一種語言來進行一些程序或者代碼的編寫。python
PS: 實際上是由於我本身報的,我說了python或者springboot均可以,領導給我選了springboot mysql
通常默認的是: springboot+mybatis+(MVC三件套: mapper,entity,service,controller) 等等,你須要瞭解基本的java語法,以及一些sql語法spring
最重要的,你須要一個前端童鞋配合你,調用你的後臺接口(全棧大神能夠忽略)sql
3.1 首先是我以前基本就不多接觸過springboot, 固然對JAVA還算熟悉,開發過appium爲框架下的自動化測試程序。那麼你須要一個自學springboot的過程數據庫
3.2 其次是業務邏輯的學習,我須要首先把Jira 的數據拉取下來,那麼須要瞭解Jira自己提供的那些api怎麼使用後端
3.3 如何與前端聯調,增強本身的後臺開發能力 api
進入正題:springboot
---------------------------------------------------------------------------------------------------------------------------------------------------------
說下我目前的開發順序和進度(你們能夠規避個人誤區,直接選擇新建一個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>