您好,登錄后才能下訂單哦!
這篇文章主要介紹“Mybatis增刪查改的命令用法”,在日常操作中,相信很多人在Mybatis增刪查改的命令用法問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Mybatis增刪查改的命令用法”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
一、說明
二、開搞
2.1 數據庫表
2.1 創建實體類
2.2 創建接口
2.3 創建XML
2.5 測試類
這二篇涉及到映射Java實體類、面向接口編寫Mybatis、增刪查改示例
上一篇好像丟了數據庫創建語句
-- 主鍵自增 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; -- ---------------------------- -- 插入測試數據 -- ---------------------------- INSERT INTO `test` VALUES (1, '小明', 30000.00);
package entity; import java.math.BigDecimal; /** * @author 發現更多精彩 關注公眾號:木子的晝夜編程 * 一個生活在互聯網底層,做著增刪改查的碼農,不諳世事的造作 * @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 + '}'; } }
package dao; import entity.TestEntity; import java.util.List; /** * @author 發現更多精彩 關注公眾號:木子的晝夜編程 分享一個生活在互聯網底層做著增刪改查的碼農的感悟與學習 * @create 2021-08-25 22:07 */ public interface TestMapper { // 新增 void save(TestEntity testEntity); // 修改 void update(TestEntity testEntity); // 刪除 這里就一個參數 所以不用@Param 也不用Map 自定義實體類等 void delete(Long id); // 根據主鍵查詢 TestEntity get(Long id); // 查詢所有數據 List<TestEntity> list(); // 根據名稱模糊查詢 List<TestEntity> listByNameLike(String name); }
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&characterEncoding=utf-8&useSSL=false&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> <!--根據主鍵查詢--> <select id="get" resultType="entity.TestEntity"> select * from test where id = #{id} </select> <!--查詢所有數據--> <select id="list" resultType="entity.TestEntity"> select * from test </select> <!--根據名稱模糊查詢--> <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>
先看一下數據庫數據
新增數據
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; /** * @author 發現更多精彩 關注公眾號:木子的晝夜編程 * 一個生活在互聯網底層,做著增刪改查的碼農,不諳世事的造作 * @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. 插入數據 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(); } } }
執行完查看數據庫數據:
根據Id 查詢數據
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; /** * @author 發現更多精彩 關注公眾號:木子的晝夜編程 * 一個生活在互聯網底層,做著增刪改查的碼農,不諳世事的造作 * @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. 根據Id 查詢數據 TestEntity testEntity = mapper.get(1L); System.out.println("查詢數據為:"+testEntity); } } }
輸出結果:
更新數據
把”小月鳥“ 工資改成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; /** * @author 發現更多精彩 關注公眾號:木子的晝夜編程 * 一個生活在互聯網底層,做著增刪改查的碼農,不諳世事的造作 * @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(); } } }
執行完查看數據庫數據:
查詢全部數據
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; /** * @author 發現更多精彩 關注公眾號:木子的晝夜編程 * 一個生活在互聯網底層,做著增刪改查的碼農,不諳世事的造作 * @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)); } } } } }
輸出結果:
根據名稱查詢數據
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; /** * @author 發現更多精彩 關注公眾號:木子的晝夜編程 * 一個生活在互聯網底層,做著增刪改查的碼農,不諳世事的造作 * @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.listByNameLike("強"); if (list.size() >0) { for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } } } } }
輸出結果:
刪除數據
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; /** * @author 發現更多精彩 關注公眾號:木子的晝夜編程 * 一個生活在互聯網底層,做著增刪改查的碼農,不諳世事的造作 * @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); // 刪除數據 mapper.delete(1L); session.commit(); } } }
執行完查看數據庫數據:
項目結構:
到此,關于“Mybatis增刪查改的命令用法”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。