栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java-springBoot使用mybaties Demo

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java-springBoot使用mybaties Demo

首先创建springBoot项目,jdk选择1.8

然后倒入mybaties的相关依赖

我们用的springBoot,当然spring全家桶里面含有mybaties,所以我们直接使用升级版的mybaties-plus。

引入这3个

lombok省的我每次创建对象,都需要get、set方法,以及toString

(IDEA里面也要安装lombok插件-》file->setting->plugin->搜索lombok安装,完后重启idea,这样lombok在idea中不报错)

mybatis-plus,mybaties的相关jar

mysql-connector-java 基本的jdbc驱动链接mysql,mybatis也要依赖他

	
		1.8
		3.4.2
	

    
		com.baomidou
		mybatis-plus-boot-starter
		${mybatis-plus.version}
	
        
            mysql
            mysql-connector-java
            runtime
        

		
			org.projectlombok
			lombok
			1.18.4
			provided
		

然后像我这样,假设我们要搭建一个非常2的大工程,建立这几个目录和文件

controller->TestController.java(class)

mapper->TestMapper.java(interface)

service->impl->TestServiceImpl.java(class)

service->TestService.java(interface)

resources->mapper->TestMapper.xml(映射xml)

接下来我们需要一个SqlSessionFactory,去解析一个个sql.xml。

而sqlSessionFactory,由SqlSessionFactoryBuilder 构建,构建一个SqlSessionFactoryBuilder ,需要链接mysql的账号密码地址。所以要读取你的账号密码,那在resource目录下创建一个xml用于他去读取。

我在resources下的mapper里面创建一个mybaties-config.xml




    
        
            
            
                
                
                
                
            
        
    
    
        
    

创建一个domain包,用来与mysql做相关映射,我在里面创建了一个实体类,与数据库做映射

@Data的就是我第一次导入的那个包,能够自动通过@Data注解,生成这个实体类的get、set方法以及toString方法。

package com.example.demo.domain;

import lombok.Data;

@Data
public class BZGG {
    String req_date;
    String stock_code;
    String stock_name;
    double fNextIncrease;
    double fNextAvgIncrease;
    String updatetime;

    public BZGG(String req_date, String stock_code, String stock_name, double fNextIncrease, double fNextAvgIncrease, String updatetime) {
        this.req_date = req_date;
        this.stock_code = stock_code;
        this.stock_name = stock_name;
        this.fNextIncrease = fNextIncrease;
        this.fNextAvgIncrease = fNextAvgIncrease;
        this.updatetime = updatetime;
    }

    public BZGG() {
    }
}

在mapper中的TestMapper中编写一个地址,并且加上Mapper映射

package com.example.demo.mapper;

import com.example.demo.domain.BZGG;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface TestMapper {
    List getBZGGonTime(String req_date);

}

mybaties的代码流程图

 resources中的sql配置(TestMapper.xml)




    

controller层代码(你到时候可以自己分离service),为了突出mybaties暂时写到一起了

package com.example.demo.controller;



import com.example.demo.domain.BZGG;
import com.example.demo.mapper.TestMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;


@RestController
public class TestController {
    @RequestMapping(value = "/Test", method = RequestMethod.GET)
    @ResponseBody
    public String getDemo(@RequestParam("num") int i) {
        String resource = "mapper/mybatis-config.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        TestMapper mapper = sqlSession.getMapper(TestMapper.class);
        List bzgGonTime = mapper.getBZGGonTime("2022-04-08");

        return bzgGonTime.toString();
    }
}

另外SpringApplication启动类,因为导入了mybaties,所以要启动时加载某个类,否则会出现url找不到等错误。

将@springBootApplication改成下面的

@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class })
package com.example.demo;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;

//在启动类的@SpringBootApplication加上
//避免冲突,加入exclude
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class })

public class DemoApplication {

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


	}

}

测试:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/922298.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号