spring boot整合mybatis(注解版)

spring boot整合mybatis很方便,直接在pom.xml引入如下坐标:

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

上面多余的坐标是用来配合spring boot开发的,我用的是spring JDBC的数据源。
给出配置文件application.yml文件:

spring:
  datasource:
    username: root
    password: root
    url: jdbc:mysql://localhost:3306/lm?serverTimezone=UTC
    driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
  configuration:
#    开启mybatis的驼峰命名支持
    map-underscore-to-camel-case: true
# 打印sql
logging:
  level:
    com.lm.springboot_mybatis.dao : trace

建立两个新包,如下目录结构所示:
在这里插入图片描述
给出User数据库表的DDL语句:

-- auto-generated definition
create table user
(
    user_id         int auto_increment comment '自增id'
        primary key,
    username        varchar(128)                                    default ''   not null comment '登录名',
    qq_open_id      char(32)                                                     null comment 'qq官方唯一编号信息',
    password        char(64)                                        default ''   not null comment '登录密码',
    user_email      varchar(64)                                     default ''   not null comment '邮箱',
    user_email_code char(13)                                                     null comment '新用户注册邮件激活唯一校验码',
    is_active       enum ('是', '否')                                 default '否'  null comment '新用户是否已经通过邮箱激活帐号',
    user_sex        enum ('保密', '女', '男')                           default '男'  not null comment '性别',
    user_qq         varchar(32)                                     default ''   not null comment 'qq',
    user_tel        varchar(32)                                     default ''   not null comment '手机',
    user_xueli      enum ('博士', '硕士', '本科', '专科', '高中', '初中', '小学') default '本科' not null comment '学历',
    user_hobby      varchar(32)                                     default ''   not null comment '爱好',
    user_introduce  text                                                         null comment '简介',
    create_time     int                                                          not null comment '创建时间',
    update_time     int                                                          not null comment '修改时间'
)
    comment '会员表' charset = utf8;

给出UserDao.java

package com.lm.springboot_mybatis.dao;

import com.lm.springboot_mybatis.domain.User;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface UserDao {
    /**
     * 找出所有用户
     * @return
     */
    @Select("select * from user")
    public List<User> findAll();
}

给出实体类User.java

package com.lm.springboot_mybatis.domain;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;

/**
 * @author  liangmu
 * @date 2020-12-09 
 */

@Data
@NoArgsConstructor
@AllArgsConstructor
public class User  implements Serializable {

	private static final long serialVersionUID =  7258016961909004084L;

	/**
	 * 自增id
	 */
	private Long userId;

	/**
	 * 登录名
	 */
	private String username;

	/**
	 * qq官方唯一编号信息
	 */
	private String qqOpenId;

	/**
	 * 登录密码
	 */
	private String password;

	/**
	 * 邮箱
	 */
	private String userEmail;

	/**
	 * 新用户注册邮件激活唯一校验码
	 */
	private String userEmailCode;

	/**
	 * 新用户是否已经通过邮箱激活帐号
	 */
	private String isActive;

	/**
	 * 性别
	 */
	private String userSex;

	/**
	 * qq
	 */
	private String userQq;

	/**
	 * 手机
	 */
	private String userTel;

	/**
	 * 学历
	 */
	private String userXueli;

	/**
	 * 爱好
	 */
	private String userHobby;

	/**
	 * 简介
	 */
	private String userIntroduce;

	/**
	 * 创建时间
	 */
	private Long createTime;

	/**
	 * 修改时间
	 */
	private Long updateTime;

}

给出启动文件application.java

package com.lm.springboot_mybatis;

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

@MapperScan("com.lm.springboot_mybatis.dao")
@SpringBootApplication
public class SpringbootMybatisApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootMybatisApplication.class, args);
    }

}

注意:@MapperScan(“com.lm.springboot_mybatis.dao”)这个注解是必须加,表示mapper所在路径,同时在dao层中的类,必须加上@repository注解,表示此类是一个持久层操作类。


结果:

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值