一小时迅速入门Mybatis之增删查改篇

目录

  • 一、说明
  • 二、开搞
    • 2.1 数据库表
    • 2.1 创建实体类
    • 2.2 创建接口
    • 2.3 创建XML
    • 2.5 测试类
    • 2.6 唠唠

一、说明 这二篇涉及到映射Java实体类、面向接口编写Mybatis、增删查改示例
怎么引入jar包,怎么配置数据库看上一篇哦~

二、开搞
2.1 数据库表
上一篇好像丢了数据库创建语句
-- 主键自增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);


2.1 创建实体类
package entity; import java.math.BigDecimal; /** * @author 发现更多精彩关注公众号:木子的昼夜编程 * 一个生活在互联网底层,做着增删改查的码农,不谙世事的造作 * @create 2021-08-25 22:05 */public class TestEntity {privateLong 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; }@Overridepublic String toString() {return "TestEntity{" +"id=" + id +", name='" + name + '\'' +", salary=" + salary +'}'; }}


2.2 创建接口
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 list(); // 根据名称模糊查询List listByNameLike(String name); }


2.3 创建XML
mybatis-config.xml

TestMapper.xml
INSERT INTO `test`( `name`, `salary`) VALUE (#{name}, #{salary}); delete from test where id = #{id}select * from test where id = #{id}select * from testselect * from testwhere name like CONCAT('%',#{name},'%')update test set name =#{name}, salary = #{salary} where id = #{id}


2.5 测试类
先看一下数据库数据
一小时迅速入门Mybatis之增删查改篇
文章图片

新增数据
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的代理MapperTestMapper 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(); }}}

执行完查看数据库数据:
一小时迅速入门Mybatis之增删查改篇
文章图片

根据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的代理MapperTestMapper mapper = session.getMapper(TestMapper.class); System.out.println(mapper); // 1. 根据Id 查询数据TestEntity testEntity = mapper.get(1L); System.out.println("查询数据为:"+testEntity); }}}

输出结果:
一小时迅速入门Mybatis之增删查改篇
文章图片

更新数据
把”小月鸟“ 工资改成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的代理MapperTestMapper 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(); }}}

执行完查看数据库数据:
一小时迅速入门Mybatis之增删查改篇
文章图片

查询全部数据
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的代理MapperTestMapper mapper = session.getMapper(TestMapper.class); System.out.println(mapper); // 查询列表List list = mapper.list(); if (list.size() >0) {for (int i = 0; i < list.size(); i++) {System.out.println(list.get(i)); }}}}}

输出结果:
一小时迅速入门Mybatis之增删查改篇
文章图片

根据名称查询数据
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的代理MapperTestMapper mapper = session.getMapper(TestMapper.class); System.out.println(mapper); // 根据名称模糊查询列表List list = mapper.listByNameLike("强"); if (list.size() >0) {for (int i = 0; i < list.size(); i++) {System.out.println(list.get(i)); }}}}}

输出结果:
一小时迅速入门Mybatis之增删查改篇
文章图片

删除数据
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的代理MapperTestMapper mapper = session.getMapper(TestMapper.class); System.out.println(mapper); // 删除数据mapper.delete(1L); session.commit(); }}}

执行完查看数据库数据:
一小时迅速入门Mybatis之增删查改篇
文章图片

项目结构:
一小时迅速入门Mybatis之增删查改篇
文章图片


2.6 唠唠
1.刚开始新增没有成功 是因为没有手动commit (改变数据库数据都需要commit)
现在大部分项目都是结合SpringBoot 事务都交给Spring管理了都不需要自己手动commit了
2.实体类没有用别名 直接用的全类路径 实际项目中有用别名的有用全类路径的
3.新增的时候没有返回自增主键的值 实际项目可能会用到这个值
4.更新的时候写死的字段 实际项目可能会根据不同的值进行不同列的更新
下篇预告:
  1. 实体类用别名
  2. 新增返回自增主键的值
  3. 多个参数的使用
  4. 动态Sql的常用标签
  5. 聊一聊 insert delete select update标签
补充一个知识点(应该不用深入研究):
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

SqlSessionFactoryBuilder有好几种方式创建SqlSessionFactory
除了使用xml还可以纯Java代码创建
【一小时迅速入门Mybatis之增删查改篇】到此这篇关于一小时迅速入门Mybatis之增删查改篇的文章就介绍到这了,更多相关Mybatis 增删查改内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    推荐阅读