当前位置:   article > 正文

springboot+mybatisPlus连接SQLServer

springboot+mybatisPlus连接SQLServer

依赖

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-web</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-test</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.projectlombok</groupId>
  12. <artifactId>lombok</artifactId>
  13. </dependency>
  14. <dependency>
  15. <groupId>org.springframework.boot</groupId>
  16. <artifactId>spring-boot-devtools</artifactId>
  17. </dependency>
  18. <dependency>
  19. <groupId>com.microsoft.sqlserver</groupId>
  20. <artifactId>sqljdbc4</artifactId>
  21. <version>4.0</version>
  22. </dependency>
  23. <dependency>
  24. <groupId>com.baomidou</groupId>
  25. <artifactId>mybatis-plus-boot-starter</artifactId>
  26. <version>3.4.3</version>
  27. </dependency>
  28. </dependencies>

yml文件

  1. spring:
  2. datasource:
  3. driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
  4. url: jdbc:sqlserver://127.0.0.1;DatabaseName=stu_db;
  5. username: sa
  6. password: 2xxx
  7. mybatis-plus:
  8. configuration:
  9. map-underscore-to-camel-case: false
  10. log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

创建实体pojo

  1. package com.chen.pojo;
  2. import com.baomidou.mybatisplus.annotation.TableId;
  3. import com.baomidou.mybatisplus.annotation.TableName;
  4. import lombok.Data;
  5. import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
  6. import java.time.LocalDate;
  7. import java.time.LocalDateTime;
  8. import java.util.Date;
  9. @Data
  10. @TableName("T.Student")
  11. public class Student {
  12. @TableId(value = "Sno")
  13. private String Sno;
  14. private String Sname;
  15. private String Ssex;
  16. private Integer Sage;
  17. private String Sdept;
  18. private Date Sdate;
  19. private String phone;
  20. }

注意如果你创建了模式的话要加上模式名,不然会找不到。

接下来我们简单做个查询测试下

mapper

  1. package com.chen.mapper;
  2. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
  3. import com.chen.pojo.Student;
  4. import org.apache.ibatis.annotations.Mapper;
  5. import java.util.List;
  6. @Mapper
  7. public interface StudentMapper extends BaseMapper<Student> {
  8. List<Student> getList();
  9. }

 service

  1. package com.chen.sevice;
  2. import com.baomidou.mybatisplus.extension.service.IService;
  3. import com.chen.pojo.Student;
  4. import java.util.List;
  5. public interface StudentService extends IService<Student> {
  6. List<Student> getList();
  7. }

 impl

  1. package com.chen.sevice.impl;
  2. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  3. import com.chen.mapper.StudentMapper;
  4. import com.chen.pojo.Student;
  5. import com.chen.sevice.StudentService;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Service;
  8. import java.util.List;
  9. @Service
  10. public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
  11. @Autowired
  12. StudentMapper studentMapper;
  13. @Override
  14. public List<Student> getList() {
  15. List<Student> students = studentMapper.selectList(null);
  16. return students;
  17. }
  18. }

 controller

  1. package com.chen.controller;
  2. import com.chen.pojo.Student;
  3. import com.chen.sevice.StudentService;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.GetMapping;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RestController;
  8. import java.util.List;
  9. @RestController
  10. @RequestMapping("/stu")
  11. public class StudentController {
  12. @Autowired
  13. StudentService studentService;
  14. @GetMapping
  15. public List<Student> getList(){
  16. return studentService.getList();
  17. }
  18. }

访问结果:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/520957
推荐阅读
相关标签
  

闽ICP备14008679号