在数字化转型的浪潮下,小程序游戏如何成为企业吸引年轻用户的重要工具?
641
2022-12-08
一小时迅速入门Mybatis之增删查改篇
目录一、说明二、开搞2.1 数据库表2.1 创建实体类2.2 创建接口2.3 创建XML2.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;
/**
* 一个生活在互联网底层,做着增删改查的码农,不谙世事的造作
* @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 创建接口
package dao;
import entity.TestEntity;
import java.util.List;
/**
* @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
}
2.3 创建XML
mybatis-config.xml
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
TestMapper.xml
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
INSERT INTO `test`( `name`, `salary`) VALUE (#{name}, #{salary});
</insert>
delete from test where id = #{id}
select * from test where id = #{id}
select * from test
select * from test where name like CONCAT('%',#{name},'%')
update test set name =#{name}, salary = #{salary} where id = #{id}
2.5 测试类
先看一下数据库数据
新增数据
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;
/**
* 一个生活在互联网底层,做着增删改查的码农,不谙世事的造作
* @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;
/**
* 一个生活在互联网底层,做着增删改查的码农,不谙世事的造作
* @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;
/**
* 一个生活在互联网底层,做着增删改查的码农,不谙世事的造作
* @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;
/**
* 一个生活在互联网底层,做着增删改查的码农,不谙世事的造作
* @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
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;
/**
* 一个生活在互联网底层,做着增删改查的码农,不谙世事的造作
* @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
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;
/**
* 一个生活在互联网底层,做着增删改查的码农,不谙世事的造作
* @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();
}
}
}
执行完查看数据库数据:
项目结构:
2.6 唠唠
1.刚开始新增没有成功 是因为没有手动commit (改变数据库数据都需要commit)
现在大部分项目都是结合SpringBoot 事务都交给Spring管理了都不需要自己手动commit了
2.实体类没有用别名 直接用的全类路径 实际项目中有用别名的有用全类路径的
3.新增的时候没有返回自增主键的值 实际项目可能会用到这个值
4.更新的时候写死的字段 实际项目可能会根据不同的值进行不同列的更新
下篇预告:
实体类用别名
新增返回自增主键的值
多个参数的使用
动态Sql的常用标签
聊一聊 insert delete select update标签
补充一个知识点(应该不用深入研究):
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSessionFactoryBuilder有好几种方式创建SqlSessionFactory
除了使用xml还可以纯Java代码创建
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~