准备工作:
首先,安装达梦6数据库。安装完之后如下建表
然后,很重要的一点(写法一定要这样写,否则无限报错)
达梦数据库查表方式:
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
架构如下图:
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 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> <!--添加servlet的依赖-->
<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> <!--必须指定mybatis版本为3.4.1否则无法连接达梦数据库 -->
<!-- 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> <!-- 热部署 -->
<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(设置初始页面为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<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", "成功"); 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:
这里不能用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:
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:
#驱动包
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):
<?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"/>
<!--输出到控制台-->
<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> <!--输出到文件-->
<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>