引言
本篇我们讲解下在Spring Boot中使用MyBatis访问MySql数据库(xml方式)的简单用法。
1. 前期准备
假设你的机器已经安装好了MySql,我们先执行如下语句创建数据库和表:
CREATE DATABASE springbootaction_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:
org.mybatis.spring.boot mybatis-spring-boot-starter 1.1.1mysql mysql-connector-java 5.1.35com.alibaba fastjson 1.2.47
说明:引入了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/springbootaction_db username: root password:
4. 修改配置文件
在application.yml中添加mybatis配置:
mybatis: mapper-locations: classpath:mybatis/*.xml type-aliases-package: com.zwwhnly.springbootaction.mybatis.entity
其中,mapper-locations为mybatis xml文件的路径,type-aliases-package为定义的实体所在的包名。
5. 定义数据库实体
定义数据库实体Author:
package com.zwwhnly.springbootaction.mybatis.entity;import com.alibaba.fastjson.annotation.JSONField;public class Author { @JSONField(name = "author_id") private Integer authorId; @JSONField(name = "author_name") private String authorName; @JSONField(name = "pen_name") private String penName; public Integer getAuthorId() { return authorId; } public void setAuthorId(Integer 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; }}
6. 编写Dao层代码
定义接口AuthorMapperV2:
package com.zwwhnly.springbootaction.mybatis.xml;import com.zwwhnly.springbootaction.mybatis.entity.Author;import org.apache.ibatis.annotations.Mapper;import org.apache.ibatis.annotations.Param;import java.util.List;@Mapperpublic interface AuthorMapperV2 { int add(@Param("author_name") String authorName, @Param("pen_name") String penName); int update(@Param("author_name") String authorName, @Param("pen_name") String penName, @Param("id") Integer id); int delete(Integer id); Author findAuthor(@Param("id") Integer id); List findAuthorList();}
注意:接口要添加@Mapper注解。
7. 编写Service层代码
定义类AuthorServiceV2:
package com.zwwhnly.springbootaction.mybatis.xml;import com.zwwhnly.springbootaction.mybatis.entity.Author;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.List;@Servicepublic class AuthorServiceV2 { @Autowired private AuthorMapperV2 authorMapperV2; public int add(String authorName, String penName) { return this.authorMapperV2.add(authorName, penName); } public int update(String authorName, String penName, Integer id) { return this.authorMapperV2.update(authorName, penName, id); } public int delete(Integer id) { return this.authorMapperV2.delete(id); } public Author findAuthor(Integer id) { return this.authorMapperV2.findAuthor(id); } public List findAuthorList() { return this.authorMapperV2.findAuthorList(); }}
注意:类添加@Service注解。
8. 添加mybatis xml文件
在resources目录下,新建mybatis文件夹,然后新建AuthorMapper.xml文件,分别实现上面定义的新增,修改,删除,获取单个作者信息,获取作者列表功能:
<?xml version="1.0" encoding="UTF-8"?> INSERT INTO author(author_name, pen_name) VALUES(#{author_name}, #{pen_name}); UPDATE author SET author_name = #{author_name,jdbcType=VARCHAR}, pen_name = #{pen_name,jdbcType=VARCHAR} WHERE author_id = #{id,jdbcType=INTEGER}; DELETE FROM author WHERE author_id = #{id}; SELECT author_id, author_name, pen_name FROM author WHERE author_id = #{id}; SELECT author_id, author_name, pen_name FROM author;
9. 编写Controller代码
新建控制器AuthorControllerV2:
package com.zwwhnly.springbootaction.controller;import com.alibaba.fastjson.JSONObject;import com.zwwhnly.springbootaction.mybatis.entity.Author;import com.zwwhnly.springbootaction.mybatis.xml.AuthorServiceV2;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 AuthorControllerV2 { @Autowired private AuthorServiceV2 authorServiceV2; /** * 查询作者列表 */ @RequestMapping(value = "getAuthorListV2