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
- Spring Boot 整合 Mybatis 和 MySQLSpring BootMyBatisMySql
- Spring和cxf3的整合,以maven的方式SpringMaven
- Spring整合Mybatis方式二 - SqlSessionTemplateSpringMyBatisSQLSession
- Spring Boot入門(七):使用MyBatis訪問MySql資料庫(xml方式)Spring BootMyBatisMySql資料庫XML
- SpringBoot 實戰 (十三) | 整合 MyBatis (XML 版)Spring BootMyBatisXML
- MyBatis從入門到精通(二):MyBatis XML方式的基本用法之SelectMyBatisXML
- MyBatis從入門到精通(三):MyBatis XML方式的基本用法之多表查詢MyBatisXML
- MyBatis從入門到精通(四):MyBatis XML方式的基本用法之增刪改MyBatisXML
- 「Mybatis系列」Mybatis開發方式和配置MyBatis
- Spring+Mybatis(一)無mapper.xml方式SpringMyBatisAPPXML
- MyBatis和Spring整合的奧祕MyBatisSpring
- Mybatis和其他主流框架的整合使用MyBatis框架
- Mybatis分頁和Spring的整合MyBatisSpring
- oracle和mybatis整合,批次插入OracleMyBatis
- Spring整合Mybatis方式一 - 常規整合 - 註冊對映器SpringMyBatis
- SpringBoot快速整合Mybatis(去XML化+註解進階)Spring BootMyBatisXML
- XML解析的兩種方式DOM和SAXXML
- Spring Boot(六)整合 MyBatis 操作 MySQL 8Spring BootMyBatisMySql
- Mybatis原始碼分析(二)XML的解析和Annotation的支援MyBatis原始碼XML
- mybatis逆向工程和SSM框架整合MyBatisSSM框架
- MyBatis mapping.xml中的flushCache和useCache的使用MyBatisAPPXML
- SpringBoot--整合Mybatis和RedisSpring BootMyBatisRedis
- SpringBoot-16-之整合MyBatis-xml篇+單元測試Spring BootMyBatisXML
- Spring boot入門(二):Spring boot整合MySql,Mybatis和PageHelper外掛Spring BootMySqlMyBatis
- spring 整合 mybatis 及mybatis 的 crud 操作SpringMyBatis
- mybatis和springmvc整合遇到的問題小結MyBatisSpringMVC
- FreeBSD 整合Unix和Windows的方式(轉)Windows
- Shiro和Spring MVC、Mybatis整合教程SpringMVCMyBatis
- XML解析-最快的方式SAXXML
- 解析xml的幾種方式XML
- Mybatis 基礎xml對映MyBatisXML
- Spring 整合 MyBatisSpringMyBatis
- SpringMVC整合MybatisSpringMVCMyBatis
- mybatis整合springMyBatisSpring
- Spring整合MyBatisSpringMyBatis
- springmvc mybatis 整合SpringMVCMyBatis
- shiro 整合MybatisMyBatis