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的依赖是否包含如下,如果没有拷贝进去

        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.1.2
        

        
            com.microsoft.sqlserver
            mssql-jdbc
            runtime
        

添加实体层

  • 右击项目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);

    int createArticle(Article article);

    int updateAritcle(Article article);
}
  • resources下面添加mapper文件夹
  • mapper添加文件ArticleMapper.xml



    
    
        insert into Article
        (title, author) values (#{title}, #{author})
    
    
        update Article
        set title = #{title}, author = #{author}
        where id = #{id}
    

添加服务层

  • 添加Package:service
  • 里面添加接口:ArticleService
public interface ArticleService {
    Article getArticleById(int id);

    int createArticle(Article article);

    int updateAritcle(Article article);
}

添加服务实现

  • 添加实现:service下面添加Package:impl
  • 添加类:ArticleServiceImpl
@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleDao articleDao;

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

    @Override
    public int createArticle(Article article) {
        return articleDao.createArticle(article);
    }

    @Override
    public int updateAritcle(Article article) {
        return articleDao.updateAritcle(article);
    }
}

添加控制器

  • 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);
    }

    @PostMapping("/createArticle/{id}")
    @ResponseBody
    public int createArticle(@RequestBody Article article) {
        return this.articleService.createArticle(article);
    }

    @PutMapping("/updateAritcle/{id}")
    @ResponseBody
    public int updateAritcle(@PathVariable int id, @RequestBody Article article) {
        article.setId(id);
        return this.articleService.updateAritcle(article);
    }
}

运行项目

访问:http://localhost:8080/article/getArticleById/1

你可能感兴趣的:(Spring+Mybatis创建简单项目)