本文主要在上一篇[Spring Boot學(xué)習(xí)筆記(四)構(gòu)建RESTful API標準工程實例]的基礎(chǔ)上揣炕,整合MyBatis族阅,實現(xiàn)簡單的MySql數(shù)據(jù)庫訪問
引入依賴
這里主要依賴兩個,一個是連接MySql的mysql-connector-java
,還一個是SpringBoot整合MyBatis的核心依賴mybatis-spring-boot-starter
可以從maven倉庫里生成對應(yīng)的配置代碼:
對應(yīng)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>
配置數(shù)據(jù)庫
自己安裝下MySql子眶,創(chuàng)建庫和表瀑凝,我這邊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
下配置對應(yīng)的數(shù)據(jù)庫地址:
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
實際代碼編寫
首先創(chuàng)建數(shù)據(jù)庫映射對象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;
}
}
然后創(chuàng)建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);
}
編寫對應(yīng)的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實現(xiàn)數(shù)據(jù)庫訪問demo")
@RequestMapping(value = "/temp",method = RequestMethod.GET)
public Temp getTemp()
{
Temp t=tempService.getTemp(1);
return t;
}
}
好啦,大功告成臭杰,如果沒有意外的話粤咪,應(yīng)該能順利看到結(jié)果啦。
問題匯總
編寫過程中不是一帆風(fēng)順的渴杆,我遇到的問題如下寥枝,供大家參考宪塔。
問題1
啟動時提示Failed to start connecter[HTTP/1.1-8080]
看到這個提示后還是比較好定位問題的,基本鎖定端口被占用了脉顿,很好奇被誰占用了蝌麸,于是排查了下:
打開命令窗口輸入:
netstat -aon|findstr "8080"
找到對應(yīng)的PID
后到任務(wù)管理器去查看被什么進程給占用了
結(jié)果發(fā)現(xiàn)是個奇怪的進程,后來百度了下和docker
有關(guān)艾疟,這才恍然大悟,之前在docker下調(diào)試 .net core項目的来吩。關(guān)閉進程問題順利解決。
問題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驅(qū)動會有數(shù)據(jù)庫和系統(tǒng)時區(qū)差異
,我用的版本是6.0.6
仪糖,所以碰到了柑司,修改下配置,執(zhí)行時區(qū)就可以了
jdbc:mysql://localhost:3306/mytest?serverTimezone=UTC
或者用回5.1.4
版本锅劝,該版本不會存在時區(qū)問題攒驰。
問題4
Could not autowired,No beans of '****' type found
這個問題困擾了我一陣,很尷尬故爵,應(yīng)該早點百度的或者先嘗試一下編譯的玻粪,后來發(fā)現(xiàn)編譯能通過,運行也沒問題诬垂,后來才知道劲室,可參考這篇博客
如果不想看到這個報錯,可降低Autowired檢測的級別:
總結(jié)
到這里结窘,簡單MyBatis實現(xiàn)數(shù)據(jù)庫訪問已經(jīng)基本實現(xiàn)很洋,但這不能滿足實際業(yè)務(wù)需求,比如復(fù)雜的sql如何處理隧枫,如何訪問多個庫等蹲缠。
同時,MyBatis的一些注解對于新手來說還是比較陌生的悠垛,下一篇我會嘗試從我的角度去深度整理下MyBatis的復(fù)雜使用,和大家一起分享娜谊,共同進步确买。