SpringBoot整合mybits详细记录

github地址
首先建立Springboot项目,废话不多说,请看上一篇【从0搭建springboot】

搭建完成目录结构如图所示
在这里插入图片描述
创建包controller、model、mapper、service。resources下创建mapping文件夹,用于写sql语句,也可以用注解的方式直接写在mapper文件里。
下面直接贴代码:

  • pom.xml
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-thymeleaf</artifactId>
                     </dependency>
                <dependency>
                    <groupId>org.mybatis.spring.boot</groupId>
                    <artifactId>mybatis-spring-boot-starter</artifactId>
	<version>1.3.2</version>
               </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.0</version>
        </dependency>
      <dependency>
             <groupId>mysql</groupId>
             <artifactId>mysql-connector-java</artifactId>
          <version>8.0.15</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

person.java

package com.alpha.springmybatis.model;

public class Person {
    private Integer id;
    private String name;
    private Integer age;

    public Person() {
    }

    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return this.age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String toString() {
        return "id=" + this.id + ", name='" + this.name + '\'' + ", age=" + this.age;
    }
}

UserController.java

package com.alpha.springmybatis.controller;

import com.alpha.springmybatis.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({"/testBoot"})
public class UserController {
    @Autowired
    private UserService userService;

    public UserController() {
    }

    @RequestMapping({"getPerson/{id}"})
    public String getPerson(@PathVariable int id) {
        return this.userService.Sel(id).toString();
    }
}

UserMapper.java

package com.alpha.springmybatis.mapper;

import com.alpha.springmybatis.model.Person;
import org.springframework.stereotype.Repository;

@Repository
public interface UserMapper {
    Person Sel(int id);
}

UserService

package com.alpha.springmybatis.service;

import com.alpha.springmybatis.mapper.UserMapper;
import com.alpha.springmybatis.model.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    @Autowired
    UserMapper userMapper;

    public UserService() {
    }

    public Person Sel(int id) {
        return this.userMapper.Sel(id);
    }
}

SpringmybatisApplication

package com.alpha.springmybatis;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@MapperScan({"com.alpha.springmybatis.mapper"})
@SpringBootApplication
public class SpringmybatisApplication {
    SpringmybatisApplication() {
    }

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

PersonMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.alpha.springmybatis.mapper.UserMapper">

    <resultMap id="BaseResultMap" type="com.alpha.springmybatis.model.Person">
        <result column="id" jdbcType="INTEGER" property="id" />
        <result column="userName" jdbcType="VARCHAR" property="userName" />
        <result column="age" jdbcType="INTEGER" property="age" />
    </resultMap>

    <select id="Sel" resultType="com.alpha.springmybatis.model.Person">
        select * from person where id = #{id}
    </select>

</mapper>

数据表结构

CREATE TABLE `person` (
  `id` int(32) NOT NULL AUTO_INCREMENT,
  `userName` varchar(32) NOT NULL,
  `age` int(32) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

将resource文件夹下原有的application.properties文件删除,创建application.yml配置文件(备注:其实SpringBoot底层会把application.yml文件解析为application.properties),本文创建了两个yml文件(application.yml和application-dev.yml),分别来看一下内容
————————————————
application.yml

spring:
  profiles:
    active: dev

application-dev.yml

server:
  port: 8080

spring:
  datasource:
    username: root
    password: 12345678
    url: jdbc:mysql://localhost:3306/mybits?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    driver-class-name: com.mysql.jdbc.Driver

mybatis:
  mapper-locations: classpath:mapping/*Mapper.xml
  type-aliases-package: com.alpha.springmybatis.model

#showSql
logging:
  level:
    com:
      example:
        mapper : debug


两个文件的意思是:

在项目中配置多套环境的配置方法。
因为现在一个项目有好多环境,开发环境,测试环境,准生产环境,生产环境,每个环境的参数不同,所以我们就可以把每个环境的参数配置到yml文件中,这样在想用哪个环境的时候只需要在主配置文件中将用的配置文件写上就行如application.yml

笔记:在Spring Boot中多环境配置文件名需要满足application-{profile}.yml的格式,其中{profile}对应你的环境标识,比如:

application-dev.yml:开发环境
application-test.yml:测试环境
application-prod.yml:生产环境
至于哪个具体的配置文件会被加载,需要在application.yml文件中通过spring.profiles.active属性来设置,其值对应{profile}值。
最后启动,浏览器输入地址看看吧:http://localhost:8080/testBoot/getPerson/1

原文链接:https://blog.csdn.net/iku5200/article/details/82856621

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值