Spring Boot入门(六):使用MyBatis访问MySql数据库(注解方式)
本系列博客记录自己学习spring boot的历程,如帮助到你,不胜荣幸,如有错误,欢迎指正!
本篇博客我们讲解下在spring boot中使用mybatis访问mysql数据库的简单用法。
1.前期准备
假设你的机器已经安装好了mysql,我们先执行如下语句创建数据库和表:
create database springbootdemo_db create table author ( author_id int auto_increment comment '作者id' primary key, author_name varchar(20) not null comment '姓名', pen_name varchar(20) not null comment '笔名' ) comment '作者';
2.修改pom文件
pom文件引入mybatis的starter pom和mysql的驱动,因后面要编写控制器,因此也引入下阿里巴巴的fastjson:
<dependency> <groupid>org.mybatis.spring.boot</groupid> <artifactid>mybatis-spring-boot-starter</artifactid> <version>1.1.1</version> </dependency> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <version>5.1.35</version> </dependency> <dependency> <groupid>com.alibaba</groupid> <artifactid>fastjson</artifactid> <version>1.2.47</version> </dependency>
说明:引入了mybatis-spring-boot-starter后,可以不再引用spring-boot-starter-jdbc,因为前者已经依赖于后者。
3.配置数据源
在resources/application.yml中配置数据源:
spring: datasource: driver-class-name: com.mysql.jdbc.driver url: jdbc:mysql://localhost:3306/springbootdemo_db username: root password:
4.定义数据库实体
定义数据库实体author:
package com.zwwhnly.springbootdemo.mybatis.entity; import com.alibaba.fastjson.annotation.jsonfield; public class author { @jsonfield(name = "author_id") private long authorid; @jsonfield(name = "author_name") private string authorname; @jsonfield(name = "pen_name") private string penname; public long getauthorid() { return authorid; } public void setauthorid(long authorid) { this.authorid = authorid; } public string getauthorname() { return authorname; } public void setauthorname(string authorname) { this.authorname = authorname; } public string getpenname() { return penname; } public void setpenname(string penname) { this.penname = penname; } }
5.编写dao层代码
定义接口authormapper:
package com.zwwhnly.springbootdemo.mybatis.annotation; import com.zwwhnly.springbootdemo.mybatis.entity.author; import org.apache.ibatis.annotations.*; import java.util.list; @mapper public interface authormapper { @insert("insert into author(author_name, pen_name) values(#{author_name}, #{pen_name})") int add(@param("author_name") string authorname, @param("pen_name") string penname); @update("update author set author_name = #{author_name}, pen_name = #{pen_name} where author_id = #{id}") int update(@param("author_name") string authorname, @param("pen_name") string penname, @param("id") integer id); @delete("delete from author where author_id = #{id}") int delete(integer id); @select("select author_id as authorid, author_name as authorname, pen_name as penname from author where author_id = #{id}") author findauthor(@param("id") long id); @select("select author_id as authorid, author_name as authorname, pen_name as penname from author") list<author> findauthorlist(); }
注意:接口要添加@mapper注解。
6.编写service层代码
定义类authorservice:
package com.zwwhnly.springbootdemo.mybatis.annotation; import com.zwwhnly.springbootdemo.mybatis.entity.author; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.service; import java.util.list; @service public class authorservice { @autowired private authormapper authormapper; public int add(string authorname, string penname) { return this.authormapper.add(authorname, penname); } public int update(string authorname, string penname, integer id) { return this.authormapper.update(authorname, penname, id); } public int delete(integer id) { return this.authormapper.delete(id); } public author findauthor(integer id) { return this.authormapper.findauthor(id); } public list<author> findauthorlist() { return this.authormapper.findauthorlist(); } }
注意:类添加@service注解。
7.编写controller代码
新建控制器authorcontroller:
package com.zwwhnly.springbootdemo.controller; import com.alibaba.fastjson.jsonobject; import com.zwwhnly.springbootdemo.mybatis.entity.author; import com.zwwhnly.springbootdemo.mybatis.annotation.authorservice; import org.springframework.beans.factory.annotation.autowired; import org.springframework.web.bind.annotation.*; import java.util.hashmap; import java.util.list; import java.util.map; @restcontroller @requestmapping(value = "/mybatis/author") public class authorcontroller { @autowired private authorservice authorservice; /** * 查询作者列表 */ @requestmapping(value = "getauthorlist", method = requestmethod.get) public map<string, object> getauthorlist() { list<author> authorlist = this.authorservice.findauthorlist(); map<string, object> param = new hashmap<>(); param.put("total", authorlist.size()); param.put("rows", authorlist); return param; } /** * 查询单个作者信息 */ @requestmapping(value = "/getauthor/{authorid:\\d+}", method = requestmethod.get) public author getauthor(@pathvariable integer authorid) { author author = this.authorservice.findauthor(authorid); if (author == null) { throw new runtimeexception("查询错误"); } return author; } /** * 新增 */ @requestmapping(value = "add", method = requestmethod.post) public void add(@requestbody jsonobject jsonobject) { string authorname = jsonobject.getstring("authorname"); string penname = jsonobject.getstring("penname"); try { this.authorservice.add(authorname, penname); } catch (exception e) { e.printstacktrace(); throw new runtimeexception("新增错误"); } } /** * 更新 */ @requestmapping(value = "/update/{authorid:\\d+}", method = requestmethod.put) public void update(@pathvariable integer authorid, @requestbody jsonobject jsonobject) { author author = this.authorservice.findauthor(authorid); string authorname = jsonobject.getstring("authorname"); string penname = jsonobject.getstring("penname"); try { this.authorservice.update(authorname, penname, author.getauthorid()); } catch (exception e) { e.printstacktrace(); throw new runtimeexception("更新错误"); } } /** * 删除 */ @requestmapping(value = "/delete/{authorid:\\d+}", method = requestmethod.delete) public void delete(@pathvariable integer authorid) { try { this.authorservice.delete(authorid); } catch (exception e) { throw new runtimeexception("删除错误"); } } }
8.使用postman验证
8.1验证新增
因为新增是post请求,因此这里我们使用下postman工具:
调用完接口,发现数据库新增数据成功。
然后用同样的方法新增下鲁迅的信息。
8.2验证更新
调用更新接口将鲁迅的名字从周作人修改为周树人:
调用完接口,发现数据库更新数据成功。
8.3验证获取列表
在浏览器访问http://localhost:8080/mybatis/author/getauthorlist,返回数据如下:
{ "total": 2, "rows": [ { "authorid": 1, "authorname": "王卫国", "penname": "路遥" }, { "authorid": 2, "authorname": "周树人", "penname": "鲁迅" } ] }
8.4验证获取单个数据
在浏览器访问http://localhost:8080/mybatis/author/getauthor/1,返回如下数据:
{ "authorid": 1, "authorname": "王卫国", "penname": "路遥" }
8.5验证删除
调用删除接口,将鲁迅的数据删除:
此时访问http://localhost:8080/mybatis/author/getauthorlist,返回数据只有1条了:
{ "total": 1, "rows": [ { "authorid": 1, "authorname": "王卫国", "penname": "路遥" } ] }
9.源码地址
原文地址:spring boot入门(六):使用mybatis访问mysql数据库(注解方式)
博客地址:
源码地址:
欢迎大家下载,有问题可以多多交流。