145. Spring Boot MyBatis Upgrade - XML

 

【Video & Communication Platform】

à SpringBoot Video 

http://study.163.com/course/introduction.htm?courseId=1004329008&utm_campaign=commission&utm_source=400000000155061&utm_medium=share

à  SpringCloud video

http://study.163.com/course/introduction.htm?courseId=1004638001&utm_campaign=commission&utm_source=400000000155061&utm_medium=share

à Spring Boot source code 

https://gitee.com/happyangellxq520/spring-boot

à Spring Boot communication platform 

http://412887952-qq-com.iteye.com/blog/2321532

 

 

Origin of need:

In a video, there is such a message: "Will there be a video of SpringBoot integrating the Mybaits configuration file sqlMapConfig.xml with mapper.xml? Seeing this integration of direct payment , the result is a rapid development mode , sql is written in the class, and the great god who wants to see the configuration method should come up with one." Fans demand, that is the real demand. Well, too much nonsense, not good, not good, let's get to the point quickly.

Outline of this section:

(1) MyBatis introduction
(2) Configuration ideas
(3) New project and adding dependency packages
(4) Create startup class App.java
(5) Write entity class Demo
(6) Write application configuration file and specify xml path
(7) Write Mapping interface DemoMapper and XML
(8) Write service class DemoService
(9) Write control class DemoController
(10) Test

 

       Let's take a look at the content of this section:

 

( 1 ) MyBatis introduction

       来源MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 201311月迁移到Github

       介绍MyBatis 是支持普通 SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis 消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索。MyBatis 使用简单的 XML或注解用于配置和原始映射,将接口和 Java POJOsPlain Ordinary Java Objects,普通的 Java对象)映射成数据库中的记录。

 

2)配置思路

       Spring Boot中使用xml集成MyBatis的话,那么核心的文件就是实体类和SQL的映射类,比如DemoMapper,在此类当中就是普通的接口即可,那么对应SQL配置文件在Demo.xml中,那么要怎么能够识别到DemoMapper类呢,使用@MapperScan();在Demo.xml中使用<mapper> namespace属性进行指定指定xml文件和mapper的对应关系,那么现在的问题就是如何识别到Demo.xml配置文件呢,这个就很简单了,在application.properties文件中配置mapper的位置即可,形如:mybatis.mapper-locations=classpath:mybatis/mapper/*.xml

       根据以上的思路,那我们编码大概的思路就是:

(a)编写实体类Demo;
(b)编写配置文件Demo.xml,主要是SQL;
(c)编写DemoMapper和Demo是对应的,在Service层就可以调用DemoMapper;
(d)在application.properties文件中配置Demo.xml文件的路径;

 

 

3)新建project以及添加依赖包

       新建project,取名为spring-boot-mybatis-xml,在pom.xml文件添加相关依赖包:

 

<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>
 
  <groupId>com.kfit</groupId>
  <artifactId>spring-boot-mybatis-xml</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
 
  <name>spring-boot-mybatis-xml</name>
  <url>http://maven.apache.org</url>
 
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
 
    <!-- spring boot parent节点,引入这个之后,在下面和spring boot相关的就不需要引入版本了; -->
    <parent>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-parent</artifactId>
       <version>1.4.1.RELEASE</version>
    </parent>
 
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <scope>test</scope>
    </dependency>
   
    <!-- web支持: 1、web mvc; 2、restful; 3、jackjson支持; 4、aop ........ -->
    <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
   
    <!-- mysql 数据库驱动. -->
    <dependency>
           <groupId>mysql</groupId>
           <artifactId>mysql-connector-java</artifactId>
    </dependency>
   
       <!-- spring-boot mybatis依赖:
       请不要使用1.0.0版本,因为还不支持拦截器插件,
     -->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.3.0</version>
    </dependency>
   
  </dependencies>
</project>
 

 

 

4)创建启动类App.java

编写启动类,注意,添加了@MapperScan("com.kfit.*.mapper"),扫描mapper接口:

 

package com.kfit;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
/**
 *
 * @author Angel --守护天使
 * @version v.0.1
 * @date 2017年8月4日
 */
@SpringBootApplication
@MapperScan("com.kfit.*.mapper")
public class App {
    public static void main(String[] args) {
       SpringApplication.run(App.class, args);
    }
}
 

 

 

5)编写实体类Demo

编写Demo类,主要属性id,name

 

package com.kfit.demo.bean;
 
public class Demo {
    private int id;
    private String name;
    public int getId() {
       return id;
    }
    public void setId(intid) {
       this.id = id;
    }
    public String getName() {
       return name;
    }
    public void setName(String name) {
       this.name = name;
    }
}
 

 

 

6)编写application配置文件,指定xml路径

########################################################

###datasource -- mysql的数据库配置.

########################################################

spring.datasource.url = jdbc:mysql://localhost:3306/test

spring.datasource.username = root

spring.datasource.password = root

spring.datasource.driverClassName = com.mysql.jdbc.Driver

spring.datasource.max-active=20

spring.datasource.max-idle=8

spring.datasource.min-idle=8

spring.datasource.initial-size=10

 

 

########################################################

###mybatis配置.

########################################################

mybatis.mapper-locations=classpath:com/kfit/*/mapper/*.xml

 

在这里重要的配置是:mybatis.mapper-locations=classpath:com/kfit/*/mapper/*.xml

 

7)编写映射接口DemoMapperxml文件

编写XML文件,Demo.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.kfit.demo.mapper.DemoMapper">
   
    <!--  insert 语句. -->
    <insert id="save" parameterType="com.kfit.demo.bean.Demo" useGeneratedKeys="true" keyProperty="id">
       insert into demo (name) values (#{name})
    </insert>
   
</mapper>
 

 

 

编写DemoMapper接口:

 

package com.kfit.demo.mapper;
 
public interface DemoMapper {
    public void save(Demo demo);
}
 

 

 

 

8)编写serviceDemoService

 

package com.kfit.demo.service;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import com.kfit.demo.bean.Demo;
import com.kfit.demo.mapper.DemoMapper;
 
@Service
public class DemoService {
   
    @Autowired
    private DemoMapper demoMapper;
   
@Transactional
    public void save(Demo demo){
       demoMapper.save(demo);
    }
   
}
 

 

 

9)编写控制类DemoController

 

package com.kfit.demo.controller;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import com.kfit.demo.bean.Demo;
import com.kfit.demo.service.DemoService;
 
@RestController
public class DemoController {
    @Autowired
    private DemoService  demoService;
   
    // http://127.0.0.1:8080/save
    @RequestMapping("/save")
    public Demo save(){
       Demo demo = new Demo();
       demo.setName("张三");
       demoService.save(demo);
       return demo;
    }
}
 

 

 

10)测试

启动,访问地址:http://127.0.0.1:8080/save

在浏览器可以看到:

{

id0,

name"张三"

}

 

视频&交流平台

à SpringBoot网易云课堂视频

http://study.163.com/course/introduction.htm?courseId=1004329008

à Spring Boot交流平台

http://412887952-qq-com.iteye.com/blog/2321532

 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326858974&siteId=291194637