SpringBoot学习笔记——mybatis自动配置

本文详细介绍了如何在SpringBoot项目中配置并使用Mybatis,包括创建工程、添加依赖、定义Bean、创建Mapper接口和文件、配置数据源以及编写测试用例。通过@MapperScan注解实现接口扫描,确保Mybatis正常工作。
摘要由CSDN通过智能技术生成

1. 创建工程

首先,创建一个mybatis的工程:

2. 依赖

引入相关依赖:

<dependencies>
		<!--springboot整合mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.1</version>
        </dependency>
		<!-- mysql驱动 -->
		<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!-- alibaba的druid数据库连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.11</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.20</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.15</version>
        </dependency>
    </dependencies>

3. 创建Bean

创建一个person类:

package cn.tx.sboot.model;

import lombok.Data;
import java.util.Date;
@Data
public class Person {
    private int pid;
    private String pname;
    private String addr;
    private int gender;
    private Date birth;
}

4. 创建mapper文件

  1. 创建一个mapper接口:
package cn.tx.sboot.mapper;

import cn.tx.sboot.model.Person;
import java.util.List;

public interface PersonMapper {
    public Person selectById(int pid);
    public List<Person> selectAll();
    public void insert(Person p);
    public void delete(int pid);
}

关于接口扫描,有两种方式:一种是在dao接口 加上@Mapper 或者在主启动类上 加@MapperScan(“cn.tx.sboot.mapper”),推荐选用@mapperscan的方式。

  1. 创建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="cn.tx.sboot.mapper.PersonMapper">
    <select id="selectById" resultType="Person">
        select * from person t where t.pid = #{pid}
    </select>
    <select id="selectAll" resultType="Person">
        select * from person
    </select>
    <insert id="insert" parameterType="person">
        <selectKey keyProperty="pid" resultType="int" order="BEFORE">
            select last_insert_id()
        </selectKey>
        insert into person(pid,pname,addr,gender,birth)values(#{pid},#{pname},#{addr},#{gender},#{birth})
    </insert>
    <delete id="delete" parameterType="int" >
        delete from person where pid = #{pid}
    </delete>
</mapper>

5. 创建配置文件

创建一个application.yml配置文件;

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/personuseUnicode=true&useJDBCCompliantTimezoneShift=true&uselegacyDatetimeCode=false&serverTimezone=UTC
    dtiver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 123456
    type: com.alibaba.druid.pool.DruidDataSource
    initialSize: 5
    minIdle: 5
    maxActive: 20
    maxwait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedstatements: true
    filters: stat,wall,1og4j
    maxPoolPreparedstatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql-true;druid.stat.slowSqlMillis=500
mybatis:
  mapper-locations: classpath:mapper/*.xml		#设置扫描所有的mapper映射文件
  configuration:
    map-underscore-to-camel-case: true		#设置驼峰模式和下划线模式的映射
  type-aliases-package: cn.tx.sboot.model		#设置别名

创建一个DataSource配置类:

package cn.tx.sboot.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DatasourceConfig {
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DruidDataSource dataSource(){ return new DruidDataSource();}
}

6. 创建测试

基本配置都写好了,就可以创建类进行测试。需要下面几个类:

  1. testcontroller
package cn.tx.sboot.controller;

import cn.tx.sboot.mapper.PersonMapper;
import cn.tx.sboot.model.Person;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
    @Autowired
    private PersonMapper personMapper;
    @RequestMapping("selectById")
    public Person selectById(){
       return personMapper.selectById(1);
    }
}
  1. 创建启动类:
package cn.tx.sboot;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("cn.tx.sboot.mapper")
public class FirstSpringApplication {
    public static void main(String[] args){
        SpringApplication.run(FirstSpringApplication.class,args);
    }
}

  1. 测试:
    在这里插入图片描述
    成功。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值