springboot项目整合mybatis基础使用

在web应用常用的持久层框架有hibernate与mybatis,两个框架都有各自的优缺点,mybatis属于轻量级框架,在互联网行业常使用mybatis作为持久层框架,在springboot项目中引入mybatis-spring-boot-starter可以实现与springboot无缝衔接,降低了框架配置,上手更容易

1.引入maven依赖,springboot web模块、MySQL-jdbc驱动、mybatis-start


<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
     
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.1.6.RELEASEversion>
        <relativePath/>
    parent>
    <groupId>com.beauhougroupId>
    <artifactId>mybatis_springartifactId>
    <version>1.0-SNAPSHOTversion>

    <dependencies>
     	
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
        dependency>
         
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>2.1.2version>
        dependency>
    dependencies>

    <build>
     
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <configuration>
                    <fork>truefork>
                configuration>
            plugin>
        plugins>
    build>
project>

2.配置yml文件(PS:由于引入了mybatis-spring-boot-start,其配置会在IOC容器中注入DataSource,该操作可从源码中获取,在yml配置文件中,需配置好DataSource属性,否则将无法启动)

server:
  servlet:
    context-path: /mybatis_spring
  port: 8081
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver #com.mysql.jdbc.Driver 8.0之后驱动依赖已过时
    #mysql8.0之后必须指定时区,否则连接会报错
    url: jdbc:mysql://localhost:3306/cv?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    password: root
mybatis:
  #指定mapper文件位置
  mapper-locations: classpath:mybatis/mapper/*.xml

3.控制器

/**
 * @author beauhou
 * @version 1.0
 * @date 2020/8/17 0017 22:05
 */
@RestController
@RequestMapping("test")
public class TestController {
     
    @Autowired
    private TestService service;

    @GetMapping("queryAll")
    public Object queryAll(){
     
        Object list = service.queryAllList();
        return list ;
    }
}

4.service服务

/**
 * @author beauhou
 * @version 1.0
 * @date 2020/8/17 0017 22:06
 */
@Service
public class TestService {
     

    @Autowired
    private UserMapper userMapper;

    public Object queryAllList(){
     
        List<User> users = userMapper.queryAll();
        return users;
    }
}

5.持久层mapper

/**
 * @author beauhou
 * @version 1.0
 * @date 2020/8/17 0017 22:08
 */
//mapper接口必须引用此注解,注入的IOC容器中
@Repository
public interface UserMapper {
        
    List<User> queryAll();
}

6.编写po实体对象


public class User {
     

    private Integer  id;

    private String username;

    private String password;

    public Integer getId() {
     
        return id;
    }

    public void setId(Integer id) {
     
        this.id = id;
    }

    public String getUsername() {
     
        return username;
    }

    public void setUsername(String username) {
     
        this.username = username;
    }

    public String getPassword() {
     
        return password;
    }

    public void setPassword(String password) {
     
        this.password = password;
    }
}

7.mapper.xml文件,




<mapper namespace="com.beauhou.mapper.UserMapper" >

    <resultMap id="BaseResultMap" type="com.beauhou.po.User" >
        <id column="id" property="id" jdbcType="BIGINT" />
        <result column="username" property="username"  />
        <result column="password" property="password" />
    resultMap>
    <sql id="userVo" >
       select * from t_user
    sql>
    
    <select id="queryAll" resultMap="BaseResultMap"  >
       select id,username,password from t_user
    select>
mapper>

8.文件目录结构
springboot项目整合mybatis基础使用_第1张图片

9.启动类

/**
 * @author beauhou
 * @version 1.0
 * @date 2020/8/17 0017 22:04
 */
@SpringBootApplication
//此注解非常重要,会将mapper接口扫描注册到IOC容器中
//故此接口可以在service层直接注入
@MapperScan("com.beauhou.mapper")
public class StartApplication {
     
    public static void main(String[] args) {
     
        SpringApplication.run(StartApplication.class,args);
    }
}

本篇只讲解了mybatis在springboot中基础应用,mybatis详细配置可以可以参照官方文档说明,由于mybatis每一个接口都会对应一个mapper文件,虽然有插件可以生成基础的sql操作,但是对于数据库的移植不是特别方便,于是有了针对mybatis优化的版本 ,mybatis-plus,地址:https://baomidou.com/,有兴趣的朋友可以去学习一下

你可能感兴趣的:(java,java,spring,mybatis)