EggJS 雲原生應用硬核實戰(Kubernetes+Traefik+Helm+Prometheus+Grafana),提供 Demo
阿新 • • 發佈:2020-12-02
1.先匯入相關依賴:
<!-- mysql:MyBatis相關依賴 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.4.0</version> <type>maven-plugin</type> </dependency> <!-- mysql:mysql驅動--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- mysql:阿里巴巴資料庫連線池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.12</version> </dependency>
2.application.yml 配置:
spring: application: name: service-order redis: database: 0 host: 127.0.0.1 port: 6379 timeout: 2000 password: #資料庫連線配置 datasource: #配置當前使用的資料來源的操作型別 type: com.alibaba.druid.pool.DruidDataSource #配置MySQL的驅動程式類 driver-class-name: com.mysql.cj.jdbc.Driver #資料庫連線地址 url: jdbc:mysql://localhost:3306/ac-new?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai #資料庫連線使用者名稱 username: root #資料庫連線密碼 password: root #進行資料庫連線池的配置 dbcp2: #初始化提供的連線數 initial-size: 5 #資料庫連線池的最小維持連線數 min-idle: 5 #最大的連線數 max-total: 5 #等待連接獲取的最大超時時間 max-wait-millis: 200 validation-query: SELECT 1 test-while-idle: true test-on-borrow: false test-on-return: false #自定義哨兵 sentinel: address: - 127.0.0.1:26379 - 127.0.0.1:26479 - 127.0.0.1:26579 masterName: mymaster #註冊中心 eureka: client: enabled: true #設定服務註冊中心的URL service-url: defaultZone: http://localhost:7900/eureka/ #mybatis配置 mybatis: mapper-locations: - classpath:mapper/*.xml
3.編寫模板 generatorConfigMySql.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> <context id="MySqlContext" targetRuntime="MyBatis3Simple" defaultModelType="flat"> <property name="beginingDelimiter" value="`"/> <property name="endingDelimiter" value="`"/> <commentGenerator> <property name="supressDate" value="true"/> <property name="addRemarkComments" value="true"/> </commentGenerator> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/ac-new?serverTimezone=UTC&useUnicode=true&zeroDateTimeBehavior=convertToNull&autoReconnect=true&characterEncoding=utf8" userId="root" password="root" > </jdbcConnection> <javaModelGenerator targetPackage="com....." targetProject="service\src\main\java\"> <property name="trimStrings" value="true"/> </javaModelGenerator> <sqlMapGenerator targetPackage="mapper" targetProject="service\src\main\resources/"/> <javaClientGenerator type="XMLMAPPER" targetPackage="com...." targetProject="service\src\main\java\"/> <table tableName="SAVE" domainObjectName="SaveInfo"/> </context> </generatorConfiguration>
需要根據自身更改(引數說明一一對應): connectionURL: 資料庫地址,埠號,資料庫名稱(localhost:3306/ac-new) userId: 資料庫使用者名稱 (root) password: 資料庫密碼 (root) targetPackage: 生成檔案存放路徑從java下的com資料夾開始(com.....) targetProject:從工程名到java目錄,service是工程名(service\src\main\java\) targetProject:生成的XML存放路徑,service是工程名(service\src\main\resources/) tableName: 資料庫表名(SAVE) domainObjectName: 生成檔名(SaveInfo)
4.java 執行main函式生成檔案:
package com....; import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.config.Configuration; import org.mybatis.generator.config.xml.ConfigurationParser; import org.mybatis.generator.internal.DefaultShellCallback; import java.io.InputStream; import java.util.ArrayList; import java.util.List; public class GeneratorToSqlFile { public static void main(String[] args) throws Exception{ List<String> warnings = new ArrayList<>(); boolean overwrite = true; InputStream is = GeneratorToSqlFile.class.getResourceAsStream("/generatorConfigMySql.xml"); if(is == null){ System.out.println("null"); } ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(is); is.close(); DefaultShellCallback callback = new DefaultShellCallback(overwrite); MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); myBatisGenerator.generate(null); for(String warning : warnings){ System.out.println(warning); } } }
執行改方法共生成:
1.帶有get,set方法的 SaveInfo.java實體類 ,
2.帶有@Mapper註解的 SaveInfoMapper.java介面類 ,
3.帶有簡單Sql語句的 SaveInfoMapper.xml配置類 三個檔案
(生成的xml,最好檢查一下每個節點對應的路徑
5.測試:
對資料庫做簡單的查詢和寫入
package com....controller; import com.....SaveInfoMapper; import com.....SaveInfo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.sql.Timestamp; import java.util.Date; @Controller public class GetInfoByIdController { @Autowired private SaveInfoMapper saveInfoMapper; @GetMapping("/selSql") public void selSql(HttpServletRequest request,HttpServletResponse response) throws IOException { SaveInfo info = saveInfoMapper.selectInfoByid("001"); String id = info.getId(); String applyno = info.getApplyno(); String address = info.getAddress(); System.out.println(id); System.out.println(applyno); System.out.println(address); response.getWriter().print("$$$$$$"); } @GetMapping("/InSql") public void InSql(HttpServletRequest request,HttpServletResponse response) throws IOException { Timestamp saveTime=new Timestamp(new Date().getTime()); SaveInfo info = new SaveInfo(); info.setId("002"); info.setApplyno("004098020000002"); info.setUserName("測試2"); info.setUserNumber("002"); info.setAddress("靜安區"); info.setSaveTime(saveTime); info.setType("Yes"); info.setCount("count"); int mu = saveInfoMapper.insert(info); response.getWriter().print(mu+""); } }
簡單查詢 :
1.SaveInfoMapper里加個方法:SaveInfo selectInfoByid(String id);
2.SaveInfoMapper.xml里加個select節點:
<select id="selectInfoByid" parameterType="java.lang.String" resultMap="BaseResultMap">
select ID, APPLYNO, USER_NAME, USER_NUMBER, ADDRESS, SAVE_TIME, TYPE, COUNT, SAVE_BOLB,
SAVE_CLOB
from save
where id = #{id,jdbcType=VARCHAR}
</select>
結果:
控制檯
頁面
資料庫