欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

一小時(shí)迅速入門(mén)Mybatis之增刪查改篇

 更新時(shí)間:2021年09月14日 16:35:32   作者:grace.free  
這篇文章主要介紹了迅速入門(mén)Mybatis之增刪查改篇,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

一、說(shuō)明

這二篇涉及到映射Java實(shí)體類(lèi)、面向接口編寫(xiě)Mybatis、增刪查改示例

怎么引入jar包,怎么配置數(shù)據(jù)庫(kù)看上一篇哦~

二、開(kāi)搞

2.1 數(shù)據(jù)庫(kù)表

上一篇好像丟了數(shù)據(jù)庫(kù)創(chuàng)建語(yǔ)句

-- 主鍵自增
DROP TABLE IF EXISTS `test`;
CREATE TABLE `test`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT, 
  `name` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `salary` decimal(10, 2) NOT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- 插入測(cè)試數(shù)據(jù)
-- ----------------------------
INSERT INTO `test` VALUES (1, '小明', 30000.00);

2.1 創(chuàng)建實(shí)體類(lèi)

package entity;

import java.math.BigDecimal;

/**
 * 一個(gè)生活在互聯(lián)網(wǎng)底層,做著增刪改查的碼農(nóng),不諳世事的造作
 * @create 2021-08-25 22:05
 */
public class TestEntity {
    private  Long id;
    private String name;
    private BigDecimal salary;

    public Long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public BigDecimal getSalary() {
        return salary;
    }

    public void setSalary(BigDecimal salary) {
        this.salary = salary;
    }

    @Override
    public String toString() {
        return "TestEntity{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", salary=" + salary +
                '}';
    }
}

2.2 創(chuàng)建接口

package dao;

import entity.TestEntity;

import java.util.List;

/**
 * 一個(gè)生活在互聯(lián)網(wǎng)底層做著增刪改查的碼農(nóng)的感悟與學(xué)習(xí)
 * @create 2021-08-25 22:07
 */
public interface TestMapper {
    // 新增
    void save(TestEntity testEntity);

    // 修改
    void update(TestEntity testEntity);

    // 刪除 這里就一個(gè)參數(shù) 所以不用@Param 也不用Map 自定義實(shí)體類(lèi)等
    void delete(Long id);

    // 根據(jù)主鍵查詢(xún)
    TestEntity get(Long id);

    // 查詢(xún)所有數(shù)據(jù)
    List<TestEntity> list();

    // 根據(jù)名稱(chēng)模糊查詢(xún)
    List<TestEntity> listByNameLike(String name);
}

2.3 創(chuàng)建XML

mybatis-config.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>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=false&amp;serverTimezone=GMT%2B8"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="TestMapper.xml"/>
    </mappers>
</configuration>

TestMapper.xml

<?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="dao.TestMapper">
    <!--增加-->
    <insert id="save" >
        INSERT INTO `test`( `name`, `salary`) VALUE (#{name}, #{salary});
    </insert>

    <!--刪除-->
    <delete id="delete">
        delete from test where id = #{id}
    </delete>

    <!--根據(jù)主鍵查詢(xún)-->
    <select id="get" resultType="entity.TestEntity">
        select * from test where id = #{id}
    </select>

    <!--查詢(xún)所有數(shù)據(jù)-->
    <select id="list"  resultType="entity.TestEntity">
        select * from test
    </select>

    <!--根據(jù)名稱(chēng)模糊查詢(xún)-->
    <select id="listByNameLike" resultType="entity.TestEntity">
        select * from test  where name like CONCAT('%',#{name},'%')
    </select>

    <update id="update">
        update test set name =#{name}, salary = #{salary} where id = #{id}
    </update>
</mapper>

2.5 測(cè)試類(lèi)

先看一下數(shù)據(jù)庫(kù)數(shù)據(jù)

請(qǐng)?zhí)砑訄D片描述

新增數(shù)據(jù)

import dao.TestMapper;
import entity.TestEntity;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 一個(gè)生活在互聯(lián)網(wǎng)底層,做著增刪改查的碼農(nóng),不諳世事的造作
 * @create 2021-08-25 21:26
 */
public class TestMain {
    public static void main(String[] args) throws Exception {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        try (SqlSession session = sqlSessionFactory.openSession()) {
            // 通過(guò)sesson獲取Mapper 這個(gè)Mapper會(huì)編程Mybatis的代理Mapper
            TestMapper mapper = session.getMapper(TestMapper.class);
            System.out.println(mapper);
            // 1. 插入數(shù)據(jù)
            TestEntity entity = new TestEntity();
            entity.setName("小月鳥(niǎo)");
            entity.setSalary(new BigDecimal(50000));
            mapper.save(entity);
            TestEntity entity02 = new TestEntity();
            entity02.setName("小強(qiáng)01");
            entity02.setSalary(new BigDecimal(50000));
            mapper.save(entity02);
            TestEntity entity03 = new TestEntity();
            entity03.setName("小強(qiáng)02");
            entity03.setSalary(new BigDecimal(50000));
            mapper.save(entity03);
            // 手動(dòng)提交
            session.commit();
        }
    }
}

執(zhí)行完查看數(shù)據(jù)庫(kù)數(shù)據(jù):

請(qǐng)?zhí)砑訄D片描述

