Spring boot框架结合MyBatis框架模板搭建

1.创建一个maven项目SpringBootDemo

2.在maven项目中的pom.xml文件中配置使用Spring boot需要用到的包



  4.0.0
  com.test.springboot
  test-springboot
  0.0.1-SNAPSHOT
  jar

  test-springboot
  Demo project for Spring WebMvc

  
    org.springframework.boot
    spring-boot-starter-parent
    1.2.5.RELEASE
    
  

  
    UTF-8
    1.7
  

  
    
      org.springframework.boot
      spring-boot-starter-web
    
    
      org.mybatis.spring.boot
      mybatis-spring-boot-starter
      1.1.1
    
    
      mysql
      mysql-connector-java
      5.1.15
    
      
          org.springframework.boot
          spring-boot-starter-data-jpa
      
  

  
    
      
        org.springframework.boot
        spring-boot-maven-plugin
      
      
        org.apache.tomcat.maven
        tomcat7-maven-plugin
        2.2
      
    
  

3.编写持久化类(实体类)

package com.test.springboot.po;

import javax.persistence.Entity;

@Entity
public class Article {
    private Integer id;
    private String title;
    private String author;

    public Article() {

    }

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

    @Override
    public String toString() {
        return "Article{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", author='" + author + '\'' +
                '}';
    }
}

4.配置application.xml(JDBC等)

spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

5.编写DAO

package com.test.springboot.dao;

import com.test.springboot.po.Article;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface ArticleDao {
    @Select("select * from article where id=#{id}")
    Article queryById(int id);
}

6.编写service(这里就省略的service的接口)

package com.test.springboot.service.impl;

import com.test.springboot.dao.ArticleDao;
import com.test.springboot.po.Article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ArticleServiceImpl {
    @Autowired
    ArticleDao articleDao;

    public Article queryById(int id){
        Article article = articleDao.queryById(id);
        return article;
    }
}

7.编写controller

package com.test.springboot.controller;

import com.test.springboot.po.Article;
import com.test.springboot.service.impl.ArticleServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ArticleController {
    @Autowired
    ArticleServiceImpl articleService;

    @RequestMapping(value = "/article/query")
    @ResponseBody
    public String queryById(){
        Article article = articleService.queryById(1);
        System.err.println(article);
        return  "Title:"+article.getTitle()+",Author:"+article.getAuthor();
    }
}

到这里就已经配置完成了

你可能感兴趣的:(Spring)