springboot+mybatis + daydream database

Keywords: Python Mybatis Spring Java Maven

preparation:

 

First, install the damon6 database. After installation, the following table is established

Then, it's very important to write in this way, or you will report unlimited errors.

Query method of Dayun database:

select * "from" library name "." schema name "." table name "

 

Secondly, download the database driver package of daydream (this can't be downloaded online through maven! )

There are many packages on the Internet, including dm6, dm7...

All tried, incompatible with springboot. The latest bag with the name above.

 

Then, drive the driver package into the local maven warehouse. The command is as follows:

 

mvn install:install-file -DgroupId=com.dm -DartifactId=DmJdbcDriver -Dversion=1.7.0 -Dpackaging=jar -Dfile=D:\DmJdbcDriver.jar

 

 

 

Construction project:

Create a new springboot 1.5.21, select only web


The architecture is as follows:

pom file:

<?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 http://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>1.5.21.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.qif.xdqdm</groupId>
    <artifactId>xdqdm</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>xdqdm</name>

    <description>Demo project for Spring Boot</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>


        <!--Add to servlet Dependence-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>


        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>

        <!--Must specify mybatis Version 3.4.1 Otherwise, we can't connect to daydream database. -->
        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.1</version>
        </dependency>


        <dependency>
             <groupId>com.dm</groupId>
             <artifactId>DmJdbcDriver</artifactId>
        <version>1.7.0</version>
         </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.32</version>
        </dependency>


        <!-- Thermal deployment -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.0.0</version>
            </plugin>
        </plugins>
    </build>

</project>

 

 

Myconfig (set the initial page to index):

package com.qif.xdqdm.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;


@Configuration
public class MyConfig extends WebMvcConfigurerAdapter {

    //Be-all WebMvcConfigurerAdapter Components work together
    @Bean //Register component in container
    public WebMvcConfigurerAdapter webMvcConfigurerAdapter() {
        WebMvcConfigurerAdapter adapter = new WebMvcConfigurerAdapter() {
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                registry.addViewController("/").setViewName("index");

            }
        };
        return adapter;
    }


}

TxtUtil is a tool for your own use.

UserController:

package com.qif.xdqdm.controller;

import com.qif.xdqdm.model.User;
import com.qif.xdqdm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 
 * @Title: UserController
 * @ProjectName xdqdm
 * @Description: TODO
 * @date 2019/7/24  11:13
 */
@Controller
public class UserController {

    @Autowired
    UserService userService;


    @RequestMapping("/getUserList")
    @ResponseBody
    public Map<String, Object> getUserList(HttpServletRequest request){
        Map<String, Object> map = new HashMap<String, Object>();
        List<User> userList =  userService.getUser();


        map.put("data", userList);
        map.put("message", "Success");

        return map;
    }
}

UserDao:

package com.qif.xdqdm.dao;

import com.qif.xdqdm.model.User;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author 
 * @Title: UserDao
 * @ProjectName xdqdm
 * @Description: TODO
 * @date 2019/7/24  11:17
 */
@Repository
public interface UserDao {
    List<User> getUserList();
}

User:

package com.qif.xdqdm.model;

/**
 * @author
 * @Title: User
 * @ProjectName sfyz
 * @Description: TODO
 * @date 2019/7/24  10:51
 */
public class User {
    private Integer id;
    private String name;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

UserService:

package com.qif.xdqdm.service;

import com.qif.xdqdm.model.User;

import java.util.List;

/**
 * @author 
 * @Title: UserService
 * @ProjectName xdqdm
 * @Description: TODO
 * @date 2019/7/24  11:15
 */
public interface UserService {
    List<User> getUser();
}

UserServiceImpl:
package com.qif.xdqdm.service.impl;

import com.qif.xdqdm.dao.UserDao;
import com.qif.xdqdm.model.User;
import com.qif.xdqdm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 
 * @Title: UserServiceImpl
 * @ProjectName xdqdm
 * @Description: TODO
 * @date 2019/7/24  11:15
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserDao userDao;
    @Override
    public List<User> getUser() {
        return userDao.getUserList();
    }
}

 

XdqdmApplication:

You can't use the tomcat8 startup class of springboot, which is not compatible with the damen6 driver package!

package com.qif.xdqdm;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
//scanning dao package
@MapperScan(value = "com.qif.xdqdm.dao")
@SpringBootApplication
public class XdqdmApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(XdqdmApplication.class, args);
    }


    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(this.getClass());
    }

}

Select tomcat7:

 

UserDao.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.qif.xdqdm.dao.UserDao">

    <select id="getUserList" resultType="com.qif.xdqdm.model.User">

        SELECT * FROM "TEST"."SYSDBA"."USER" ;

    </select>


</mapper>

 

index: "helloworld"

 

application.properties:

#Driving package
spring.datasource.driver-class-name=dm.jdbc.driver.DmDriver
#12345 is the default port number of damen6 Test is the library name
spring.datasource.url=jdbc:dm://localhost:12345/TEST

#Default account and password of daydream database 6
spring.datasource.username=SYSDBA
spring.datasource.password=SYSDBA

 

application.yml:

mybatis:
  # Specify sql mapping file location
  mapper-locations: classpath:mybatis/mapper/*.xml

logback (modify GBK to UTF-8 in case of disorderly code):

<?xml version="1.0" encoding="GBK"?>
<configuration  scan="true" scanPeriod="60 seconds" debug="false">
    <contextName>logback</contextName>
    <springProperty scope="context" name="logLevel" source="logging.levels"/>
    <springProperty scope="context" name="logPath" source="logging.path"/>
    <!--Output to console-->
    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <!-- <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
             <level>ERROR</level>
         </filter>-->
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} =================%contextName [%thread] %-5level %logger{36} - %msg%n</pattern>
            <charset>GBK</charset>
        </encoder>
    </appender>

    <!--output to a file-->
    <appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${logPath}logback.%d{yyyy-MM-dd}.log</fileNamePattern>
        </rollingPolicy>
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n</pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>

    <root level="info">
        <appender-ref ref="console" />
        <appender-ref ref="file" />
    </root>

</configuration>

Posted by phelpsa on Thu, 17 Oct 2019 08:41:39 -0700