springboot和mybatis
发布日期:2021-08-31 01:31:21 浏览次数:2 分类:技术文章

本文共 2269 字,大约阅读时间需要 7 分钟。

hot3.png

一、参考文献

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;

 

转载于:https://my.oschina.net/Cubicluo/blog/1541397

转载地址:https://blog.csdn.net/weixin_34056162/article/details/91930244 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:iOS 组件化-使用cocoapods集成实战演练
下一篇:JSTL 核心标签库 (03) : 循环语句

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2024年04月18日 04时41分16秒