2.springboot2.x整合mysql和mybatis(以xml的方式)

qq_37164847發表於2018-08-22

參考連結: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

 

相關文章