1. 程式人生 > 實用技巧 >Spring+Mybatis建立簡單專案

Spring+Mybatis建立簡單專案

建立專案

  • New Project,選擇Spring Initiallizr,點選Next
  • 輸入Group:com.example,輸入Artifact:mybatisdemo,選擇Java Version 8,點選Next
  • 選擇Web的Spring Web,SQL的MyBatis Framework和MS Sql Server Driver,點選Next

準備資料

CREATE TABLE Article(
    id          INT IDENTITY PRIMARY KEY,
    title       VARCHAR(100) NOT NULL,
    author      VARCHAR(100) NOT NULL
);
INSERT INTO Article
VALUES ('Working with MyBatis in Spring', 'Baeldung');
INSERT INTO Article
VALUES ('Java程式設計思想', '張三');

新增配置

resources下面新增application.yml檔案

spring:
  datasource:
    name: mssqlDB
    url: jdbc:sqlserver://localhost:1433;databaseName=OnLineDB
    username: sa
    password: sa
    driverClassName: com.microsoft.sqlserver.jdbc.SQLServerDriver
mybatis:
  mapper-locations: classpath:mapper/*.xml

注意:卻下pom.xml的依賴是否包含如下,如果沒有拷貝進去

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.2</version>
        </dependency>

        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <scope>runtime</scope>
        </dependency>

新增實體層

  • 右擊專案com.example.mybatisdemo,新增Java Class
  • 輸入entity.Article
public class Article {
    private Integer id;

    private String title;

    private String author;

    private static final long serialVersionUID = 1L;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }
}

新增DAO層

  • com.example.mybatisdemo新增Package:dao
  • dao下面新增介面ArticleDao
@Mapper
public interface ArticleDao {
    Article getArticleById(int id);
}
  • resources下面新增mapper資料夾
  • mapper新增檔案ArticleMapper.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.mybatisdemo.dao.ArticleDao">
    <select id="getArticleById" resultType="com.example.mybatisdemo.entity.Article">
        select * from Article where id = #{id}
    </select>
</mapper>

新增服務層

  • 新增Package:service
  • 裡面新增介面:ArticleService
public interface ArticleService {
    Article getArticleById(int id);
}

新增服務層

  • 新增實現:service下面新增Package:impl
  • 新增類:ArticleServiceImpl
@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleDao articleDao;

    @Override
    public Article getArticleById(int id) {
        return articleDao.getArticleById(id);
    }
}

新增控制器

  • com.example.mybatisdemo新增Package:controller
  • 新增控制器:ArticleController
@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @GetMapping("/getHello")
    @ResponseBody
    public String getHello() {
        return "Hello";
    }

    @GetMapping("/getArticleById/{id}")
    @ResponseBody
    public Article getArticleById(@PathVariable int id) {
        return this.articleService.getArticleById(id);
    }
}

執行專案

訪問:http://localhost:8080/article/getArticleById/2