The seventh day .spring boot integrate mybatis

 

1. Integration ideas:

  1.1 Add rely mybatis

  1.2 the configuration data in the configuration file source information

  1.3 Interface mapeer write pojo mapper mapping file

  1.4 to manually configure mybatis scanning package, adding the master boot class @MapperScan

    1.5 server start springboot

2. Start the deployment project:

  2.1: Adding rely mybatis

    

<! - Integration Integration of springboot and mybatis -> 
< Dependencies > 
        < dependency > 
            < the groupId > org.mybatis.spring.boot </ the groupId > 
            < the artifactId > mybatis-springboot-Starter </ the artifactId > 
            < Version > 1.1.1 </ Version > 
        </ dependency > 
</ Dependencies > 
 <-! the mapper interface mapper mapper mapping file in dependence on the next packet mapper required -> 
    < Build > 
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.*</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

2.2: configuration data source information in the configuration file application.yml

#DB Configation  JPA Configation
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test
    username: root
    password: root

  jpa:
    database: MySQL
    generate-ddl: true
    show-sql: true

2.3 write pojo mapper interfaces mapeer mapping file

 

 If you do not want to interfaces and mapping files in a package under, you will encounter:

错误:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.offcn.mapper.UserMapper.getUserList

Solution:

A mapping file into exactly the same directory structure directory resources

2. According to the above configuration dependent plug behind to

mapper interfaces:

package com.xhn.mapper;

import com.xhn.pojo.Muser;

import java.util.List;

public interface MuserMapper {
    List<Muser> getUserList();
}

mapeer mapping file:

<?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.xhn.mapper.MuserMapper">
    <select id="getUserList" resultType="com.xhn.pojo.Muser">
    select * from user
  </select>
</mapper>

Corresponding to the writing controller interface methods:

package com.xhn.controller;

import com.xhn.mapper.MuserMapper;
import com.xhn.pojo.Muser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    private MuserMapper muserMapper;


    //使用mybatis查询出所有数据
    @RequestMapping("/list1")
    public List<Muser> getUserList1(){
        return muserMapper.getUserList();
    }
}

2.4 to manually configure mybatis scanning package, adding the master boot class @MapperScan

Package com.xhn; 

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

// all interfaces mapper and mapper mapping file in the package scanning 
@ MapperScan (basePackages = "com.xhn.mapper" )
 // add startup class 
@SpringBootApplication
 public  class the StartApplication {
     public  static  void main (String [] args) { 
        SpringApplication.run (the StartApplication. class , args); 
    } 
}

 2.5 server start springboot

 

 Complete the integration, thank you visit

Guess you like

Origin www.cnblogs.com/xinghaonan/p/11801490.html