上篇寫了怎麼搭建一個最基礎的微服務項目。今天,主要通過實踐,總結下springboot整合mybatis,快速入門。好了,下面直接開始正題吧。
首先:創建一張基礎表,添加測試數據如下:
1:pom.xml添加mysql、jdbc、mybatis依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
2:修改application.properties文件:
#配置數據庫信息
jdbc.type=mysql
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#mybatis.mapper文件的位置
mybatis.mapper-locations=classpath*:mapper/*Mapper.xml
mybatis.type-aliases-package=com.example.demo.entity
3:Controller中新建HelloWorldTest類:
package com.example.demo.controller;
import com.example.demo.entity.student;
import com.example.demo.service.HelloWorldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/Demo")
public class HelloWorldTest{
@Autowired
HelloWorldService helloWorldService;
@RequestMapping(value = "/helloWorld",method = RequestMethod.GET)
public List<student> Test() throws Exception{
return helloWorldService.query();
}
}
4:建Service接口類及實現類:
HelloWorldService接口類:
package com.example.demo.service;
import com.example.demo.entity.student;
import java.util.List;
public interface HelloWorldService {
List<student> query() throws Exception;
}
HelloWorldServiceImpl實現類:
package com.example.demo.service.ServiceImpl;
import com.example.demo.DAO.HelloWorldDAO;
import com.example.demo.entity.student;
import com.example.demo.service.HelloWorldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class HelloWorldServiceImpl implements HelloWorldService {
@Autowired HelloWorldDAO helloWorldDAO;
@Override
public List<student> query() throws Exception{
return helloWorldDAO.query();
}
}
5:dao接口:
package com.example.demo.DAO;
import com.example.demo.entity.student;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
@Mapper
public interface HelloWorldDAO {
List<student> query() throws Exception;
}
6:編寫mapper.xml文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.demo.DAO.HelloWorldDAO">
<select id="query" resultMap="studentResult">
select * from student
</select>
<resultMap type="student" id="studentResult">
<result property="studentId" column="studentId"/>
<result property="studentName" column="studentName"/>
</resultMap>
</mapper>
7:在啓動類上添加掃描註解:
@SpringBootApplication(scanBasePackages={"com.example.*"})
@MapperScan("com.example.demo.DAO")
8:以上模塊整合完成後,項目結構如圖所示:
9:啓動項目,訪問地址,返回結果如圖所示:
總結:springboot整合mybatis完成了,實現了一個基本請求訪問數據庫並返回結果。如有問題,歡迎大佬留言討論!
知識就是要不斷的學習,不斷的複習,纔會記憶的更加的深刻。加油,美好的風景一直在路上!