您的位置:首页 > 编程语言 > Java开发

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

2017-07-24 14:53 811 查看

1.创建一个maven项目SpringBootDemo

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

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>com.test.springboot</groupId>
<artifactId>test-springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>test-springboot</name>
<description>Demo project for Spring WebMvc</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.5.RELEASE</version>
<relativePath/>
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.7</java.version>
</properties>

<dependencies>
<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>1.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.15</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
</plugins>
</build>
</project>

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


到这里就已经配置完成了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息