根據(jù)Id 查詢(xún)數(shù)據(jù)

import dao.TestMapper;
import entity.TestEntity;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.InputStream;

/**
 * 一個(gè)生活在互聯(lián)網(wǎng)底層,做著增刪改查的碼農(nóng),不諳世事的造作
 * @create 2021-08-25 21:26
 */
public class TestMain {
    public static void main(String[] args) throws Exception {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        try (SqlSession session = sqlSessionFactory.openSession()) {
            // 通過(guò)sesson獲取Mapper 這個(gè)Mapper會(huì)編程Mybatis的代理Mapper
            TestMapper mapper = session.getMapper(TestMapper.class);
            System.out.println(mapper);
            // 1. 根據(jù)Id 查詢(xún)數(shù)據(jù)
            TestEntity testEntity = mapper.get(1L);
            System.out.println("查詢(xún)數(shù)據(jù)為:"+testEntity);
        }
    }
}

輸出結(jié)果:

請(qǐng)?zhí)砑訄D片描述

更新數(shù)據(jù)
把”小月鳥(niǎo)“ 工資改成40000

import dao.TestMapper;
import entity.TestEntity;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.InputStream;
import java.math.BigDecimal;

/**
 * 一個(gè)生活在互聯(lián)網(wǎng)底層,做著增刪改查的碼農(nóng),不諳世事的造作
 * @create 2021-08-25 21:26
 */
public class TestMain {
    public static void main(String[] args) throws Exception {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        try (SqlSession session = sqlSessionFactory.openSession()) {
            // 通過(guò)sesson獲取Mapper 這個(gè)Mapper會(huì)編程Mybatis的代理Mapper
            TestMapper mapper = session.getMapper(TestMapper.class);
            System.out.println(mapper);
            // 更新
            TestEntity entityUpdate = new TestEntity();
            entityUpdate.setId(40L);
            entityUpdate.setName("小月鳥(niǎo)");
            entityUpdate.setSalary(new BigDecimal(40000));
            mapper.update(entityUpdate);
            session.commit();
        }
    }
}

執(zhí)行完查看數(shù)據(jù)庫(kù)數(shù)據(jù):

請(qǐng)?zhí)砑訄D片描述

查詢(xún)?nèi)繑?shù)據(jù)

import dao.TestMapper;
import entity.TestEntity;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.InputStream;
import java.util.List;

/**
 * 一個(gè)生活在互聯(lián)網(wǎng)底層,做著增刪改查的碼農(nóng),不諳世事的造作
 * @create 2021-08-25 21:26
 */
