Java工作笔记-Spring Boot中使用Mybatis操作达梦数据库

假装没事ソ 提交于 2019-12-22 13:46:46

这里以达梦数据库为例,使用MyBatis对数据库进行增删改查

这里先给出截图

此处为了简单,直接在Controller里面调用dao中数据:

关键代码如下:

application.properties

spring.datasource.driver-class-name=dm.jdbc.driver.DmDriver
spring.datasource.url=jdbc:dm://XXX.XXX.XXX.XXX
spring.datasource.username=XXXX
spring.datasource.password=XXXX

mybatis.mapperLocations=classpath:/*.xml

AppInfo.java

i1package com.example.demo.object;

import lombok.Data;

@Data
public class AppInfo{

    private String appXXId;
    private String nodeXXId;
    private String appXXEngName;
    private String appXXChnName;
    private String appXXStatus;
}

AppInfoMapper.java

package com.example.demo.dao;


import com.example.demo.object.AppInfo;
import org.apache.ibatis.annotations.Mapper;


import java.util.List;

@Mapper
public interface AppInfoMapper {

    List<AppInfo> getAllAPPInfo();
    void addAPPInfo(AppInfo appInfo);
    void updateAPPInfo(AppInfo appInfo);
    void deleteAPPInfo(AppInfo appInfo);
}

TestController.java

package com.example.demo.controller;

import com.example.demo.dao.AppInfoMapper;

import com.example.demo.object.AppInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@ResponseBody
public class TestController {

    @Resource
    private AppInfoMapper mapper;

    @GetMapping("/select")
    public String select(){

        System.out.println(mapper.getAllAPPInfo());
        return "<h1>Hello World</h1>";
    }

    @GetMapping("/insert")
    public String insert(){

        AppInfo appInfo = new AppInfo();
        appInfo.setAppXXId("10086");
        appInfo.setNodeXXId("10010");
        appInfo.setAppXXChnName("呵呵哒");
        appInfo.setAppXXEngName("English");
        mapper.addAPPInfo(appInfo);
        System.out.println(mapper.getAllAPPInfo());
        return "<h1>Hello World</h1>";
    }

    @GetMapping("/update")
    public String update(){

        AppInfo appInfo = new AppInfo();
        appInfo.setAppXXId("10086");
        appInfo.setNodeXXId("10000");
        appInfo.setAppXXChnName("哦!是吗");
        appInfo.setAppXXEngName("Chinese");
        mapper.updateAPPInfo(appInfo);
        System.out.println(mapper.getAllAPPInfo());
        return "<h1>Hello World</h1>";
    }

    @GetMapping("/delete")
    public String delete(){

        AppInfo appInfo = new AppInfo();
        appInfo.setAppXXId("10086");
        mapper.deleteAPPInfo(appInfo);
        System.out.println(mapper.getAllAPPInfo());
        return "<h1>Hello World</h1>";
    }
}

关键的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.example.demo.dao.AppInfoMapper">

    <select id="getAllAPPInfo" resultType="com.example.demo.object.AppInfo">
        select APPXXID as appXXId, NODEXXID as nodeXXId, APPXX_ENG_NAME as appXXEngName, APPXX_CHN_NAME as appXXChnName, APPXX_STATUS as appXXStatus from XX.XX
    </select>

    <insert id="addAPPInfo" useGeneratedKeys="false" parameterType="com.example.demo.object.AppInfo">
        insert into XX.XX(APPXXID, NODEXXID, APPXX_ENG_NAME, APPXX_CHN_NAME) values(#{appXXId}, #{nodeXXId}, #{appXXEngName}, #{appXXChnName})
    </insert>

    <update id="updateAPPInfo" parameterType="com.example.demo.object.AppInfo">
        update XX.XX set NODEXXID=#{nodeXXId}, APPXX_ENG_NAME=#{appXXEngName}, APPXX_CHN_NAME=#{appXXChnName} where APPXXID=#{appXXId}
    </update>

    <delete id="deleteAPPInfo" parameterType="com.example.demo.object.AppInfo">
        delete from XX.XX where APPXXID=#{appXXId}
    </delete>
    
</mapper>

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!