Spring Boot配置mybatis+xml访问数据库代码样例

概述

spring boot搭配mybatis访问数据库是微服务常见配置。mybatis支持注解和xml配置两种方式。下面给一个xml配置的样例。

样例代码

代码目录结构

Spring Boot配置mybatis+xml访问数据库代码样例_第1张图片

在application.yml中配置数据库url

server:
  context-path: /elon/mybatisxml
  port: 10001

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/testdb?useUnicode=true&characterEncoding=utf-8
    username: root
    password:
    driver-class-name: com.mysql.jdbc.Driver

logging:
  config: classpath:log4j2.xml

配置数据源。在buildSqlSessionFactory中指定扫描的xml文件路径

package com.elon.datasource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * 配置数据源
 *
 * @author elon
 */
@Configuration
@MapperScan(basePackages = "com.elon.mapper", sqlSessionFactoryRef = "mySqlSessionFactory")
public class DataSourceConfig {

    @Bean(name = "myDataSource")
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource buildDataSource(){
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "mySqlSessionFactory")
    public SqlSessionFactory buildSqlSessionFactory(@Qualifier("myDataSource") DataSource dataSource,
                                                    org.apache.ibatis.session.Configuration config) throws Exception {
        SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
        factory.setDataSource(dataSource);
        factory.setConfiguration(config);
        factory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
        return factory.getObject();
    }

    @Bean
    @ConfigurationProperties("mybatis.configuration")
    public org.apache.ibatis.session.Configuration globalConfiguration() {
        return new org.apache.ibatis.session.Configuration();
    }

    @Bean(name = "myTransactionManager")
    public DataSourceTransactionManager buildTransactionManager(@Qualifier("myDataSource") DataSource dataSource){
        return new DataSourceTransactionManager(dataSource);
    }
}

定义IUserMapper测试接口

package com.elon.mapper;

import org.apache.ibatis.annotations.Mapper;

import java.util.List;
import java.util.Map;

@Mapper
public interface IUserMapper {

    /**
     * 查询用户信息
     * @return
     */
    List> queryUserInfo();
}

在UserMapper.xml中编写SQL





    

编写测试接口

package com.elon.rest;

import com.alibaba.fastjson.JSONObject;
import com.elon.mapper.IUserMapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

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

    @Resource
    private IUserMapper userMapper;

    @GetMapping("/alluser")
    public String queryUserInfo(){
        List> resultMap = userMapper.queryUserInfo();
        return JSONObject.toJSONString(resultMap);
    }
}

POSTMAN测试接口显示查询结果

Spring Boot配置mybatis+xml访问数据库代码样例_第2张图片

你可能感兴趣的:(Java,Spring,Boot,Java开发技术)