SpringBoot+Mybatis+SyBase整合详细

Posted 123早点睡

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot+Mybatis+SyBase整合详细相关的知识,希望对你有一定的参考价值。

声明:部分内容来自于

​​​​​​SpringBoot整合Mybatis(最全版)_lzh253985690的博客-CSDN博客

部分结合自身需要做出了修改

1.搭建项目环境

1.1新建项目

打开IDEA→File→New→Project

1.1新建项目

打开IDEA→File→New→Project

选择Maven→Next 

填写组名、版本然后Next→Finish建好项目

1.2 引入依赖

打开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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.lzh</groupId>
    <artifactId>springboot-mybatis02</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
    </parent>

    <!--指定编码为UTF-8 指定使用jdk1.8-->
    <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>
        <!-- -springboot 整合Web组件 整合SpringMVC 就会把传统方式的SpringMVC依赖的jar全部给下载来 -->
        <!-- 引入spring-boot-starter-web 帮你整合好所有相关的依赖jar包 原理 maven依赖传递 -->
        <!-- 原理: spring-boot-starter-parent< 中,整合号相关 jar依赖信息 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- jdbcTemplate 依赖 -->
     <!--   <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>-->
        <!-- springboot整合mybatis的依赖 -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>


        <dependency>
            <groupId>com.sybase</groupId>
            <artifactId>jconn4</artifactId>
            <version>1.0</version>
        </dependency>



        <!-- https://mvnrepository.com/artifact/net.sourceforge.jtds/jtds -->
        <dependency>
            <groupId>net.sourceforge.jtds</groupId>
            <artifactId>jtds</artifactId>
            <version>1.3.1</version>
        </dependency>


        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
        </dependency>


    </dependencies>

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



</project>

1.3 添加yml配置文件

在resources下新建配置文件application.yml

#端口号
server:
  port: 8889

#配置数据库
spring:
  datasource:
    username: hdev198
    password: Monjan123@
    url: jdbc:sybase:Tds:10.97.111.70:4100/db_qv?charset=big5hk
    driver-class-name: com.sybase.jdbc4.jdbc.SybDriver


mybatis:
  #mybatis的xml映射文件mapper存放路径
  mapper-locations: classpath:mapper/*Mapper.xml
  type-aliases-package: com.lzh.entity
  configuration:
    map-underscore-to-camel-case: true

#把sql语句打印在控制台
logging:
  level:
    com:
      example:
        mapper : debug


1.4 创建实体类实现业务流程

在java目录下创建如下包
com.lzh.controller、com.lzh.mapper、com.lzh.entity、com.lzh.service
在resources目录下创建mapper包

新建如下实体类 直接复制粘贴代码即可

 User.java

package com.lzh.entity;

public class User 
    private Long id;
    private String userName;
    private String passWord;

    public Long getId() 
        return id;
    

    public void setId(Long id) 
        this.id = id;
    

    public String getUserName() 
        return userName;
    

    public void setUserName(String userName) 
        this.userName = userName;
    

    public String getPassWord() 
        return passWord;
    

    public void setPassWord(String passWord) 
        this.passWord = passWord;
    

    @Override
    public String toString() 
        return "User" +
                "id=" + id +
                ", userName='" + userName + '\\'' +
                ", passWord='" + passWord + '\\'' +
                '';
    

 UserMapper .java

package com.lzh.mapper;

import com.lzh.entity.User;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

public interface UserMapper 
    @Select("SELECT * FROM db_test2 where id = #id")
    User getUserById1(@Param("id") Long id);


UserService .java

import org.springframework.stereotype.Service;

@Service
public class UserService 
    @Autowired
    UserMapper userMapper;

    public User getUserById1(Long id)
        User user = userMapper.getUserById1(id);
        return user;
    


UserController.java

package com.lzh.controller;

        import com.lzh.entity.User;
        import com.lzh.service.UserService;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.web.bind.annotation.PathVariable;
        import org.springframework.web.bind.annotation.RequestMapping;
        import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController 

    @Autowired
    private UserService userService;

    @RequestMapping("/getUserById1/id")
    public User getUserById1(@PathVariable Long id) 
        User user = userService.getUserById1(id);
        return user;
    
    

1.5 添加启动类

com.lzh包下创建类Application

package com.lzh;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@MapperScan("com.lzh.mapper") //mybatis扫描mapper的范围
@SpringBootApplication
public class Application 
    public static void main(String[] args) 
        SpringApplication.run(Application.class, args);
    

注意:

sybase的连接语句
   url: jdbc:sybase:Tds:10.97.111.70:4100/db_qv?charset=big5hk
   driver-class-name: com.sybase.jdbc4.jdbc.SybDriver


依赖为:
    <groupId>com.sybase</groupId>
            <artifactId>jconn4</artifactId>
            <version>1.0</version>
        </dependency>

以上是关于SpringBoot+Mybatis+SyBase整合详细的主要内容,如果未能解决你的问题,请参考以下文章

无法通过 jconn4.jar/Sybase 驱动版本 7 & Mybatis 框架更新 Sybase 数据库中的多条记录

Springboot 与 Sybase 服务器 - 无法加载驱动程序类:com.sybase.jdbc4.jdbc.SybDriver

SpringBoot 集成测试 Sybase 和 Testcontainers

SpringBoot使用·下篇(SpringBoot集成MyBatis+日志打印+MyBatis-plus)

SpringBoot使用·下篇(SpringBoot集成MyBatis+日志打印+MyBatis-plus)

SpringBoot 整合其他框架 -- SpringBoot整合Mybatis