最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

一小時迅速入門Mybatis之增刪查改篇

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

一、說明

這二篇涉及到映射Java實體類、面向接口編寫Mybatis、增刪查改示例

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

二、開搞

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

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

-- 主鍵自增
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;

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

2.1 創(chuàng)建實體類

package entity;

import java.math.BigDecimal;

/**
 * 一個生活在互聯(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;

/**
 * 一個生活在互聯(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);

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

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

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

    // 根據(jù)名稱模糊查詢
    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ù)主鍵查詢-->
    <select id="get" resultType="entity.TestEntity">
        select * from test where id = #{id}
    </select>

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

    <!--根據(jù)名稱模糊查詢-->
    <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 測試類

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

請?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;

/**
 * 一個生活在互聯(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()) {
            // 通過sesson獲取Mapper 這個Mapper會編程Mybatis的代理Mapper
            TestMapper mapper = session.getMapper(TestMapper.class);
            System.out.println(mapper);
            // 1. 插入數(shù)據(jù)
            TestEntity entity = new TestEntity();
            entity.setName("小月鳥");
            entity.setSalary(new BigDecimal(50000));
            mapper.save(entity);
            TestEntity entity02 = new TestEntity();
            entity02.setName("小強01");
            entity02.setSalary(new BigDecimal(50000));
            mapper.save(entity02);
            TestEntity entity03 = new TestEntity();
            entity03.setName("小強02");
            entity03.setSalary(new BigDecimal(50000));
            mapper.save(entity03);
            // 手動提交
            session.commit();
        }
    }
}

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

請?zhí)砑訄D片描述

根據(jù)Id 查詢數(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;

/**
 * 一個生活在互聯(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()) {
            // 通過sesson獲取Mapper 這個Mapper會編程Mybatis的代理Mapper
            TestMapper mapper = session.getMapper(TestMapper.class);
            System.out.println(mapper);
            // 1. 根據(jù)Id 查詢數(shù)據(jù)
            TestEntity testEntity = mapper.get(1L);
            System.out.println("查詢數(shù)據(jù)為:"+testEntity);
        }
    }
}

輸出結(jié)果:

請?zhí)砑訄D片描述

更新數(shù)據(jù)
把”小月鳥“ 工資改成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;

/**
 * 一個生活在互聯(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()) {
            // 通過sesson獲取Mapper 這個Mapper會編程Mybatis的代理Mapper
            TestMapper mapper = session.getMapper(TestMapper.class);
            System.out.println(mapper);
            // 更新
            TestEntity entityUpdate = new TestEntity();
            entityUpdate.setId(40L);
            entityUpdate.setName("小月鳥");
            entityUpdate.setSalary(new BigDecimal(40000));
            mapper.update(entityUpdate);
            session.commit();
        }
    }
}

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

請?zhí)砑訄D片描述

查詢?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;

/**
 * 一個生活在互聯(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()) {
            // 通過sesson獲取Mapper 這個Mapper會編程Mybatis的代理Mapper
            TestMapper mapper = session.getMapper(TestMapper.class);
            System.out.println(mapper);
            // 查詢列表
            List<TestEntity> list = mapper.list();
            if (list.size() >0) {
                for (int i = 0; i < list.size(); i++) {
                    System.out.println(list.get(i));
                }
            }
        }
    }
}


輸出結(jié)果:

請?zhí)砑訄D片描述

根據(jù)名稱查詢數(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;

/**
 * 一個生活在互聯(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()) {
            // 通過sesson獲取Mapper 這個Mapper會編程Mybatis的代理Mapper
            TestMapper mapper = session.getMapper(TestMapper.class);
            System.out.println(mapper);
            // 根據(jù)名稱模糊查詢列表
            List<TestEntity> list = mapper.listByNameLike("強");
            if (list.size() >0) {
                for (int i = 0; i < list.size(); i++) {
                    System.out.println(list.get(i));
                }
            }
        }
    }
}


輸出結(jié)果:

請?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;

/**
 * 一個生活在互聯(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()) {
            // 通過sesson獲取Mapper 這個Mapper會編程Mybatis的代理Mapper
            TestMapper mapper = session.getMapper(TestMapper.class);
            System.out.println(mapper);
            // 刪除數(shù)據(jù)
            mapper.delete(1L);
			session.commit();
        }
    }
}


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

請?zhí)砑訄D片描述

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

請?zhí)砑訄D片描述

2.6 嘮嘮

1.剛開始新增沒有成功 是因為沒有手動commit (改變數(shù)據(jù)庫數(shù)據(jù)都需要commit)

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

2.實體類沒有用別名 直接用的全類路徑 實際項目中有用別名的有用全類路徑的

3.新增的時候沒有返回自增主鍵的值 實際項目可能會用到這個值

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

下篇預(yù)告:

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

補充一個知識點(應(yīng)該不用深入研究):

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

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

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

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

相關(guān)文章

  • 分享幾個Java工作中實用的代碼優(yōu)化技巧

    分享幾個Java工作中實用的代碼優(yōu)化技巧

    這篇文章主要給大家分享幾個Java工作中實用代碼優(yōu)化技巧,文章基于Java的相關(guān)資料展開對其優(yōu)化技巧的分享,需要的小伙伴可以參考一下
    2022-04-04
  • Java反射機制,反射相關(guān)API,反射API使用方式(反射獲取實體類字段名和注解值)

    Java反射機制,反射相關(guān)API,反射API使用方式(反射獲取實體類字段名和注解值)

    這篇文章主要介紹了Java反射機制,反射相關(guān)API,反射API使用方式(反射獲取實體類字段名和注解值),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • 解決SpringBoot整合RocketMQ遇到的坑

    解決SpringBoot整合RocketMQ遇到的坑

    這篇文章主要介紹了解決SpringBoot整合RocketMQ遇到的坑,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • spring+netty服務(wù)器搭建的方法

    spring+netty服務(wù)器搭建的方法

    本篇文章主要介紹了spring+netty服務(wù)器搭建的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01
  • 詳解Maven項目Dependencies常見報錯及解決方案

    詳解Maven項目Dependencies常見報錯及解決方案

    這篇文章主要介紹了詳解Maven項目Dependencies常見報錯及解決方案,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • Scala求和示例代碼

    Scala求和示例代碼

    這篇文章主要介紹了Scala求和示例代碼,需要的朋友可以參考下
    2019-06-06
  • JAVA多線程知識匯總

    JAVA多線程知識匯總

    這篇文章主要介紹了JAVA多線程的相關(guān)資料,文中講解非常細致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • java實現(xiàn)簡單的拼圖游戲

    java實現(xiàn)簡單的拼圖游戲

    這篇文章主要為大家詳細介紹了java實現(xiàn)簡單的拼圖游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • SpringBoot實現(xiàn)License認證(只校驗有效期)的詳細過程

    SpringBoot實現(xiàn)License認證(只校驗有效期)的詳細過程

    License也就是版權(quán)許可證書,一般用于收費軟件給付費用戶提供的訪問許可證明,這篇文章主要介紹了SpringBoot實現(xiàn)License認證(只校驗有效期),需要的朋友可以參考下
    2024-04-04
  • Mybatis中@Param注解的用法詳解

    Mybatis中@Param注解的用法詳解

    @Param注解的作用是給參數(shù)命名,參數(shù)命名后就能根據(jù)名字得到參數(shù)值,正確的將參數(shù)傳入sql語句中,下面這篇文章主要給大家介紹了關(guān)于Mybatis中@Param注解用法的相關(guān)資料,需要的朋友可以參考下
    2022-07-07

最新評論

岚皋县| 积石山| 河东区| 栾川县| 和政县| 政和县| 青海省| 西丰县| 永丰县| 阳春市| 望奎县| 朝阳市| 长汀县| 惠安县| 芜湖市| 界首市| 梧州市| 澄城县| 泽普县| 青河县| 惠东县| 太湖县| 中宁县| 鹤峰县| 嫩江县| 资阳市| 吴堡县| 开阳县| 萝北县| 恩施市| 界首市| 汉源县| 金溪县| 陈巴尔虎旗| 嘉荫县| 枝江市| 广安市| 泰安市| 莱阳市| 应用必备| 新昌县|