springboot+mybatis HelloWorld示例

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot+mybatis HelloWorld示例相关的知识,希望对你有一定的参考价值。

springboot+mybatis HelloWorld示例。

版本:springboot-1.5.1.RELEASE、mybatis-spring-boot-1.2.0

1,maven配置文件pom.xml如下:

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <mybatis-spring-boot>1.2.0</mybatis-spring-boot>
    </properties>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.1.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

    <!-- spring boot -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-validator</artifactId>
      </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- mybatis -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>${mybatis-spring-boot}</version>
        </dependency>

        <!-- mysql-jdbc -->
            <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.27</version>
        </dependency>
    </dependencies>

2,springboot配置文件:application.properties

    server.port=9090

    spring.datasource.url=jdbc:mysql://xxxx:3306/xxx?useUnicode=true&characterEncoding=utf8
    spring.datasource.username=xxx
    spring.datasource.password=xxx
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver

3,编写mybatis.model代码,HelloDO.java、HelloMapper.java、HelloMapper.xml

    package mybatis.model;

    public class HelloDO {
        private Long id;
        private String name;
        public Long getId() {
            return id;
        }
        public void setId(Long id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }   
    }

    HelloMapper.java
    package mybatis.model;

    import org.springframework.stereotype.Repository;

    @Repository(value="helloMapper")
    public interface HelloMapper {
        public void insert(HelloDO hello);
    }

    HelloMapper.xml
    <?xml version="1.0" encoding="utf-8" ?>
        <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTO Mapper 3.0//EN" 
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
        <mapper namespace="mybatis.model.HelloMapper">
        <resultMap type="mybatis.model.HelloDO" id="helloDO">
            <id column="ID" property="id" />
            <result column="NAME" property="name" />
        </resultMap>

        <insert id="insert" parameterType="mybatis.model.HelloDO">
            insert into
            HELLO(ID,NAME)
            values(#{id},#{name})
        </insert>
    </mapper>

4,编写service类:HelloWorldService.java

package mybatis.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import mybatis.model.HelloDO;
import mybatis.model.HelloMapper;

@Service("helloWorldService")
public class HelloWorldService {
    @Autowired
    private HelloMapper helloMapper;

    public void addHello(HelloDO helloDO) {
        // TODO Auto-generated method stub
        helloMapper.insert(helloDO);
    }
}

5,编写App类:App.java

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import mybatis.model.HelloDO;
import mybatis.service.HelloWorldService;

/**
* Hello world!
*
*/
@SpringBootApplication(scanBasePackages = "mybatis/service")//scanBasePackages配置用于spring扫描service类
@RestController
@MapperScan("mybatis")//MapperScan用于扫描mybatis配置文件
public class App {
    @Autowired
    HelloWorldService helloWorldService;

    @RequestMapping(value = "/create/hello", method = RequestMethod.POST, consumes = { "text/plain", "application/*" })
    public @ResponseBody long createHello(@RequestBody HelloDO helloDO) {
        helloWorldService.addHello(helloDO);
        return helloDO.getId();
    }

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

6,运行测试

1,根据HelloMapper.xml,在数据库中建表。

2,运行App类,然后通过http://localhost:9090/create/hello来测试

以上是关于springboot+mybatis HelloWorld示例的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot 整合其他框架 -- SpringBoot整合Mybatis

springboot.springboot用最简单的方式整合mybatis

yml的mybatis的sql查看;Mybatis+Springboot 控制台查看日志,Mybatis结合springboot打印日志

企业分布式微服务云SpringCloud SpringBoot mybatis (十三)Spring Boot整合MyBatis

SpringBoot.04.SpringBoot整合MyBatis

SpringBoot.04.SpringBoot整合MyBatis