public class TestMain {
    public static void main(String[] args) throws Exception {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        try (SqlSession session = sqlSessionFactory.openSession()) {
            // 通過(guò)sesson獲取Mapper 這個(gè)Mapper會(huì)編程Mybatis的代理Mapper
            TestMapper mapper = session.getMapper(TestMapper.class);
            System.out.println(mapper);
            // 查詢(xún)列表
            List<TestEntity> list = mapper.list();
            if (list.size() >0) {
                for (int i = 0; i < list.size(); i++) {
                    System.out.println(list.get(i));
                }
            }
        }
    }
}


輸出結(jié)果:

請(qǐng)?zhí)砑訄D片描述

根據(jù)名稱(chēng)查詢(xún)數(shù)據(jù)

import dao.TestMapper;
import entity.TestEntity;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.InputStream;
import java.util.List;

/**
 * 一個(gè)生活在互聯(lián)網(wǎng)底層,做著增刪改查的碼農(nóng),不諳世事的造作
 * @create 2021-08-25 21:26
 */
public class TestMain {
    public static void main(String[] args) throws Exception {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        try (SqlSession session = sqlSessionFactory.openSession()) {
            // 通過(guò)sesson獲取Mapper 這個(gè)Mapper會(huì)編程Mybatis的代理Mapper
            TestMapper mapper = session.getMapper(TestMapper.class);
            System.out.println(mapper);
            // 根據(jù)名稱(chēng)模糊查詢(xún)列表
            List<TestEntity> list = mapper.listByNameLike("強(qiáng)");
            if (list.size() >0) {
                for (int i = 0; i < list.size(); i++) {
                    System.out.println(list.get(i));
                }
            }
        }
    }
}


輸出結(jié)果:

請(qǐng)?zhí)砑訄D片描述

刪除數(shù)據(jù)

import dao.TestMapper;
import entity.TestEntity;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.InputStream;
import java.util.List;

/**
 * 一個(gè)生活在互聯(lián)網(wǎng)底層,做著增刪改查的碼農(nóng),不諳世事的造作
 * @create 2021-08-25 21:26
 */
public class TestMain {
    public static void main(String[] args) throws Exception {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        try (SqlSession session = sqlSessionFactory.openSession()) {
            // 通過(guò)sesson獲取Mapper 這個(gè)Mapper會(huì)編程Mybatis的代理Mapper
            TestMapper mapper = session.getMapper(TestMapper.class);
            System.out.println(mapper);
            // 刪除數(shù)據(jù)
            mapper.delete(1L);
			session.commit();
        }
    }
}


執(zhí)行完查看數(shù)據(jù)庫(kù)數(shù)據(jù):

請(qǐng)?zhí)砑訄D片描述

項(xiàng)目結(jié)構(gòu):

請(qǐng)?zhí)砑訄D片描述

2.6 嘮嘮

1.剛開(kāi)始新增沒(méi)有成功 是因?yàn)闆](méi)有手動(dòng)commit (改變數(shù)據(jù)庫(kù)數(shù)據(jù)都需要commit)

現(xiàn)在大部分項(xiàng)目都是結(jié)合SpringBoot 事務(wù)都交給Spring管理了都不需要自己手動(dòng)commit了

2.實(shí)體類(lèi)沒(méi)有用別名 直接用的全類(lèi)路徑 實(shí)際項(xiàng)目中有用別名的有用全類(lèi)路徑的

3.新增的時(shí)候沒(méi)有返回自增主鍵的值 實(shí)際項(xiàng)目可能會(huì)用到這個(gè)值

4.更新的時(shí)候?qū)懰赖淖侄?實(shí)際項(xiàng)目可能會(huì)根據(jù)不同的值進(jìn)行不同列的更新

下篇預(yù)告:

  1. 實(shí)體類(lèi)用別名
  2. 新增返回自增主鍵的值
  3. 多個(gè)參數(shù)的使用
  4. 動(dòng)態(tài)Sql的常用標(biāo)簽
  5. 聊一聊 insert delete select update標(biāo)簽

補(bǔ)充一個(gè)知識(shí)點(diǎn)(應(yīng)該不用深入研究):

SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

SqlSessionFactoryBuilder有好幾種方式創(chuàng)建SqlSessionFactory

除了使用xml還可以純Java代碼創(chuàng)建

