一:什么是Mybatis。
mybatis官方網站(http://www.mybatis.org/mybatis-3/zh/index.html)中是這樣描述的:
MyBatis 是一款優秀的持久層框架,它支持定制化 SQL、存儲過程以及高級映射。MyBatis 避免了幾乎所有的 JDBC 代碼和手動設置參數以及獲取結果集。MyBatis 可以使用簡單的 XML 或注解來配置和映射原生信息,將接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java對象)映射成數據庫中的記錄。
相信已經有很多朋友用過,或者是正在使用mybatis了,那么接下來我們就整合到Spring boot中去吧。
如果你不了解spring boot,請看一下,我的spring boot簡單示例文章吧:目錄 , SpringBoot之簡單入門
二:開始搭建
1,打開eclipse。依次點擊 File -> New -> Other。選擇 Spring Starter Project ,然后Next
2.填寫項目信息,然后Next
3.選擇Spring boot版本,在編寫這篇文章的時候,spring boot的最新版本是2.0.5,所以就使用了這個版本。
4.選擇項目依賴,選擇web,mysql驅動包,mybatis
5.最后點擊Finish。等待一會,項目就創建完成了。創建好的項目目錄如下:
我們也來看一下pom.xml中的內容:可以看到,mysql的驅動包和mybatis已經有了。
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zcz</groupId> <artifactId>learnSpringBootWithMybatis2</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>learnSpringBootWithMybatis2</name> <description>learnSpringBootWithMybatis</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
6.接下來就要寫代碼了。新建controller,entity,dao,servcie,service.impl包。在資源文件夾中新建mapper包。在資源文件夾中新建application.yml文件。新建完成后的文件目錄如下圖:
7.編寫application.yml配置文件。內容如下
# 默認使用開發配置
spring:
profiles:
active: dev
#配置mybatis參數
mybatis:
type-aliases-package: com.zcz.entity
mapper-locations: classpath:mapper/*.xml
---
#開發配置
spring:
profiles: dev
datasource:
url: jdbc:mysql://localhost:3306/learnspringbootwithmybatis
driver-class-name: com.mysql.jdbc.Driver
username: root
password: 123
8.新建數據庫learnspringbootwithmybatis,並在數據庫中新建表user
9.在entity包中新建User類,代碼如下:
package com.zcz.entity; public class User { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
10.在dao中,新建UserDao接口類,並添加@Mapper注解,代碼如下:
類中用到了@Mapper注解,如果你不了解他們的用法和意義的話可以參考 常用注解記錄
package com.zcz.dao; import java.util.List; import org.apache.ibatis.annotations.Mapper; import com.zcz.entity.User; @Mapper public interface UserDao { public List<User> findAllUser(); }
11.在service中新建UserService 接口,代碼如下:
package com.zcz.service; import java.util.List; import com.zcz.entity.User; public interface UserService { public List<User> getUsers(); }
12.在service.impl中新建UserServiceImpl實現類,實現UserService接口,添加@Service注解,代碼如下:
類中用到了@Service,@Autowired注解,如果你不了解他們的用法和意義的話可以參考 常用注解記錄
package com.zcz.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.zcz.dao.UserDao; import com.zcz.entity.User; import com.zcz.service.UserService; @Service public class UserServiceImpl implements UserService { @Autowired private UserDao userDao; @Override public List<User> getUsers() { // TODO Auto-generated method stub return userDao.findAllUser(); } }
13.在controller中新建UserController類,並配置訪問路徑,代碼如下:
類中用到了@RestController,@RequestMapping注解,如果你不了解他們的用法和意義的話可以參考 常用注解記錄
package com.zcz.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.zcz.entity.User; import com.zcz.service.UserService; @RestController @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @RequestMapping("/getUser") public List<User> getUsers(){ return userService.getUsers(); } }
14,在mapper中新建UserMapper.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.zcz.dao.UserDao"> <select id="findAllUser" resultType="com.zcz.entity.User"> select id,name from user </select> </mapper>
15,修改數據庫,添加兩天記錄
16,進入LearnSpringBootWithMybatis2Application類中,鼠標右鍵:Run as -> Spring Boot App。啟動項目。並訪問http://localhost:8080/user/getUser
17,好了,本次Spring Boot 整合Mybatis的過程就只有這些了。雖然這只是一個簡單的配置,但是我們成功了,不是嗎?
大家也發現了,這里只有一個查詢的功能,接下來我將繼續實現增,刪,改的內容。在這里:SpringBoot之整合Mybatis(增,改,刪)。
18,我把源代碼上傳到了github倉庫:https://github.com/ZCC1/learnSpringBootWithMybatis2.git。可以clone下來參考。歡迎star。
原創不易,轉發請注明出處:https://www.cnblogs.com/zhangchengzi/p/9662247.html