日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

SpringBoot整合Mybatis演示

作者:一眉程序猿 更新時(shí)間: 2022-05-20 編程語(yǔ)言

SpringBoot整合Mybatis演示

1.環(huán)境準(zhǔn)備

  • JDK 1.8
  • MySQL 5.7
  • Maven 3.6.3
  • Idea 2020.1.1

數(shù)據(jù)庫(kù)模擬數(shù)據(jù)準(zhǔn)備:

CREATE DATABASE `springboot01`;
USE `springboot01`;

CREATE TABLE `table_emp` (
  `emp_id` int(11) NOT NULL AUTO_INCREMENT,
  `emp_name` varchar(50) NOT NULL,
  `emp_age` int(11) NOT NULL,
  `emp_birthday` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`emp_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

insert  into `table_emp`(`emp_name`,`emp_age`,`emp_birthday`) values ('ara',20,'2020-07-28 23:21:38'),('胡',20,'2020-07-28 23:21:38');

2.搭建SpringBoot項(xiàng)目

使用Idea創(chuàng)建一個(gè)SpringBoot的空項(xiàng)目,添加依賴如下:


<dependency>
   <groupId>mysqlgroupId>
   <artifactId>mysql-connector-javaartifactId>
dependency>


<dependency>
   <groupId>org.mybatis.spring.bootgroupId>
   <artifactId>mybatis-spring-boot-starterartifactId>
   <version>2.1.3version>
dependency>


<dependency>
   <groupId>org.projectlombokgroupId>
   <artifactId>lombokartifactId>
dependency>

在pojo包下創(chuàng)建與數(shù)據(jù)庫(kù)匹配的實(shí)體類

package com.ara.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;

/**
 * 測(cè)試員工類
 * @author Ara
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Emp {

    private int empId;
    private String empName;
    private int empAge;
    private Date empBirthday;

}

配置SpringBoot的配置文件(application.yaml),這里配置數(shù)據(jù)源的連接配置和Mybatis的配置,如下:

spring:
  # 數(shù)據(jù)源的配置
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/springboot01?serverTimezone=GMT%2B8
    username: root
    password: 123456
mybatis:
  # 別名包
  type-aliases-package: com.ara.pojo
  # mapper.xml文件地址
  mapper-locations: classpath:mybatis/mapper/*.xml
  configuration:
    # 開啟駝峰命名轉(zhuǎn)換
    map-underscore-to-camel-case: true

在主啟動(dòng)類的同級(jí)目錄下創(chuàng)建mapper包,然后在里面創(chuàng)建EmpMapper接口如下:

package com.ara.mapper;

import com.ara.pojo.Emp;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Mybatis數(shù)據(jù)庫(kù)操作測(cè)試
 *
 * @author Ara
 */
@Mapper
@Repository
public interface EmpMapper {

    /**
     * 查詢所有的員工信息
     * @return 員工信息列表
     */
    List<Emp> getAllEmp();

}

然后在resources目錄下創(chuàng)建mybatis文件夾,并在其中創(chuàng)建mapper文件夾,然后在mapper文件夾中編寫EmpMapper.xml文件(注意這里的路徑要和配置文件中mapper.xml文件地址一致),如下:



<mapper namespace="com.ara.mapper.EmpMapper">
    <select id="getAllEmp" resultType="Emp">
        select * from table_emp;
    select>
mapper>

到此,查詢的方法就算是寫完了,然后我們進(jìn)行測(cè)試,在測(cè)試啟動(dòng)類中添加如下:

@Autowired
private EmpMapper empMapper;

@Test
void contextLoads() {

    List<Emp> empList = empMapper.getAllEmp();
    for (Emp emp : empList) {
        System.out.println(emp);
    }

}

測(cè)試結(jié)果如下:
在這里插入圖片描述
表示我們的配置和代碼編寫都沒有問題。

3.補(bǔ)充增刪改

上面我們對(duì)數(shù)據(jù)庫(kù)進(jìn)行了查詢操作,接下來(lái)我們就完成簡(jiǎn)單的增刪改功能。

在EmpMapper接口中添加如下方法:

/**
 * 插入數(shù)據(jù)
 * @param emp 待插入數(shù)據(jù)
 * @return 受影響行數(shù)
 */
int insertEmp(Emp emp);

EmpMapper.xml:

<insert id="insertEmp" parameterType="Emp">
    insert into table_emp (emp_name,emp_age,emp_birthday)
    values (#{empName},#{empAge},#{empBirthday});
insert>

測(cè)試代碼:

@Test
void insertTest(){

    Emp insertEmp = new Emp();
    insertEmp.setEmpName("admin");
    insertEmp.setEmpAge(18);
    insertEmp.setEmpBirthday(new Date());

    empMapper.insertEmp(insertEmp);

    //查詢驗(yàn)證
    List<Emp> empList = empMapper.getAllEmp();
    for (Emp emp : empList) {
        System.out.println(emp);
    }

}

測(cè)試結(jié)果:
在這里插入圖片描述

刪除

EmpMapper接口:

/**
 * 刪除數(shù)據(jù)
 * @param id 待刪除數(shù)據(jù)的Id
 * @return 受影響的行數(shù)
 */
int deleteEmpById(int id);

EmpMapper.xml:

<delete id="deleteEmpById" parameterType="int">
    delete from table_emp where emp_id = #{id};
delete>

測(cè)試代碼:

@Test
void deleteTest(){

    empMapper.deleteEmpById(3);

    //查詢驗(yàn)證
    List<Emp> empList = empMapper.getAllEmp();
    for (Emp emp : empList) {
        System.out.println(emp);
    }
}

測(cè)試結(jié)果:
在這里插入圖片描述

修改

EmpMapper接口:

/**
 * 修改數(shù)據(jù)
 * @param emp 待修改數(shù)據(jù)
 * @return 受影響行數(shù)
 */
int updateEmpById(Emp emp);

EmpMapper.xml:

<update id="updateEmpById" parameterType="Emp">
    update table_emp set
    emp_name =  #{empName},
    emp_age = #{empAge},
    emp_birthday = #{empBirthday}
    where
    emp_id = #{empId};
update>

測(cè)試代碼:

@Test
void updateTest(){

    Emp updateEmp = new Emp();
    updateEmp.setEmpId(2);
    updateEmp.setEmpName("admin");
    updateEmp.setEmpAge(18);
    updateEmp.setEmpBirthday(new Date());

    empMapper.updateEmpById(updateEmp);

    //查詢驗(yàn)證
    List<Emp> empList = empMapper.getAllEmp();
    for (Emp emp : empList) {
        System.out.println(emp);
    }

}

測(cè)試結(jié)果:
在這里插入圖片描述
到此就表明我們簡(jiǎn)單的數(shù)據(jù)庫(kù)操作就已經(jīng)成功了。

4.補(bǔ)充說(shuō)明

上面我們編寫的測(cè)試,重心基本都在業(yè)務(wù)邏輯上,我們做的很少,就是因?yàn)镾pringBoot幫我們都做了,比如數(shù)據(jù)庫(kù)連接,甚至我們還發(fā)現(xiàn)它還有自己默認(rèn)的數(shù)據(jù)庫(kù)連接池(Hikari),剛剛我們并未顯式的指定數(shù)據(jù)庫(kù)連接池,所以就使用了默認(rèn)的配置,如果我們需要自定義其他的數(shù)據(jù)庫(kù)連接池類型,我們只需要在配置中指定即可。

舉個(gè)例子:如果我們?cè)谏厦娴幕A(chǔ)上,將數(shù)據(jù)庫(kù)連接池類型換成阿里巴巴的druid,我們只需要在pom文件中導(dǎo)入druid的依賴,然后將數(shù)據(jù)源的類型指定為druid即可:

<dependency>
    <groupId>com.alibabagroupId>
    <artifactId>druidartifactId>
    <version>1.1.21version>
dependency>

在這里插入圖片描述
這樣就成功的更換了數(shù)據(jù)庫(kù)的連接池類型。

如果需要更加詳細(xì)的日志打印在控制臺(tái)上,比如上面的例子,我們想要查看運(yùn)行的Sql語(yǔ)句,我們只需要在配置文件中添加一個(gè)配置即可:

logging:
  level:
    # 對(duì)應(yīng)的包:日志級(jí)別
    com.ara.mapper: debug

這樣就設(shè)置了打印com.ara.mapper包下級(jí)別為debug的日志。

原文鏈接:https://blog.csdn.net/weixin_45935633/article/details/107696945

欄目分類
最近更新