springboot+mybatis+达梦数据库

准备工作:

 

首先,安装达梦6数据库。安装完之后如下建表

springboot+mybatis+达梦数据库_第1张图片

然后,很重要的一点(写法一定要这样写,否则无限报错)

达梦数据库查表方式:

select  *  from    "库名"."模式名"."表名"

 

其次,下载达梦数据库驱动包(这个通过maven在线下载是下载不到的!)

网上的包很多,有dm6,dm7 ...。。。。。。

都试过,和springboot不兼容。用上面这个名字的最新的包。

 

再然后,把驱动包打入本地maven仓库,命令如下:

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

 

搭建项目:

新建springboot  1.5.21,只选择web

 

架构如下图:

springboot+mybatis+达梦数据库_第2张图片

  

pom文件:



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.21.RELEASE
         
    
    com.qif.xdqdm
    xdqdm
    0.0.1-SNAPSHOT
    war
    xdqdm

    Demo project for Spring Boot

    
        UTF-8
        UTF-8
        1.8
    

    


        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        


        
        
            javax.servlet
            javax.servlet-api
            3.1.0
            provided
        


        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.2
        

        
        
        
            org.mybatis
            mybatis
            3.4.1
        


        
             com.dm
             DmJdbcDriver
        1.7.0
         
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            com.alibaba
            fastjson
            1.2.32
        


        
        
            org.springframework.boot
            spring-boot-devtools
            true
        

    

    
        
            
                maven-war-plugin
                3.0.0
            
        
    

Myconfig(设置初始页面为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 {

    //所有的WebMvcConfigurerAdapter组件都会一起起作用
    @Bean //将组件注册在容器
    public WebMvcConfigurerAdapter webMvcConfigurerAdapter() {
        WebMvcConfigurerAdapter adapter = new WebMvcConfigurerAdapter() {
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                registry.addViewController("/").setViewName("index");

            }
        };
        return adapter;
    }


}

 

TxtUtil不用管,自己用的工具

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 getUserList(HttpServletRequest request){
        Map map = new HashMap();
        List userList =  userService.getUser();


        map.put("data", userList);
        map.put("message", "成功");

        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 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 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 getUser() {
        return userDao.getUserList();
    }
}

XdqdmApplication:

这里不能用springboot自带tomcat8启动类启动,与达梦6驱动包不兼容!

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;
//扫描dao包
@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());
    }

}

选择tomcat7:

springboot+mybatis+达梦数据库_第3张图片

 

UserDao.xml:





    


 

index: "helloworld"

 

application.properties:

#驱动包
spring.datasource.driver-class-name=dm.jdbc.driver.DmDriver
#12345为达梦6默认端口号 Test为库名
spring.datasource.url=jdbc:dm://localhost:12345/TEST

#达梦数据库6默认的账户和密码
spring.datasource.username=SYSDBA
spring.datasource.password=SYSDBA

 

 

application.yml:

mybatis:
  # 指定sql映射文件位置
  mapper-locations: classpath:mybatis/mapper/*.xml

logback(乱码则修改GBK为UTF-8):



    logback
    
    
    
    
        
        
            %d{HH:mm:ss.SSS} =================%contextName [%thread] %-5level %logger{36} - %msg%n
            GBK
        
    

    
    
        
            ${logPath}logback.%d{yyyy-MM-dd}.log
        
        
            %d{HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n
            UTF-8
        
    

    
        
        
    

 

转载于:https://www.cnblogs.com/MagicAsa/p/11237429.html

你可能感兴趣的:(springboot+mybatis+达梦数据库)