写在前面:
文章满满干货,每一步都是自己从零开始操作并验证的,写出来为了记录一下其中遇到的各种坑。可能有的坑比较弱智,还请大佬们见谅哈,需要工程源码的私信我就好。
看完这篇文章,相信你完全可以自己搭建出来一套在国产环境运行的JAVA程序。
直接看部署流程请点击这里:
纯国产环境(银河麒麟 + 飞腾)JAVA程序(Springboot + Mybatis + 达梦数据库)部署
这里之前找到一个大佬写的文章,非常详细,兄弟们按照这个安装创建数据库就OK
达梦8安装(WINDOWS环境)
我的数据库中表结构为:
故下面Dao生成,页面数据显示,均按照该表结构进行生成
IDE之类的就不说了哦,我用的是Idea,咱们直接从Springboot搭建开始:
先下载达梦8的jar包,用于程序访问达梦数据库,这里点击下面链接就可以
DmJdbcDriver18-8.1.1.193
提取码:7duw
下载后进入jar包下载的目录,打开CMD,运行下面命令将该jar包引入到本地的MAVEN库:
mvn install:install-file -Dfile=DmJdbcDriver18-8.1.1.193.jar -DgroupId=dm -DartifactId=dm.jdbc.driver -Dversion=8.1.1 -Dpackaging=jar
pom文件如下:(因为自己已经做完一遍流程,故这里附上完整的pom文件)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>2.0.1</version>
<name>demo</name>
<packaging>jar</packaging>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 达梦数据库 -->
<dependency>
<groupId>dm</groupId>
<artifactId>dm.jdbc.driver</artifactId>
<version>8.1.1</version>
</dependency>
<!-- druid数据源驱动 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<!--for build jar-->
<mainClass>com.example.DemoApplication</mainClass>
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- 自动生成mapper -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<dependencies>
<!–for mysql–>
<!–<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>–>
<!–for oracle–>
<dependency>
<groupId>com.dm</groupId>
<artifactId>DmJdbcDriver</artifactId>
<version>1.8.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/DmJdbcDriver18-8.1.1.193.jar</systemPath>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>3.4.0</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<phase>package</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
<configurationFile>src/main/resources/config/mybatis-generator.xml</configurationFile>
</configuration>
</plugin>
</plugins>
<!--打包-->
<!--<resources>
<resource>
<directory>lib</directory>
<targetPath>BOOT-INF/lib/</targetPath>
<includes>
<include>**/*.jar
src/main/resources
**/ *.yml</include>
<include>**/*.xml
**/ *.properties</include>
</includes>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml
**/ *.properties</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<targetPath>BOOT-INF/classes/</targetPath>
</resource>
</resources>
<finalName>di</finalName>-->
</build>
</project>
application.properties :
#-----------------dataSource start-----------------
#spring.datasource.type=dm.jdbc.pool.DmdbDataSource
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.druid.url=jdbc:dm://localhost:5236/DI
spring.datasource.druid.username=SYSDBA
spring.datasource.druid.password=WDwd123456
#这里必须加下面语句哦,对应mapper的xml文件路径,不加的话项目启动时springboot扫不到
mybatis.mapper-locations=classpath:mapper/**.xml
spring.datasource.druid.test-on-borrow=false
spring.datasource.druid.test-while-idle=true
spring.datasource.druid.test-on-return=false
spring.datasource.druid.validation-query=SELECT 1
#-----------------dataSource end-----------------
UserController.java
package com.example.controller;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/testUser")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("getUser")
public String demo() {
String a = userService.userDemo().toString();
return a;
}
}
DemoUser.java
package com.example.dao;
import javax.persistence.Column;
public class DemoUser {
@Column(name = "ID")
private String id;
@Column(name = "NAME")
private String name;
@Column(name = "AGE")
private String age;
/**
* @return ID
*/
public String getId() {
return id;
}
/**
* @param id
*/
public void setId(String id) {
this.id = id == null ? null : id.trim();
}
/**
* @return NAME
*/
public String getName() {
return name;
}
/**
* @param name
*/
public void setName(String name) {
this.name = name == null ? null : name.trim();
}
/**
* @return AGE
*/
public String getAge() {
return age;
}
/**
* @param age
*/
public void setAge(String age) {
this.age = age == null ? null : age.trim();
}
public String toString() {
return "NAME = " + name + " AGE = " + age;
}
}
DemoUserMapper.java
package com.example.mapper;
import com.example.dao.DemoUser;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface DemoUserMapper {
DemoUser queryInfo();
}
UserService.java
package com.example.service;
import com.example.dao.DemoUser;
import com.example.mapper.DemoUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
private DemoUserMapper userMapper;
public DemoUser userDemo() {
DemoUser user = userMapper.queryInfo();
return user;
}
}
DemoApplication.java
package com.example;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan({"com.**.dao", "com.**.mapper"})
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
拓展:mybatis-generator.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="mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<!--<plugin type="tk.mybatis.mapper.generator.MapperPlugin">
<!– 该配置会使生产的Mapper自动继承MyMapper –>
<property name="mappers" value="com.example.mapper"/>
<!– caseSensitive默认false,当数据库表名区分大小写时,可以将该属性设置为true –>
<property name="caseSensitive" value="false"/>
</plugin>-->
<!-- 阻止生成自动注释 -->
<commentGenerator>
<property name="javaFileEncoding" value="UTF-8"/>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!-- 数据库链接地址账号密码 for DMDB -->
<jdbcConnection
driverClass="dm.jdbc.driver.DmDriver"
connectionURL="jdbc:dm://localhost:5236/DI"
userId="SYSDBA"
password="WDwd123456">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 生成Model类存放位置 -->
<javaModelGenerator targetPackage="com.example.dao" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成映射文件存放位置 -->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 生成Dao类存放位置 -->
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件的代码
type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象
type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.example.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 配置需要生成的表 -->
<table tableName="DEMOUSER" domainObjectName="User" enableCountByExample="false"
enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
<!--<generatedKey column="MB_ID" sqlStatement="oralce" identity="true"/>-->
</table>
</context>
</generatorConfiguration>
将如上代码编写完毕后,该项目就可运行了
启动后打开浏览器输入url:
localhost:8080/testUser/getUser
页面显示类似如下,即项目搭建成功
至此,基于Springboot + Mybatis + 达梦数据库 的一个基础的java程序已搭建完成,下一节我将会把该项目部署至 银河麒麟+飞腾 操作系统中。
点击链接直达:
纯国产环境(银河麒麟 + 飞腾)JAVA程序(Springboot + Mybatis + 达梦数据库)部署
第一次写东西,有很多东西写的不全面,还望见谅。