一、参考文献
1.1 (直接点Quick Start)
二、步骤
好爽,不需要xml,基本不要配置,直接可以写
2.1 搭建好springboot框架,pom中需要添加如下
我用的springboot版本是
1.5.7.RELEASE
org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.1 mysql mysql-connector-java runtime
2.2 application.properties配置
#配置数据库spring.datasource.url=jdbc:mysql://localhost/villagespring.datasource.username=rootspring.datasource.password=rootspring.datasource.driver-class-name=com.mysql.jdbc.Driver
2.3 实体类(与数据库数据映射)
package com.clover286.entity;import java.io.Serializable;public class Test implements Serializable { private static final long serialVersionUID = 1L; private String id; private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Test{" + "id='" + id + '\'' + ", name='" + name + '\'' + '}'; }}
2.4 mapper接口
package com.clover286.mapper;import com.clover286.entity.Test;import org.apache.ibatis.annotations.Mapper;import org.apache.ibatis.annotations.Select;@Mapperpublic interface TestMapper { @Select("SELECT id, name FROM test WHERE id = #{id}") Test findTest(String id);}
2.5 调用
package com.clover286.controller;import com.clover286.entity.Test;import com.clover286.mapper.TestMapper;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class Welcome { @Autowired private TestMapper testMapper; @RequestMapping("/") public String home() { Test test = testMapper.findTest("1"); return test.toString(); }}
2.6 数据库sql
DROP TABLE IF EXISTS `test`;CREATE TABLE `test` ( `id` varchar(32) NOT NULL, `name` varchar(20) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;