首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring Boot学习笔记(五)整合MyBatis实现数据库访问

Spring Boot学习笔记(五)整合MyBatis实现数据库访问

作者头像
Bug生活2048
发布2018-08-31 15:54:06
4000
发布2018-08-31 15:54:06
举报
文章被收录于专栏:Bug生活2048Bug生活2048

本文主要在上一篇[Spring Boot学习笔记(四)构建RESTful API标准工程实例]的基础上,整合MyBatis,实现简单的MySql数据库访问

引入依赖

这里主要依赖两个,一个是连接MySql的`mysql-connector-java`,还一个是SpringBoot整合MyBatis的核心依赖`mybatis-spring-boot-starter`

可以从maven仓库里生成对应的配置代码:

http://mvnrepository.com/

对应`pom.xml`如下:

<dependency>

<groupId>org.mybatis.spring.boot</groupId>

<artifactId>mybatis-spring-boot-starter</artifactId>

<version>1.3.1</version>

</dependency>

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<version>6.0.6</version>

</dependency>

配置数据库

自己安装下MySql,创建库和表,我这边demo的表如下:

CREATE TABLE `temp` (

`id` INT(11) NOT NULL AUTO_INCREMENT COMMENT '自增长id',

`name` VARCHAR(50) NOT NULL DEFAULT '' COMMENT '姓名',

`content` VARCHAR(100) NOT NULL DEFAULT '' COMMENT '描述',

PRIMARY KEY (`id`)

)

COMMENT='测试表';

`application.properties`下配置对应的数据库地址:

spring.datasource.url=jdbc:mysql://localhost:3306/mytest

spring.datasource.username=root

spring.datasource.password=root

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

实际代码编写

首先创建数据库映射对象`Temp`:

public class Temp {

private Integer id;

private String name;

private String content;

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public String getName() {

return name;

}

public void setId(String name) {

this.name = name;

}

public String getContent() {

return content;

}

public void setContent(String content) {

this.content = content;

}

}

然后创建`Temp`映射的操作`TempMapper`:

@Mapper

public interface TempMapper {

@Select("SELECT * FROM TEMP WHERE ID = #{id}")

Temp findById(@Param("id") Integer id);

@Insert("INSERT INTO TEMP(NAME, CONTENT) VALUES(#{name}, #{content})")

int insert(Temp temp);

@Update("UPDATE TEMP SET CONTENT=#{content} WHERE ID=#{id}")

int update(@Param("content") String content, @Param("id") Integer id);

}

编写对应的service层`TempServiceImpl`,`TempService`:

public interface TempService {

public Temp getTemp(Integer id);

public Boolean insertTemp(Temp entity);

public Boolean updateTemp(Temp entity);

}

@Service

public class TempServiceImpl implements TempService{

@Autowired

private TempMapper tempMapper;

@Override

public Temp getTemp(Integer id)

{

return tempMapper.findById(id);

}

@Override

public Boolean insertTemp(Temp entity)

{

return tempMapper.insert(entity)>0;

}

@Override

public Boolean updateTemp(Temp entity)

{

return tempMapper.update(entity.getContent(),entity.getId())>0;

}

}

最后编写Controller

@RestController

public class TempController {

@Autowired

private TempService tempService;

@ApiOperation(value="MyBatis_Demo", notes="MyBatis实现数据库访问demo")

@RequestMapping(value = "/temp",method = RequestMethod.GET)

public Temp getTemp()

{

Temp t=tempService.getTemp(1);

return t;

}

}

好啦,大功告成,如果没有意外的话,应该能顺利看到结果啦。

问题汇总

编写过程中不是一帆风顺的,我遇到的问题如下,供大家参考。

问题1:

启动时提示`Failed to start connecter[HTTP/1.1-8080]`

看到这个提示后还是比较好定位问题的,基本锁定端口被占用了,很好奇被谁占用了,于是排查了下:

打开命令窗口输入:

netstat -aon|findstr "8080"

找到对应的`PID`后到任务管理器去查看被什么进程给占用了

结果发现是个奇怪的进程,后来百度了下和`docker`有关,这才恍然大悟,之前在docker下调试 .net core项目的。关闭进程问题顺利解决。

问题2: Consider defining a bean of type 'com.example.api_demo.domain.repository.TempMapper' in your configuration.

这个自己犯了错误,忘记在启动类配置扫描包了,立马添加上。

问题3: The server time zone value '?泄???????' is unrecognized or represents more t...

也是一个奇葩的问题,百度一下,原来碰到的人挺多,原因就是高版本的MySql驱动会有数据库和系统时区差异,我用的版本是`6.0.6`,所以碰到了,修改下配置,执行时区就可以了

jdbc:mysql://localhost:3306/mytest?serverTimezone=UTC

或者用回`5.1.4`版本,该版本不会存在时区问题。

问题4 Could not autowired,No beans of '****' type found

这个问题困扰了我一阵,很尴尬,应该早点百度的或者先尝试一下编译的,后来发现编译能通过,运行也没问题,后来才知道,可参考下面的博客:

http://blog.csdn.net/u012453843/article/details/54906905

如果不想看到这个报错,可降低Autowired检测的级别:

总结

到这里,简单MyBatis实现数据库访问已经基本实现,但这不能满足实际业务需求,比如复杂的sql如何处理,如何访问多个库等。

同时,MyBatis的一些注解对于新手来说还是比较陌生的,下一篇我会尝试从我的角度去深度整理下MyBatis的复杂使用,和大家一起分享,共同进步。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-02-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Bug生活2048 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档