基于SpringBoot极简配置搭建Mybatis

前言

旨在浏览器键入Url就能放回JSON数据

安装

简单结构

在这里插入图片描述
这个目录结构图比较简陋,可以去官网配置生成,idea生成.

依赖

在文件pom.xml中添加一下三个依赖,SpringBoot的Web模块,Mybatis快速配置模块,以及Mysql驱动

		<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> 2.0.0</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

配置

在默认配置文件application.properties中配置mysql连接信息

spring.datasource.url=jdbc:mysql://xxx:33061/test
spring.datasource.username=test
spring.datasource.password=test123!
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

映射实体类

定义属性

/**
 * @author terry
 * @since 19/07/2019
 */
public class City {
    private Long id;

    private String name;

    private String state;

    private String country;

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

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }
}

Mapper接口

定制访问数据库的sql

/**
 * @author terry
 * @since 19/07/2019
 */
@Mapper
public interface CityMapper {

    @Select("select id, name, state, country from city where state = #{state}")
    City findByState(@Param("state") String state);

}

接口控制器

注入资源CityMapper,返回给浏览器

/**
 * @author terry
 * @since 19/07/2019
 */
@RestController
public class CityController {
    @Resource
    private CityMapper cityMapper;

    @GetMapping(value = "/index")
    public City info(){
        return this.cityMapper.findByState("CA");
    }
}

项目启动类

运行入口

/**
 * https://github.com/tengxing/WebFrameCollection/SpringBootMybatis-Simple
 * @author terry
 * @since 04/06/2019
 */
@SpringBootApplication
public class SbApplication implements CommandLineRunner {

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

数据库(跳过)

对数据库比较陌生的话,可以把如下代码放入项目启动类SbApplication

   @Resource
    SqlSessionFactory factory;

    @Override
    @SuppressWarnings("squid:S106")
    public void run(String... args) throws SQLException {
        initDb();
    }

    private void initDb() throws SQLException {
        Connection conn = factory.openSession().getConnection();
        Statement statement = conn.createStatement();
        statement.addBatch("drop table if exists city;");
        statement.addBatch("CREATE TABLE `city` (\n" +
                "  `id` bigint(20) DEFAULT NULL,\n" +
                "  `name` varchar(255) DEFAULT NULL,\n" +
                "  `state` varchar(255) DEFAULT NULL,\n" +
                "  `country` varchar(255) DEFAULT NULL\n" +
                ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;" );
        statement.addBatch("insert into city (name, state, country) values ('San Francisco', 'CA', 'US');");

        System.out.println("Creating table in given database...");
        statement.executeBatch();
        System.out.println("Created table in given database...");
        statement.close();
        conn.close();
    }

测试

在这里插入图片描述
代码示例:Github

后记

郑重说明:使用适用于对项目比较陌生,或者刚刚开始入手的开发人员,便于跑通项目流程,理解开发本质,快速上手,不适用于牛逼的老鸟。代码结构,业务逻辑隔离等等都先不考虑。

参考文章

mybatis官方文档:http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/index.html

评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值