MyBatis单表操作
- mapper.xml修改 完成CRUD的操作
<?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="com.tongjun.mybatis.mapper.DepartmentMapper">
<insert id="insert" parameterType="com.tongjun.mybatis.entity.Department">
insert into tbl_department (id, name, tel) values (#{id}, #{name}, #{tel})
</insert>
<update id="update" parameterType="com.tongjun.mybatis.entity.Department">
update tbl_department set name = #{name}, tel = #{tel} where id = #{id}
</update>
<delete id="deleteById" parameterType="string">
delete from tbl_department where id = #{id}
</delete>
<select id="findAll" resultType="com.tongjun.mybatis.entity.Department">
select * from tbl_department
</select>
<select id="findById" parameterType="string" resultType="com.tongjun.mybatis.entity.Department">
select * from tbl_department where id = #{id}
</select>
</mapper>
- 动态代理的DepartmentMapper接口
package com.tongjun.mybatis.mapper;
import com.tongjun.mybatis.entity.Department;
import java.util.List;
/**
* @author tj
* @date 2022/8/25 10:47
* @Version 1.0
*/
public interface DepartmentMapper {
List<Department> findAll();
int insert(Department department);
int update(Department department);
int deleteById(String id);
Department findById(String id);
}
- 实体类没有区别
package com.tongjun.mybatis.entity;
import lombok.Data;
import java.io.Serializable;
/**
* @author tj
* @date 2022/8/25 10:26
* @Version 1.0
*/
@Data
public class Department implements Serializable {
private String id;
private String deptName;
private String phone;
}
- 编写测试程序
package com.tongjun.mybatis.app;
import com.tongjun.mybatis.entity.Department;
import com.tongjun.mybatis.mapper.DepartmentMapper;
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 tj
* @date 2022/8/25 10:28
* @Version 1.0
*/
public class ApplicationTest {
public static void main(String[] args) throws Exception{
//拿到mybatis配置文件的流
InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
//创建sqlSession工厂
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
//通过工厂拿到sqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//获取Mapper接口的代理对象
DepartmentMapper departmentMapper = sqlSession.getMapper(DepartmentMapper.class);
List<Department> departmentList = departmentMapper.findAll();
System.out.println("---------------departmentMapper.findAll()-----------------");
departmentList.forEach(System.out::println);
Department department = departmentMapper.findById("53e3803ebbf4f97968e0253e5ad4cc83");
System.out.println("---------------departmentMapper.findById-----------------");
System.out.println(department);
}
}
- 查看控制台输出信息
---------------departmentMapper.findAll()-----------------
Department(id=18ec781fbefd727923b0d35740b177ab, deptName=开发部, phone=110)
Department(id=53e3803ebbf4f97968e0253e5ad4cc83, deptName=测试产品部, phone=111)
Department(id=ee0e342201004c1721e69a99ac0dc0df, deptName=运维部, phone=112)
2022-08-25 10:52:55,350 2431 [ main] DEBUG pper.DepartmentMapper.findById - ==> Preparing: select * from tbl_department where id = ?
2022-08-25 10:52:55,350 2431 [ main] DEBUG pper.DepartmentMapper.findById - ==> Parameters: 53e3803ebbf4f97968e0253e5ad4cc83(String)
2022-08-25 10:52:55,366 2447 [ main] DEBUG pper.DepartmentMapper.findById - <== Total: 1
---------------departmentMapper.findById-----------------
Department(id=53e3803ebbf4f97968e0253e5ad4cc83, deptName=测试产品部, phone=111)
标签:03,DepartmentMapper,笔记,mybatis,Department,department,MyBatis,import,id
From: https://www.cnblogs.com/tongjun/p/16623579.html