spring-boot加mybatis实现数据库访问
Posted zhizou
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring-boot加mybatis实现数据库访问相关的知识,希望对你有一定的参考价值。
项目结构
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实现数据库访问的主要内容,如果未能解决你的问题,请参考以下文章
Spring-boot+Mybatis+Maven+MySql搭建实例
IntelliJ IDEA 2017版 spring-boot2.0.4+mybatis反向工程;mybatis+springboot逆向工程