栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot整合Mybatis连接MS SQL server数据库

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot整合Mybatis连接MS SQL server数据库

一、配置SQL server
  1. 打开Microsoft SQL Server Management Studio 18
  2. 点开:安全性-登录名-找到需要操作的数据库名称,右键点击选择属性
  3. 选择状态,按图中操作
  4. 桌面右键点击我的电脑,选择管理
  5. 依次展开服务和应用程序—SQL Server配置管理器—SQL Server网络配置—MSSQLSERVER的协议,然后右键点击TCP/IP选择启用
  6. 右键点击TCP/IP选择属性、选择IP地址,将所有的IP地址改为127.0.0.1,将TCP端口号改为1433,将所有已启动改为是,如图即可


7. 在Microsoft SQL Server Management Studio 18中重启服务

二、在IDEA中使用SpringBoot整合Mybatis连接SQL server数据库

1.新建springboot项目




2. 建包

3. 编写配置文件application.yml

spring:
  datasource:
    driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
    url: jdbc:sqlserver://localhost:1433;database=new_admin
    username: sa
    password: 123456

mybatis:
  type-aliases-package: com.test3.mapper
  mapper-locations: classpath:mapper/*Mapper.xml

  1. 配置启动类的扫描包

    5.添加测试类
    1. controller
package com.test3.controller;


import com.test3.server.StudentServer;
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;

@Controller
public class StudentController {

    @Autowired
    private StudentServer studentServer;

    @RequestMapping("/stu")
    @ResponseBody
    public Object selectStudentAll(){

        return studentServer.studentAll();
    }

}
  1. server层
package com.test3.server;

import java.util.List;
import java.util.Map;

public interface StudentServer {

    List studentAll();
}
package com.test3.server.Impl;

import com.test3.mapper.StudentMapper;
import com.test3.server.StudentServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class StudentServerImpl implements StudentServer {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public List studentAll() {
        return studentMapper.studentAll();
    }
}
  1. mapper层
package com.test3.mapper;

import org.apache.ibatis.annotations.Mapper;

import java.util.List;
import java.util.Map;

public interface StudentMapper {
    List studentAll();
}
  1. resources包下的mapper文件夹下的xml文件对应Java包下的mapper



    
    

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/425644.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号