javascript
Eclipse中实现SpringBoot与Mybatis整合(图文教程带源码)
場景
數據庫中數據
實現效果
項目結構
前面參照
Eclipse中新建SpringBoot項目并輸出HelloWorld
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/88359953
Eclipse中新建SpringBoot項目完成對json、pojo、map、list的請求
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/88360041
實現
數據庫設計
新建user表
業務實現
完成業務實現所需包的創建以及包下類的創建
pojo包下新建User.java
package com.example.demo.pojo;public class User {private Integer id;private String username;private String password;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}}resource下新建mapper文件夾,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.example.demo.mapper.UserMapper"><select id="findAll" resultType="user">select * from user</select></mapper>mapper下新建config文件夾,config下新建sqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration></configuration>?
新建service包,并在包下新建UserService.java接口類
package com.example.demo.service;import java.util.List;import org.springframework.stereotype.Service;import com.example.demo.pojo.User; @Service public interface UserService {public List<User> findAll();}
在service下新建UserServiceImpl.java
?
新建mapper包并在下面新建UserMapper.java接口
package com.example.demo.mapper;import java.util.List;import org.apache.ibatis.annotations.Mapper;import com.example.demo.pojo.User;@Mapper public interface UserMapper {/*** 查詢所有用戶*/public List<User> findAll(); }新建controller包并在下面新建UserController.java
package com.example.demo.controller;import java.util.ArrayList;import java.util.HashMap; 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.example.demo.pojo.User; import com.example.demo.service.UserService;/*** @author badao* @Description:測試* @Time:2019年3月6日 下午11:20:19*/ @RestController public class UserController {//注入Service服務對象@Autowiredprivate UserService userService;/*** 整合SSM*/@RequestMapping("ssm")public List<User> findAll(){List<User> list = userService.findAll();return list;}}
?配置文件配置
打開application.properties
#springboot整合Mybatis框架 #加載Mybatis配置文件 mybatis.mapper-locations=classpath:mapper/*Mapper.xml mybatis.config-location=classpath:mapper/config/sqlMapConfig.xml #定義別名 mybatis.type-aliases-package=com.example.demo.pojo #數據源 spring.datasource.url=jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf8 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.username=root spring.datasource.password=123?
打開pom.xml
<?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.3.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.badao</groupId><artifactId>helloSpringBoot</artifactId><version>0.0.1-SNAPSHOT</version><name>helloSpringBoot</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-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- mybatis整合Springboot --><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>8.0.11</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>打開HelloSpringBootApplication.java,注意其位置要和業務包同級。
package com.example.demo;import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @MapperScan("com.example.demo.mapper") @SpringBootApplication public class HelloSpringBootApplication {public static void main(String[] args) {SpringApplication.run(HelloSpringBootApplication.class, args);}}運行
在HelloSpringBootApplication.java中右擊選擇Run as--Java Application
打開瀏覽器輸入:
http://localhost:8080/ssm
源碼下載
https://download.csdn.net/download/badao_liumang_qizhi/11029904
總結
以上是生活随笔為你收集整理的Eclipse中实现SpringBoot与Mybatis整合(图文教程带源码)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Mysql8.0注意url变更写法
- 下一篇: SpringBoot中使用POI实现Ex