到此這篇關(guān)于一小時(shí)迅速入門(mén)Mybatis之增刪查改篇的文章就介紹到這了,更多相關(guān)Mybatis 增刪查改內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java經(jīng)典算法匯總之順序查找(Sequential Search)

    Java經(jīng)典算法匯總之順序查找(Sequential Search)

    Java查找算法之順序查找說(shuō)明:順序查找適合于存儲(chǔ)結(jié)構(gòu)為順序存儲(chǔ)或鏈接存儲(chǔ)的線性表。 下面我們來(lái)詳細(xì)說(shuō)明下
    2016-04-04
  • spring boot+mybatis 多數(shù)據(jù)源切換(實(shí)例講解)

    spring boot+mybatis 多數(shù)據(jù)源切換(實(shí)例講解)

    下面小編就為大家?guī)?lái)一篇spring boot+mybatis 多數(shù)據(jù)源切換(實(shí)例講解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-09-09
  • 關(guān)于MD5算法原理與常用實(shí)現(xiàn)方式

    關(guān)于MD5算法原理與常用實(shí)現(xiàn)方式

    這篇文章主要介紹了關(guān)于MD5算法原理與常用實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • Spring Boot集成Shiro實(shí)現(xiàn)動(dòng)態(tài)加載權(quán)限的完整步驟

    Spring Boot集成Shiro實(shí)現(xiàn)動(dòng)態(tài)加載權(quán)限的完整步驟

    這篇文章主要給大家介紹了關(guān)于Spring Boot集成Shiro實(shí)現(xiàn)動(dòng)態(tài)加載權(quán)限的完整步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • java中List接口的方法詳解

    java中List接口的方法詳解

    這篇文章主要介紹了java中List接口的方法詳解,List接口是繼承Collection接口,所以Collection集合中有的方法,List集合也繼承過(guò)來(lái),本文主要介紹一下list下的方法,需要的朋友可以參考下
    2023-10-10
  • SpringBoot使用log4j2將日志記錄到文件及自定義數(shù)據(jù)庫(kù)的配置方法

    SpringBoot使用log4j2將日志記錄到文件及自定義數(shù)據(jù)庫(kù)的配置方法

    這篇文章主要介紹了SpringBoot使用log4j2將日志記錄到文件及自定義數(shù)據(jù)庫(kù)的配置方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2024-03-03
  • Java線程之間的共享與協(xié)作詳解

    Java線程之間的共享與協(xié)作詳解

    這篇文章主要介紹了Java線程之間的共享與協(xié)作詳解,進(jìn)程是操作系統(tǒng)進(jìn)行資源分配的最小單位,線程是進(jìn)程的一個(gè)實(shí)體,是CPU調(diào)度和分派的基本單位,它是比經(jīng)常更小的、能夠獨(dú)立運(yùn)行的基本單位
    2022-07-07
  • Java實(shí)現(xiàn)數(shù)據(jù)脫敏的方法詳細(xì)講解

    Java實(shí)現(xiàn)數(shù)據(jù)脫敏的方法詳細(xì)講解

    這篇文章主要給大家介紹了關(guān)于Java實(shí)現(xiàn)數(shù)據(jù)脫敏的相關(guān)資料,數(shù)據(jù)脫敏是指對(duì)某些敏感信息通過(guò)脫敏規(guī)則進(jìn)行數(shù)據(jù)的變形,實(shí)現(xiàn)敏感隱私數(shù)據(jù)的可靠保護(hù),需要的朋友可以參考下
    2023-06-06
  • java如何去除圖片中的白色背景

    java如何去除圖片中的白色背景

    這篇文章主要為大家詳細(xì)介紹了java去除圖片中白色背景的方法,教大家如何將圖片中的白色背景去掉,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • MyBatis?if?test?判斷字符串相等不生效問(wèn)題

    MyBatis?if?test?判斷字符串相等不生效問(wèn)題

    這篇文章主要介紹了MyBatis?if?test?判斷字符串相等不生效問(wèn)題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-10-10

最新評(píng)論