欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

spring boot整合mybatis方式一

程序员文章站 2022-07-11 10:53:43
方式一: 导入maven依赖: 配置mybatis核心配置文件(主要配置别名与映射xml文件): 配置xml文件,与mapper层一一对应: 再之后就是配置实体类与mapper层接口,此处省略。。。 springboot整合junit测试(两个注解): 最后附一份GitHub项目仓库地址: http ......

方式一:

导入maven依赖:  

<!--web依赖配置-->
<dependency>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter-web</artifactid>
</dependency>
<!--连接池-->
<dependency>
    <groupid>com.alibaba</groupid>
    <artifactid>druid</artifactid>
    <version>1.1.12</version>
</dependency>
<!--mysql-->
<dependency>
    <groupid>mysql</groupid>
    <artifactid>mysql-connector-java</artifactid>
</dependency>
<!--引入mybatis-->
<dependency>
    <groupid>org.mybatis.spring.boot</groupid>
    <artifactid>mybatis-spring-boot-starter</artifactid>
    <version>1.3.2</version>
</dependency>
<!--工具类-->
<dependency>
    <groupid>org.projectlombok</groupid>
    <artifactid>lombok</artifactid>
</dependency>
<!--guawa工具类-->
<dependency>
    <groupid>com.google.guava</groupid>
    <artifactid>guava</artifactid>
    <version>27.0.1-jre</version>
</dependency>


配置application.properties:
#druid
spring.druid.url=jdbc:mysql:///mysql04?useunicode=true&characterencoding=utf8&servertimezone=gmt
spring.druid.driverclassname=com.mysql.jdbc.driver
spring.druid.username=root
spring.druid.password=root
#最大链接数
spring.druid.maxactive=30
#最小链接数
spring.druid.minidle=5
#获得链接的最大等待时间
spring.druid.maxwait=10000

#指定mybatis配置文件地址
mybatis.config-location=classpath:mybatis/mybatis-config.xml

#端口
server.port=8888


配置druidconfig配置类(包含sql检测:显示慢sql):

@configuration
public class druidconfig {


    @bean
    @configurationproperties(prefix = "spring.druid")
    public druiddatasource druiddatasource(){
        druiddatasource datasource = new druiddatasource();
        datasource.setproxyfilters(lists.newarraylist(statfilter()));
        return datasource;
    }
    @bean
    public statfilter statfilter(){
        statfilter filter = new statfilter();
        filter.setlogslowsql(true);
        filter.setmergesql(true);
        filter.setslowsqlmillis(5);
        return filter;
    }

    @bean
    public servletregistrationbean servletregistrationbean(){
        return new servletregistrationbean(new statviewservlet(),"/druid/*");
    }

}

配置mybatis核心配置文件(主要配置别名与映射xml文件):

<?xml version="1.0" encoding="utf-8" ?>
<!doctype configuration
        public "-//mybatis.org//dtd config 3.0//en"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <settings>
        <!--关闭缓存,多机环境 数据更新 不会刷新缓存-->
        <setting name="cacheenabled" value="false"/>
        <!--开启驼峰匹配-->
        <setting name="mapunderscoretocamelcase" value="true"/>
        <!--允许主键生成-->
        <setting name="usegeneratedkeys" value="true"/>
        <!--配置默认的执行器。simple 就是普通的执行器;
        reuse 执行器会重用预处理语句(prepared statements);
        batch 执行器将重用语句并执行批量更新。
    请自行百度,这部分内容不会详细介绍,涉及到架构设计,跟我们暂时还不沾边
    https://blog.csdn.net/qingtian211/article/details/81838042
        -->
        <setting name="defaultexecutortype" value="reuse"/>
        <setting name="defaultstatementtimeout" value="600"/>
    </settings>


    <typealiases>
        <package name="com.springbootmybatis.procedure.entity"/>
    </typealiases>

    <mappers>
        <mapper resource="mapper/ordermapper.xml"/>
    </mappers>
</configuration>

 

配置xml文件,与mapper层一一对应:

<?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.springbootmybatis.procedure.mapper.ordermapper">
    <update id="cancelorder" parametertype="string" statementtype="callable">
        call cancel_order(#{orderid,jdbctype=varchar,mode=in})
    </update>
    <select id="findall" resulttype="order">
        select * from xmcc_order
    </select>
</mapper>

 

再之后就是配置实体类与mapper层接口,此处省略。。。

 

springboot整合junit测试(两个注解):

@runwith(springrunner.class)
@springboottest
public class procedureapplicationtests {

    @autowired
    private ordermapper ordermapper;

    @test
    public void contextloads() {
        list<order> orders = ordermapper.findall();
        orders.stream().foreach(system.out::println);
    }
}

 

最后附一份github项目仓库地址:

https://github.com/ycheneye/procedure