注册
springboot连接达梦数据库
技术分享/ 文章详情 /

springboot连接达梦数据库

DM_sms 2022/06/29 5242 0 2

一、下载并导入 springboot 工程

1.1 下载 springboot 工程

springboot 官网下载,springboot 提供了构建 springboot 项目的模板代码。如下图所示:
image.png

1.2 导入工程到 Eclipse

将下载好的项目导入到 eclipse 中。如下图所示。
(1)选择要导入的项目:
image.png
(2)导入项目:
image.png

二、SpringBoot 工程项目结构介绍

2.1 添加依赖:pom.xml

pom.xml 文件内容如下:

<?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>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.7.0</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.dameng</groupId>
	<artifactId>dmSpringboot</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>dmSpringboot</name>
	<description>Demo project for Spring Boot to DM</description>
	<properties>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>
 		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>
    <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.2.3</version>
    </dependency>
		<dependency>
            <groupId>com.dameng</groupId>
            <artifactId>DmJdbcDriver18</artifactId>
            <version>8.1.1.193</version>
        </dependency>
		
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

2.2 配置 application.properties

在 src/main/resources 路径下有一个 application.properties 的核心配置文件。主要用于指定自定义配置,具体内容如下:
(1)服务器的端口 server.port=8088;
(2)配置一个项目名(spirngboot 默认没有项目名)server.context_path=/dm;

#配置端口
server.port=8088
#配置项目名称
#server.context_path=/dm
server.servlet.contest-path=/demo
#自定义配置
com.name=dameng

(3)整合mybatis配置数据源等

#配置数据库连接
spring.application.name=dmspringboot

spring.datasource.driver-class-name=dm.jdbc.driver.DmDriver
spring.datasource.url=jdbc:dm://49.232.86.67:5236
spring.datasource.username=dmhr
spring.datasource.password=123456789
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

2.3 启动类配置

2.3.1 编写 JavaBean 封装表的字段

package com.dameng.dmSpringboot.pojo;

import java.sql.Date;
import java.util.Arrays;

/**
* @类名  PRODUCT_CATEGORY.java
* @作者  li2025
* @版本  V1.0
* @日期  2022年6月27日-下午9:43:19
* @描述  create table SimplyTest(id int,name varchar2(20),createtime timestamp(6),photo clob,txt Blob)
*  create table dmhr.SimplyTest(id int,name varchar2(20),createtime timestamp(6),photo Blob,txt Clob);

*/

public class SimplyTest {
	private int id;
    private String name;
    private Date createtime;
    private byte[] photo;
    private String txt;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Date getCreatetime() {
		return createtime;
	}
	public void setCreatetime(Date createtime) {
		this.createtime = createtime;
	}
	public byte[] getPhoto() {
		return photo;
	}
	public void setPhoto(byte[] photo) {
		this.photo = photo;
	}
	public String getTxt() {
		return txt;
	}
	public void setTxt(String txt) {
		this.txt = txt;
	}
	@Override
	public String toString() {
		return "SimplyTest [id=" + id + ", name=" + name + ", createtime=" + createtime + ", photo="
				+ Arrays.toString(photo) + ", txt=" + txt + "]";
	}
   
}

2.3.2 编写 Dao 层接口类

package com.dameng.dmSpringboot.dao;

import java.util.List;

import com.dameng.dmSpringboot.pojo.Employee;
import com.dameng.dmSpringboot.pojo.SimplyTest;

/**
* @类名  productCategoryDao.java
* @作者  li2025
* @版本  V1.0
* @日期  2022年6月27日-下午9:45:27
* @描述  
*
*/
public interface SimplyTestDao {
	//测试插入
	int testInstert(SimplyTest simplyTest);
	//测试修改
	int testUpdate(String name,Integer id);
	//测试根据id删除
	int testDelete(Integer id);
	//测试根据id查询
	SimplyTest testSelectById(Integer id);
	//测试全查
	List<SimplyTest> testSelectAll();

}

2.3.3 编写 Dao 层服务类

package com.dameng.dmSpringboot.dao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.dameng.dmSpringboot.pojo.SimplyTest;

/**
* @类名  productImpl.java
* @作者  li2025
* @版本  V1.0
* @日期  2022年6月27日-下午9:49:49
* @描述  productCategoryDao 实现
*
*/
@Repository
public class SimplyTestDaoImpl implements SimplyTestDao {
	 @Autowired //是用在JavaBean中的注解,通过byType形式,用来给指定的字段或方法注入所需的外部资源。
	 private JdbcTemplate jdbcTemplate; //jdbc连接工具类

	@Override
	public int testInstert(SimplyTest simplyTest) {
		  String sql = "insert into SimplyTest(id,name,createtime,photo,txt)values(?,?,?,?,?)";
	        Object[] params = {simplyTest.getId(),simplyTest.getName(),simplyTest.getCreatetime(),simplyTest.getPhoto(),simplyTest.getTxt()};
	        return jdbcTemplate.update(sql, params);

	}

