zoukankan      html  css  js  c++  java
  • SpringBoot整合MyBatis【数据库连接】

    1、pom文件引入

    <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.0.RELEASE</version>
        </parent>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
            <!-- 测试 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>1.1.1</version>
            </dependency>
            <!-- mysql 依赖 -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
            <!-- springboot-web组件 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
        </dependencies>

    2、application.properties配置文件

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

    3、配置DataSourceConfig ;

    
    
    package com.yuhuiqing.config;

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

    import javax.sql.DataSource;


    @Configuration
    @MapperScan(basePackages = "com.yuhuiqing.mapper", sqlSessionFactoryRef = "SqlSessionFactory")
    public class DataSourceConfig {

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

    @Bean(name = "SqlSessionFactory")
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("DataSource") DataSource dataSource)
    throws Exception {
    SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    bean.setDataSource(dataSource);
    bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
    return bean.getObject();
    }
    @Bean(name = "TransactionManager")
    public DataSourceTransactionManager testTransactionManager(@Qualifier("DataSource") DataSource dataSource) {
    return new DataSourceTransactionManager(dataSource);
    }
    @Bean(name = "SqlSessionTemplate")
    public SqlSessionTemplate testSqlSessionTemplate(
    @Qualifier("SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
    return new SqlSessionTemplate(sqlSessionFactory);
    }


    }
     

    4、创建对应的Mapper层service层就可以直接连接使用了!

    <?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.yuhuiqing.mapper.CountManageMapper" >
        <select id="getInfo" parameterType="com.yuhuiqing.entities.CountManage" resultType="com.yuhuiqing.entities.CountManage">
            SELECT
            ID id,
            HQ_TITLE hqTitle,
            HQ_COUNT hqCount,
            HQ_PASSWORD hqPassword,
            HQ_LINKADD hqLinkadd
            FROM COUNT_MANAGE
            WHERE ISDELETE='N'
            <if test="hqTitle != null and hqTitle != ''">
                and HQ_TITLE like concat('%',#{hqTitle},'%')
            </if>
        </select>
        <insert id="add" parameterType="com.yuhuiqing.entities.CountManage" >
      insert into COUNT_MANAGE(HQ_TITLE,
        HQ_COUNT ,
        HQ_PASSWORD ,
        HQ_LINKADD,
        HQ_REMARK)
        values (#{hqTitle},#{hqCount},#{hqPassword},#{hqLinkadd},#{hqRemark})
    </insert>
        <update id="delete" parameterType="com.yuhuiqing.entities.CountManage" >
      update COUNT_MANAGE set ISDELETE='Y' where ID=#{id}
    </update>
        <update id="update" parameterType="com.yuhuiqing.entities.CountManage" >
            update COUNT_MANAGE set ID=#{id}
            <if test="hqTitle != null and hqTitle != ''">
                , HQ_TITLE = #{hqTitle}
            </if>
            <if test="hqCount != null and hqCount != ''">
                , HQ_COUNT = #{hqCount}
            </if>
            <if test="hqPassword != null and hqPassword != ''">
                , HQ_PASSWORD = #{hqPassword}
            </if>
            <if test="hqLinkadd != null and hqLinkadd != ''">
                , HQ_LINKADD = #{hqLinkadd}
            </if>
            <if test="hqRemark != null and hqRemark != ''">
                , HQ_REMARK = #{hqRemark}
            </if>
            where ID=#{id}
        </update>
    </mapper>
    

      

  • 相关阅读:
    mysql 和navicat for mysql 安装教程
    Linux 下配置php开发环境
    RecyclerView的使用(四)
    RecyclerView的使用(三)
    RecyclerView的使用(二)
    RecyclerView的使用(一)
    适配器(一)
    ListView简介
    Android ListView 详解
    android SwipeRefreshLayout google官方下拉刷新控件
  • 原文地址:https://www.cnblogs.com/yuhuiqing/p/10701515.html
Copyright © 2011-2022 走看看