先說一些注解:
@EnableAutoConfiguration 可以幫助SpringBoot應用將所有符合條件的@Configuration配置都加載到當前SpringBoot創建並使用的IoC容器。
@ComponentScan 掃包
@Configuration 用於定義配置類,可替換xml配置文件
使用以上是三個注解 則可以實現 springboot 的啟動類的功能,不過每次 寫的太得 所以 可以使用@SpringBootApplication 代替三個注解,實現啟動類功能
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class), @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) }) public @interface SpringBootApplication
@SringBootApplication只能作用於同級目錄之下,其余目錄之下無法掃描不起作用
使用springboot搭建mybatis
第一步:導入 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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>springboot-mybatis-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-mybatis-demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 添加 mybatis所需要的依賴 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!-- 引入數據庫連接 注意版本,版本低了 執行的時候 報錯 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
第二步:創建數據庫表
CREATE TABLE `users` (
`name` varchar(50) DEFAULT NULL,
`age` int(50) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

創建一個名為users的表,有兩個字段name和age
第三步:編寫mapper類 創建一個 名為com.example.mapper的包,並創建 UserMapper接口
package com.example.mpper; import java.util.List; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Update; @Mapper//如果不用mapper注解 則需要在啟動類中配置@MapperScan(basePackages = { "包名" }) public interface UserMapper { @Select("SELECT * FROM USERS WHERE NAME = #{name}") List<com.example.entity.User> findByName(@Param("name") String name); @Insert("INSERT INTO USERS(NAME, AGE) VALUES(#{name}, #{age})") int insert(@Param("name") String name, @Param("age") Integer age); @Delete("DELETE FROM USERS WHERE AGE=#{age}") void delete(@Param("age") int age); @Update("UPDATE USERS SET AGE=#{age} WHERE NAME=#{name}") void update(@Param("name") String name, @Param("age") int age); }
@Mapper注解:mybatis的注解,不用像以前那樣配置 xml文件,然后在xml文件里面 寫 sql語句了
第四步:創建 UserService類,
package com.example.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import com.example.entity.User; import com.example.mpper.UserMapper; @Controller public class UserService { @Autowired public UserMapper mapper; public List<User> findUser(String name) { return mapper.findByName(name); } public void insertUser(String name, int age) { mapper.insert(name, age); } public void delet(int age) { mapper.delete(age); } public void update(String name, int age) { mapper.update(name, age); } }
第五步: 編寫 controller類
package com.example.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.example.entity.User; import com.example.service.UserService; @Controller public class UserController { @Autowired public UserService service; @RequestMapping("/selectUser") @ResponseBody public List<User> selectUser(String name) { List<User> users = service.findUser(name); return users; } @RequestMapping("/insertUser") public String insertUser(String name, int age) { service.insertUser(name, age); return "success"; } @RequestMapping("/deletUser") @ResponseBody public String deletUser(int age) { service.delet(age); return "success"; } @RequestMapping("/updateUser") @ResponseBody public String updateUser(String name, int age) { service.update(name, age); return "success"; } }
第六步:編寫啟動類
編寫啟動類的時候 選擇用的是@SpringBootApplication注解啟動,所以 要保證所有的都再同一目錄結構
package com.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringbootMybatisDemoApplication { public static void main(String[] args) { SpringApplication.run(SpringbootMybatisDemoApplication.class, args); } }
完整項目包結構:

如果只是這樣,啟動的時候會報錯,找不到 數據源,所以我們還需要配置數據源
第七步:配置 數據源 springboot 的配置文件 有兩種 一種是properties文件 一種 是yml文件,原來一直用的是properties文件,沒有yml文件好用。可讀性,編寫的時候都要方便些
spring: datasource: url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
driver-class-name: com.mysql.jdbc.Driver username: root password: root
這里如果 使用yml文件沒有自動提示 或者補全的話 可以升級或者安裝sts插件,在拋出一個問題 如果有多個數據源的時候springboot如何處理多個數據源,按照上面步驟就可以簡單的完成一個 spring boot+mybatis的crud
如果哪里有問題的,有人到了這個文章 則留言 改正...........