	@Override
	public int testUpdate(String name, Integer id) {
		 String sql = "update SimplyTest set name=? where id=?";
	        Object[] params = {name,id};
	        return jdbcTemplate.update(sql,params);

	}

	@Override
	public int testDelete(Integer id) {
		 String sql = "delete from SimplyTest where id=?";
	        Object[] params = {id};
	        return jdbcTemplate.update(sql,params);

	}

	@Override
	public SimplyTest testSelectById(Integer id) {
		 String sql = "select * from SimplyTest where id=?";
	        Object[] params = new Object[]{id};
	        return jdbcTemplate.queryForObject( sql, params,new BeanPropertyRowMapper<SimplyTest>(SimplyTest.class));

	}

	@Override
	public List<SimplyTest> testSelectAll() {
		String sql = "select * from SimplyTest";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<SimplyTest>(SimplyTest.class));
	}

}

2.3.4 编写 Controller 类

package com.dameng.dmSpringboot.controller;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.sql.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.dameng.dmSpringboot.dao.EmployeeDaoJdbc;
import com.dameng.dmSpringboot.dao.SimplyTestDaoImpl;
import com.dameng.dmSpringboot.pojo.Employee;
import com.dameng.dmSpringboot.pojo.SimplyTest;

/**
* @类名  MyController.java
* @作者  li2025
* @版本  V1.0
* @日期  2022年6月14日-下午5:40:07
* @描述  
*
*/
@RestController
//@Controller
public class MyController {
//	@ResponseBody
	@RequestMapping("/hello2")
	public String hello2(){

	return "hello springboot2!!";


	}

    @Autowired
    private SimplyTestDaoImpl simplyTestDaoImpl;
 
    @RequestMapping(value = "/simplyall",method = RequestMethod.GET)
 
    public String testSelectAll(){
        List<SimplyTest> list = simplyTestDaoImpl.testSelectAll();
        for (SimplyTest simplyTest : list) {
            System.out.println(simplyTest.toString());
        }
        return "查询所有!";
    }

    @RequestMapping(value = "/simplydelete",method = RequestMethod.GET)
    
    public String testDelete(){
    	Integer id=10;
            System.out.println(simplyTestDaoImpl.testDelete(id));
        return "删除 "+id;
    } 
    
 @RequestMapping(value = "/simplybyid",method = RequestMethod.GET)
    
    public String testSelectPersonById(){
    	Integer id=10;
            System.out.println(simplyTestDaoImpl.testSelectById(id).toString());
        return "查询 "+id;
    } 
    
    @RequestMapping(value = "/simplyupdate",method = RequestMethod.GET)
 
    public String testUpdate(){
    	String name="王五";
    	Integer id=10;
            System.out.println( simplyTestDaoImpl.testUpdate(name,id));
        return "修改成功!";
    }
    
    @RequestMapping(value = "/simplyinsert",method = RequestMethod.GET)
    
    public String testInstert(){
    	
    	
    	try {
            String filePath = "E:\\达梦\\vim_en.gif";
            File file = new File(filePath);
            String filePath2 = "E:\\达梦\\work\\SQL_server导出所有索引.txt";
            File file2 = new File(filePath2);
            InputStream in;
            in = new BufferedInputStream(new FileInputStream(file));
            byte[] bytes1 = new byte[102400];
            byte[] bytes2 = new byte[102400];
            in.read(bytes1);
            InputStream in2 = new BufferedInputStream(new FileInputStream(file));
            in2.read(bytes2);
            BufferedReader reader = new BufferedReader(new InputStreamReader
                    (new FileInputStream(file2),"UTF-8"));
            StringBuffer stringBuffer = new StringBuffer("");
            String str = null;
            while((str = reader.readLine())!=null) {
                stringBuffer.append(str);
                stringBuffer.append("\n");
            }
//            BigData bigData = new BigData(null,bytes1,bytes2,stringBuffer.toString());
            SimplyTest simplyTest=new SimplyTest();
        	simplyTest.setName("李四");
        	simplyTest.setId(10);
        	simplyTest.setCreatetime( new Date(System.currentTimeMillis()));
        	simplyTest.setPhoto(bytes1);
        	simplyTest.setTxt(stringBuffer.toString());
            System.out.println( simplyTestDaoImpl.testInstert(simplyTest));

            in.close();
            in2.close();
            reader.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    	
        return "新增成功!";
    }
}

2.4 运行 SpringBoot

以下提供全部方法截图,具体结果依据代码内容而定。

2.4.1 新增

image.png

2.4.2 查询

image.png

2.4.3 修改

image.png

2.4.4 查 ID

image.png

2.4.5 删除

image.png

评论
后发表回复

作者

文章

阅读量

获赞

扫一扫
联系客服