赞
踩
依赖
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- </dependency>
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-devtools</artifactId>
- </dependency>
- <dependency>
- <groupId>com.microsoft.sqlserver</groupId>
- <artifactId>sqljdbc4</artifactId>
- <version>4.0</version>
- </dependency>
- <dependency>
- <groupId>com.baomidou</groupId>
- <artifactId>mybatis-plus-boot-starter</artifactId>
- <version>3.4.3</version>
- </dependency>
- </dependencies>

yml文件
- spring:
- datasource:
- driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
- url: jdbc:sqlserver://127.0.0.1;DatabaseName=stu_db;
- username: sa
- password: 2xxx
-
- mybatis-plus:
- configuration:
- map-underscore-to-camel-case: false
- log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
创建实体pojo
package com.chen.pojo; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; import net.sf.jsqlparser.expression.DateTimeLiteralExpression; import java.time.LocalDate; import java.time.LocalDateTime; import java.util.Date; @Data @TableName("T.Student") public class Student { @TableId(value = "Sno") private String Sno; private String Sname; private String Ssex; private Integer Sage; private String Sdept; private Date Sdate; private String phone; }
注意如果你创建了模式的话要加上模式名,不然会找不到。
接下来我们简单做个查询测试下
- package com.chen.mapper;
-
- import com.baomidou.mybatisplus.core.mapper.BaseMapper;
- import com.chen.pojo.Student;
- import org.apache.ibatis.annotations.Mapper;
- import java.util.List;
- @Mapper
- public interface StudentMapper extends BaseMapper<Student> {
- List<Student> getList();
- }
service
- package com.chen.sevice;
-
-
- import com.baomidou.mybatisplus.extension.service.IService;
- import com.chen.pojo.Student;
-
- import java.util.List;
-
- public interface StudentService extends IService<Student> {
- List<Student> getList();
- }
impl
- package com.chen.sevice.impl;
-
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.chen.mapper.StudentMapper;
- import com.chen.pojo.Student;
- import com.chen.sevice.StudentService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- import java.util.List;
-
- @Service
- public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
-
- @Autowired
- StudentMapper studentMapper;
-
- @Override
- public List<Student> getList() {
- List<Student> students = studentMapper.selectList(null);
- return students;
- }
- }

controller
- package com.chen.controller;
-
- import com.chen.pojo.Student;
- import com.chen.sevice.StudentService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- import java.util.List;
-
- @RestController
- @RequestMapping("/stu")
- public class StudentController {
-
- @Autowired
- StudentService studentService;
-
- @GetMapping
- public List<Student> getList(){
- return studentService.getList();
- }
-
- }

访问结果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。