码迷,mamicode.com
首页 > 数据库 > 详细

spring-boot加mybatis实现数据库访问

时间:2019-08-20 18:35:03      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:alt   username   zone   request   start   server   repo   config   thymeleaf   

项目结构

技术图片

1.maven

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.zhizous</groupId>
    <artifactId>blogs</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>blogs</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <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.1.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.35</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.17</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

 2.Application.class(启动类)

package com.zhizous.blogs;

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

@MapperScan("com.zhizous.blogs.mapper") //启动时扫描所有的Mapper
@SpringBootApplication
public class BlogsApplication {

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

}

3.application.yml(配置文件)

server:
  port: 8080
  servlet: #配置项目的访问路径
    context-path: / #(http://localhost/boot)

spring:
  datasource: #连接数据的基本配置
    username: root
    password: admin
    url: jdbc:mysql://localhost:3306/blogs?serverTimezone=UTC #设置时区,不然可能会报错
    driver-class-name: com.mysql.cj.jdbc.Driver

mybatis:
  configuration:
    map-underscore-to-camel-case: true #开启驼峰命名转换机制

4.创建一个实体类(music)

package com.zhizous.blogs.bean;

public class Music {

    private int id;//id
    private String musWriter;//音乐作者
    private String musName;//音乐名
    private int musTime;//音乐总秒数
    private String musSrc;//音乐文件路径
    private String musImg;//音乐图片

    public String getMusImg() {
        return musImg;
    }

    public void setMusImg(String musImg) {
        this.musImg = musImg;
    }

    public int getId() {
        return id;
    }

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

    public String getMusWriter() {
        return musWriter;
    }

    public void setMusWriter(String musWriter) {
        this.musWriter = musWriter;
    }

    public String getMusName() {
        return musName;
    }

    public void setMusName(String musName) {
        this.musName = musName;
    }

    public int getMusTime() {
        return musTime;
    }

    public void setMusTime(int musTime) {
        this.musTime = musTime;
    }

    public String getMusSrc() {
        return musSrc;
    }

    public void setMusSrc(String musSrc) {
        this.musSrc = musSrc;
    }
}

5.设置mapper

package com.zhizous.blogs.mapper;

import com.zhizous.blogs.bean.Music;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface MusicMapper {
    //    select * from music where id=#{id}
    @Select("select * from music where id = #{id}")
    public Music getMusicById(@Param("id") Integer id);

    @Select("select * from music")
    public List<Music> getMusics();

}

 

6.设置server

package com.zhizous.blogs.service;

import com.zhizous.blogs.bean.Music;
import com.zhizous.blogs.mapper.MusicMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MusicService {
    @Autowired
    MusicMapper musicMapper;

    public Music getMusicById(Integer id) {
        return musicMapper.getMusicById(id);
    }

    public List<Music> getMusics(){
        return musicMapper.getMusics();
    }
}

7.最后设置我们的控制器

package com.zhizous.blogs.controller;
import com.zhizous.blogs.bean.Music;
import com.zhizous.blogs.service.MusicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping(path = "/music", produces = "application/json;charset=UTF-8")
public class MusicController {

    @Autowired
    private MusicService musicService;

    //@PathVariable Integer id
    @RequestMapping("/getmusic")
    public Music getMusic(Integer id) {
        Music music = musicService.getMusicById(id);
        return music;
    }

    @RequestMapping("/musiclist")
    public List<Music> getMusicList() {
        List<Music> musics = musicService.getMusics();
        return musics;
    }

}

 8.浏览器查看结果

技术图片

技术图片

9.数据库

技术图片

 

spring-boot加mybatis实现数据库访问

标签:alt   username   zone   request   start   server   repo   config   thymeleaf   

原文地址:https://www.cnblogs.com/zhizou/p/11384396.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!