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

一、配置SQL server

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

SpringBoot整合Mybatis连接MS SQL server数据库_第6张图片
7. 在Microsoft SQL Server Management Studio 18中重启服务
SpringBoot整合Mybatis连接MS SQL server数据库_第7张图片

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

1.新建springboot项目
SpringBoot整合Mybatis连接MS SQL server数据库_第8张图片
SpringBoot整合Mybatis连接MS SQL server数据库_第9张图片
SpringBoot整合Mybatis连接MS SQL server数据库_第10张图片
SpringBoot整合Mybatis连接MS SQL server数据库_第11张图片
2. 建包
SpringBoot整合Mybatis连接MS SQL server数据库_第12张图片
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

SpringBoot整合Mybatis连接MS SQL server数据库_第13张图片

  1. 配置启动类的扫描包
    SpringBoot整合Mybatis连接MS SQL server数据库_第14张图片
    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<Map> 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<Map> 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<Map> studentAll();
}
  1. resources包下的mapper文件夹下的xml文件对应Java包下的mapper

DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.test3.mapper.StudentMapper">
    
    <select id="studentAll" resultType="java.util.LinkedHashMap">
        select * from student
    select>
mapper>

你可能感兴趣的:(springboot,mybatis,数据库,spring,boot,sqlserver)