idea springboot mybatis

1、首先添加maven引用,javax.xml.bind不添加會報錯 java.lang.ClassNotFoundException: javax.xml.bind.DatatypeConverter

<!-- 持久層mybatis框架 -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.2</version>
</dependency>

<!-- 數據庫驅動程序 -->
<dependency>
    <groupId>com.microsoft.sqlserver</groupId>
    <artifactId>mssql-jdbc</artifactId>
    <scope>runtime</scope>
</dependency>

<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.3.0</version>
</dependency>

 

2、在resource中添加文件夾mapper,添加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.demo1.mapper.BusinessMapper">

    <resultMap type="com.example.demo1.domain.Business" id="businessResult">
        <id property="businessID" column="businessID"/>
        <result property="loanKind" column="LoanKind"></result>
        <result property="productType" column="ProductType"></result>

        <collection property="bills" ofType="com.example.demo1.domain.Bill">
            <id property="billID" column="BillID"/>
            <result property="billMonth" column="BillMonth"/>
            <result property="billType" column="BillType"/>
            <result property="billStatus" column="BillStatus"/>
        </collection>
    </resultMap>

    <select id="getBusiness" resultMap="businessResult">
      SELECT * FROM dbo.Business b
      JOIN dbo.Bill bi ON b.BusinessID=bi.BusinessID
      WHERE b.BusinessID=#{0}
   </select>

    <select id="getBusinessOne" resultType="com.example.demo1.domain.Business">
      SELECT * FROM dbo.Business WHERE BusinessID=#{0}
   </select>

</mapper>

3、添加package爲domain層,然後添加實體類

package com.example.demo1.domain;

import lombok.Data;

import java.util.List;

@Data
public class Business {
    private int businessID;
    private String loanKind;
    private Integer productType;
    List<Bill> bills;
}
package com.example.demo1.domain;

import lombok.Data;

@Data
public class Bill {

    private int billID;
    private String billMonth;
    private int billType;
    private int billStatus;
}

4、添加package爲mapper層,然後添加映射xml的方法

package com.example.demo1.mapper;

import com.example.demo1.domain.Business;

public interface BusinessMapper {
    Business getBusinessOne(int id);

    Business getBusiness(int id);
}

5、添加package爲service層,然後添加服務層

package com.example.demo1.service;

import com.example.demo1.domain.Business;
import com.example.demo1.mapper.BusinessMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BusinessService {
    @Autowired
    private BusinessMapper businessMapper;

    public Business getBusinessOne(int id) {
        return businessMapper.getBusinessOne(id);
    }

    public Business getBusiness(int id) {
        return businessMapper.getBusiness(id);
    }
}

6、然後添加控制器層

package com.example.demo1.controller;

import com.example.demo1.domain.Business;
import com.example.demo1.service.BusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HomeController {
    @Autowired
    private BusinessService businessService;

    @GetMapping("/home/index/{id}")
    public Business index(@PathVariable("id") int id) {
        return businessService.getBusinessOne(id);
    }

    @GetMapping("/home/bill/{id}")
    public Business bill(@PathVariable("id") int id) {
        return businessService.getBusiness(id);
    }
}

7、然後在main函數,也就是啓動類中添加掃描mapper的接口層

package com.example.demo1;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.example.demo1.mapper")
public class Demo1Application {

    public static void main(String[] args) {
        SpringApplication.run(Demo1Application.class, args);
    }
}

8、application.yml文件中添加數據庫連接配置和mybatis的xml配置

spring:
  datasource:
    driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
    url: jdbc:sqlserver://192.168.1.1:1433;database=PostLoan;
    username: sa
    password: 123456
mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.example.demo1.domain

項目結構圖

 

注意事項:

1、resource文件夾中的mapper文件夾中的xml文件的命名空間,就是代碼的mapper層,需要具體到mapper層中的接口,比如在本示例中的xml的命名空間就是下面這樣

<mapper namespace="com.example.demo1.mapper.BusinessMapper">

在代碼的mapper層中有個BusinessMapper的接口,具體到接口名。

2、代碼mapper的接口方法一定要和xml中的id相對應。

3、配置文件中mybatis配置type-aliases-package配置項是指向代碼中的 domain層,但是我發現我指向mapper層也是沒有問題的???

4、如果不想在接口層,也就是mapper層的每個類上添加@Mapper註解,那麼一定要在啓動類上添加@MapperScan註解,來掃描mapper層的所有接口。

 

以上是基於xml的寫法,還可以基於註解方式來寫sql

package com.example.demo1.mapper;

import com.example.demo1.domain.Instruction;
import org.apache.ibatis.annotations.Select;

public interface IInstruction {
    @Select("SELECT * FROM pay.DeductInstruction where DeductInstructionID=#{id}")
    Instruction getInstruction(int id);
}

 

源碼地址:https://github.com/awith/demo1

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章