2.springboot2.x整合mysql和mybatis(以xml的方式)
參考連結:https://www.cnblogs.com/gavincoder/p/10105762.html
專案結構:
資料庫中的測試表(sys_user_role):
1.在springboot專案的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.4.RELEASE</version> <relativePath/> </parent> <groupId>com.shengquan</groupId> <artifactId>meeting</artifactId> <version>0.0.1-SNAPSHOT</version> <name>meeting</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <!--Spring Boot的核心啟動器,包含了自動配置、日誌和YAML--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!--springboot程式測試依賴,如果是自動建立專案預設新增--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--springboot web模組支援,自動幫我們引入了web模組開發需要的相關jar包--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--MySQL 連線驅動依賴--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!-- SpringBoot Mybatis 依賴 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency> <!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.0</version> </dependency> </dependencies> <!--maven構建--> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2.yml檔案
server: port: 9002 servlet: context-path: /meeting spring: #資料來源 datasource: url: jdbc:mysql://localhost:3306/water?useUnicode=true&characterEncoding=utf8&useSSL=true username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver #mybatis配置 mybatis: typeAliasesPackage: com.shengquan.meeting.entity mapperLocations: classpath:mapper/*.xml
3.entity目錄下實體類:
import lombok.Data; /** * @description:測試實體類 * @author: Administrator * @date: 2019-04-25 10:06 */ @Data public class Department { private long id; private long user_id; private long role_id; }
4.mapper目錄下:
import com.shengquan.meeting.entity.Department; import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Repository; import java.util.List; /** * @description: * @author: Administrator * @date: 2019-04-25 10:41 */ @Mapper @Repository public interface TestMapper { List<Department> findall(); }
5.resources目錄下新建mapper目錄,建立TestMapper.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.shengquan.meeting.mapper.TestMapper"> <select id="findall" resultType="com.shengquan.meeting.entity.Department"> SELECT * FROM sys_user_role </select> </mapper>
6.service目錄中:
import com.shengquan.meeting.entity.Department; import com.shengquan.meeting.mapper.TestMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; /** * @description: * @author: Administrator * @date: 2019-04-25 10:45 */ @Service public class TestService { @Autowired private TestMapper testMapper; public List<Department> findaa(){ return testMapper.findall(); } }
7.controller目錄中:
import com.shengquan.meeting.service.TestService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * @description: * @author: Administrator * @date: 2019-04-25 09:02 */ @RestController public class TestController { @Autowired private TestService testService; @GetMapping("test") public String test(){ System.out.println("查詢到資料=="+testService.findaa().toString()); return "測試"+testService.findaa().toString(); } }
8.測試:
http://localhost:9002/meeting/test
demo原始碼:https://gitee.com/xusea/springbootmybatis.git
相關文章
- MyBatis 與 SpringBoot 整合:註解和xml兩種使用方式介紹MyBatisSpring BootXML
- SpringBoot 實戰 (十三) | 整合 MyBatis (XML 版)Spring BootMyBatisXML
- Spring Boot入門(七):使用MyBatis訪問MySql資料庫(xml方式)Spring BootMyBatisMySql資料庫XML
- Spring整合Mybatis方式二 - SqlSessionTemplateSpringMyBatisSQLSession
- Spring+Mybatis(一)無mapper.xml方式SpringMyBatisAPPXML
- MyBatis從入門到精通(二):MyBatis XML方式的基本用法之SelectMyBatisXML
- SpringBoot快速整合Mybatis(去XML化+註解進階)Spring BootMyBatisXML
- MyBatis從入門到精通(三):MyBatis XML方式的基本用法之多表查詢MyBatisXML
- MyBatis從入門到精通(四):MyBatis XML方式的基本用法之增刪改MyBatisXML
- MyBatis和Spring整合的奧祕MyBatisSpring
- MyBatis mapping.xml中的flushCache和useCache的使用MyBatisAPPXML
- 「Mybatis系列」Mybatis開發方式和配置MyBatis
- Mybatis和其他主流框架的整合使用MyBatis框架
- Spring Boot(六)整合 MyBatis 操作 MySQL 8Spring BootMyBatisMySql
- SpringBoot-16-之整合MyBatis-xml篇+單元測試Spring BootMyBatisXML
- SpringBoot邊學邊用(二)SpringBoot 整合 Mybatis(xml檔案形式)Spring BootMyBatisXML
- Spring整合Mybatis方式一 - 常規整合 - 註冊對映器SpringMyBatis
- Mybatis原始碼分析(二)XML的解析和Annotation的支援MyBatis原始碼XML
- oracle和mybatis整合,批次插入OracleMyBatis
- Spring boot入門(二):Spring boot整合MySql,Mybatis和PageHelper外掛Spring BootMySqlMyBatis
- Shiro和Spring MVC、Mybatis整合教程SpringMVCMyBatis
- 求大佬幫助我!!!IDEA中Mybatis的mapper.xml和dao.xml出現問題!!!IdeaMyBatisAPPXML
- XML解析-最快的方式SAXXML
- 《手寫Mybatis》第4章:Mapper XML的解析和註冊使用MyBatisAPPXML
- Mybatis 基礎xml對映MyBatisXML
- java-Mybatis XML 對映器(select,insert, update 和 delete)JavaMyBatisXMLdelete
- Mybatis去xml化:我再也不想寫xml了MyBatisXML
- Spring基於XML方式的使用SpringXML
- MyBatis基於xml檔案的 CURD案例MyBatisXML
- Springboot整合MybatisSpring BootMyBatis
- shiro 整合MybatisMyBatis
- Spring 整合 MyBatisSpringMyBatis
- SpringMVC整合MybatisSpringMVCMyBatis
- springBoot 整合 mybatisSpring BootMyBatis
- Spring整合MyBatisSpringMyBatis
- mybatis整合springMyBatisSpring
- ABAP和XML資料格式互相轉換的兩種方式XML
- Spring Boot 整合 MyBatis和 SQL Server實踐Spring BootMyBatisSQLServer