海建网站,查电商软件下载,html友情链接代码,做网站可以赚多少钱一、MyBatis 概述
1. 框架简介 MyBatis 是一款支持自定义 SQL、存储过程以及高级映射的持久层框架。它避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集的操作#xff0c;使开发人员能够更专注于 SQL 语句的编写和业务逻辑的处理。
2. 核心组件
SqlSessionFactoryB…一、MyBatis 概述
1. 框架简介 MyBatis 是一款支持自定义 SQL、存储过程以及高级映射的持久层框架。它避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集的操作使开发人员能够更专注于 SQL 语句的编写和业务逻辑的处理。
2. 核心组件
SqlSessionFactoryBuilder用于创建 SqlSessionFactory 实例。SqlSessionFactory是 MyBatis 的核心对象负责创建 SqlSession 实例。SqlSession提供了执行 SQL 语句、管理事务等功能。
3. 工作原理 MyBatis 通过读取配置文件和映射文件将 SQL 语句与 Java 方法进行映射。在运行时根据方法调用生成相应的 SQL 语句并执行数据库操作最后将结果映射为 Java 对象返回。
二、MyBatis 基础操作
1. 环境搭建
引入 MyBatis 依赖以 Maven 项目为例
dependencygroupIdorg.mybatis/groupIdartifactIdmybatis/artifactIdversionx.x.x/version
/dependency配置mybatis-config.xml文件
?xml version1.0 encodingUTF-8?
!DOCTYPE configuration PUBLIC -//mybatis.org//DTD Config 3.0//EN http://mybatis.org/dtd/mybatis-3-config.dtd
configurationenvironments defaultdevelopmentenvironment iddevelopmenttransactionManager typeJDBC /dataSource typePOOLEDproperty namedriver value${jdbc.driver} /property nameurl value${jdbc.url} /property nameusername value${jdbc.username} /property namepassword value${jdbc.password} //dataSource/environment/environmentsmappersmapper resourcemapper/EmpMapper.xml /mapper resourcemapper/DeptMapper.xml //mappers
/configuration2. 单表操作 实体类定义
public class Emp {private Integer empid;private String empname;private String empjob;private BigDecimal empsalary;private Integer empdid;// 省略getter和setter方法
}Mapper 接口定义
public interface EmpDao {ListEmp findAll();Emp findById(Integer id);int insert(Emp emp);int update(Emp emp);int delete(Integer id);
}Mapper XML 文件配置
?xml version1.0 encodingUTF-8?
!DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//EN http://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespaceorg.example.Dao.EmpDaoresultMap idBaseResultMap typeEmpid propertyempid columnemp_id jdbcTypeINTEGER /result propertyempname columnemp_name jdbcTypeVARCHAR /result propertyempjob columnemp_job jdbcTypeVARCHAR /result propertyempsalary columnemp_salary jdbcTypeDECIMAL /result propertyempdid columndid jdbcTypeINTEGER //resultMapselect idfindAll resultMapBaseResultMapselect * from tbl_emp/selectselect idfindById resultMapBaseResultMapselect * from tbl_emp where emp_id #{id}/selectinsert idinsert keyPropertyempid useGeneratedKeystrueinsert into tbl_emp(emp_name, emp_job, emp_salary, did)values (#{empname}, #{empjob}, #{empsalary}, #{empdid})/insertupdate idupdateupdate tbl_empset emp_name #{empname}, emp_job #{empjob}, emp_salary #{empsalary}, did #{empdid}where emp_id #{empid}/updatedelete iddeletedelete from tbl_emp where emp_id #{id}/delete
/mapper3. 测试类编写
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 org.example.Entity.Emp;
import org.junit.Test;import java.io.IOException;
import java.io.InputStream;
import java.util.List;public class EmpDaoTest {Testpublic void testFindAll() throws IOException {InputStream resourceAsStream Resources.getResourceAsStream(mybatis-config.xml);SqlSessionFactory sqlSessionFactory new SqlSessionFactoryBuilder().build(resourceAsStream);SqlSession sqlSession sqlSessionFactory.openSession();EmpDao empDao sqlSession.getMapper(EmpDao.class);ListEmp emps empDao.findAll();for (Emp emp : emps) {System.out.println(emp);}sqlSession.close();}Testpublic void testFindById() throws IOException {InputStream resourceAsStream Resources.getResourceAsStream(mybatis-config.xml);SqlSessionFactory sqlSessionFactory new SqlSessionFactoryBuilder().build(resourceAsStream);SqlSession sqlSession sqlSessionFactory.openSession();EmpDao empDao sqlSession.getMapper(EmpDao.class);Emp emp empDao.findById(1);System.out.println(emp);sqlSession.close();}Testpublic void testInsert() throws IOException {InputStream resourceAsStream Resources.getResourceAsStream(mybatis-config.xml);SqlSessionFactory sqlSessionFactory new SqlSessionFactoryBuilder().build(resourceAsStream);SqlSession sqlSession sqlSessionFactory.openSession();EmpDao empDao sqlSession.getMapper(EmpDao.class);Emp emp new Emp();emp.setEmpname(张三);emp.setEmpjob(开发工程师);emp.setEmpsalary(new BigDecimal(8000));emp.setEmpdid(1);int result empDao.insert(emp);System.out.println(插入成功影响行数 result);sqlSession.commit();sqlSession.close();}Testpublic void testUpdate() throws IOException {InputStream resourceAsStream Resources.getResourceAsStream(mybatis-config.xml);SqlSessionFactory sqlSessionFactory new SqlSessionFactoryBuilder().build(resourceAsStream);SqlSession sqlSession sqlSessionFactory.openSession();EmpDao empDao sqlSession.getMapper(EmpDao.class);Emp emp new Emp();emp.setEmpid(1);emp.setEmpname(李四);emp.setEmpjob(高级开发工程师);emp.setEmpsalary(new BigDecimal(10000));emp.setEmpdid(2);int result empDao.update(emp);System.out.println(更新成功影响行数 result);sqlSession.commit();sqlSession.close();}Testpublic void testDelete() throws IOException {InputStream resourceAsStream Resources.getResourceAsStream(mybatis-config.xml);SqlSessionFactory sqlSessionFactory new SqlSessionFactoryBuilder().build(resourceAsStream);SqlSession sqlSession sqlSessionFactory.openSession();EmpDao empDao sqlSession.getMapper(EmpDao.class);int result empDao.delete(1);System.out.println(删除成功影响行数 result);sqlSession.commit();sqlSession.close();}
}三、MyBatis 高级特性
1. 分页查询
引入 PageHelper 依赖
dependencygroupIdcom.github.pagehelper/groupIdartifactIdpagehelper/artifactIdversion6.0.0/version
/dependency在 MyBatis 配置文件中添加插件配置
pluginsplugin interceptorcom.github.pagehelper.PageInterceptor!-- 可配置参数 --property nameparam1 valuevalue1 //plugin
/plugins代码示例
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
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 org.example.Dao.EmpDao;
import org.example.Entity.Emp;
import org.junit.Test;import java.io.IOException;
import java.io.InputStream;
import java.util.List;public class PaginationTest {Testpublic void testPagination() throws IOException {InputStream resourceAsStream Resources.getResourceAsStream(mybatis-config.xml);SqlSessionFactory sqlSessionFactory new SqlSessionFactoryBuilder().build(resourceAsStream);SqlSession sqlSession sqlSessionFactory.openSession();EmpDao empDao sqlSession.getMapper(EmpDao.class);// 开启分页查询第一页每页显示3条数据PageHelper.startPage(1, 3);ListEmp emps empDao.findAll();// 将查询结果封装到PageInfo中PageInfoEmp pageInfo new PageInfo(emps);// 输出相关信息System.out.println(总条数 pageInfo.getTotal());System.out.println(总页数 pageInfo.getPages());System.out.println(当前页记录);ListEmp list pageInfo.getList();for (Emp emp : list) {System.out.println(emp);}sqlSession.close();}
}2. 联表查询
多对一关系查询以员工和部门为例使用association标签
?xml version1.0 encodingUTF-8?
!DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//EN http://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespaceorg.example.Dao.EmpDaoresultMap idEmpWithDeptResultMap typeEmpid propertyempid columnemp_id jdbcTypeINTEGER /result propertyempname columnemp_name jdbcTypeVARCHAR /result propertyempjob columnemp_job jdbcTypeVARCHAR /result propertyempsalary columnemp_salary jdbcTypeDECIMAL /result propertyempdid columndid jdbcTypeINTEGER /association propertydept javaTypeDeptid propertydeptid columndept_id jdbcTypeINTEGER /result propertydeptname columndept_name jdbcTypeVARCHAR /result propertydeptloc columndept_loc jdbcTypeVARCHAR //association/resultMapselect idfindAllWithDept resultMapEmpWithDeptResultMapselect e.*, d.dept_id, d.dept_name, d.dept_locfrom tbl_emp ejoin tbl_dept d on e.did d.dept_id/select
/mapper一对多关系查询以部门和员工为例使用collection标签
?xml version1.0 encodingUTF-8?
!DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//EN http://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespaceorg.example.Dao.DeptDaoresultMap idDeptWithEmpsResultMap typeDeptid propertydeptid columndept_id jdbcTypeINTEGER /result propertydeptname columndept_name jdbcTypeVARCHAR /result propertydeptloc columndept_loc jdbcTypeVARCHAR /collection propertyemps ofTypeEmpid propertyempid columnemp_id jdbcTypeINTEGER /result propertyempname columnemp_name jdbcTypeVARCHAR /result propertyempjob columnemp_job jdbcTypeVARCHAR /result propertyempsalary columnemp_salary jdbcTypeDECIMAL /result propertyempdid columndid jdbcTypeINTEGER //collection/resultMapselect idfindAllWithEmps resultMapDeptWithEmpsResultMapselect d.*, e.*from tbl_dept dleft join tbl_emp e on d.dept_id e.did/select
/mapper3. 动态 SQL
trim通过修剪 SQL 语句的开头和结尾来动态生成 SQL 片段。它可以用于去除不必要的 SQL 关键字或条件语句并提供了一些属性来定义修剪规则。
where用于在生成的 SQL 语句中添加 WHERE 子句。它可以自动处理条件语句的前缀并在有条件语句存在时添加 WHERE 关键字。而且会去除sql的第一个and标签。
set用于在生成的 SQL 语句中添加 SET 子句。它主要用于更新操作可以根据条件来动态生成需要更新的列。
foreach用于在生成的 SQL 语句中进行循环操作。它可以遍历集合或数组并根据指定的模板将集合元素或数组元素插入到 SQL 语句中。
if用于在生成的 SQL 语句中添加条件判断。可以根据指定的条件决定是否包含某个 SQL 语句片段。
choose类似于 Java 中的 switch 语句根据条件选择执行不同的 SQL 语句片段。它可以包含多个 when 和一个可选的 otherwise 标签。
when用于在 choose 标签中定义条件分支。可以根据指定的条件判断是否执行特定的 SQL 语句片段。
otherwise在 choose 标签中可选的标签用于定义当没有任何 when 条件匹配时执行的 SQL 语句片段。
if标签示例根据条件查询员工
?xml version1.0 encodingUTF-8?
!DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//EN http://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespaceorg.example.Dao.EmpDaoselect idselectByCondition resultMapBaseResultMapselect * from tbl_empwhereif testname! null and name! and emp_name like concat(%, #{name}, %)/ifif testjob! null and job! and emp_job #{job}/ifif testsalary! nulland emp_salary #{salary}/if/where/select
/mapperchoose、when、otherwise标签示例根据条件查询员工
?xml version1.0 encodingUTF-8?
!DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//EN http://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespaceorg.example.Dao.EmpDaoselect idselectByCondition2 resultMapBaseResultMapselect * from tbl_empwherechoosewhen testname! null and name! and emp_name like concat(%, #{name}, %)/whenwhen testjob! null and job! and emp_job #{job}/whenwhen testsalary! nulland emp_salary #{salary}/whenotherwiseand 1 1/otherwise/choose/where/select
/mapperforeach标签示例批量删除员工
?xml version1.0 encodingUTF-8?
!DOCTYPE mapper PUBLIC -//mybatis.org//DTD Mapper 3.0//EN http://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespaceorg.example.Dao.EmpDaodelete idbatchDeletedelete from tbl_emp where emp_id inforeach collectionids itemid open( close) separator,#{id}/foreach/delete
/mapper