0.前言
前面的文章介绍resultType,除了resultType外,我们经常用到的还有parameterType。parameterType是参数类型,就是我们传递给数据库的这个参数的类型,
1.传入类型
- 基本数据类型:int、string、Date等基本数据类型都可以
- 类(JavaBean、List): (没有太好的例子,所以归为一类)
- Map
注意:
parameterType在查询全部方法不需要这个参数,也就是没有
parameterType添加返回的返回的是****实体类的全类名
parameterType在更新方法里,这个参数返回的也是实体类的全类名
parameterType在删除方法里,这个参数返回的也是实体类的Integer,就是成功影响一条数据的记录表示成功
2.例子
(1)基本类型
场景:比如我要根据学生的id去删除一条数据,那么我就要传入这个学生的id。
StudentsMapper文件
import com.cat.pojo.Students;
public interface StudentsMapper {
List<Students> listAllStudents();
int deleteStudent(int id);
Mapper.xml
<?xml version="1.0" encoding="UTF-8"?>
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cat.dao.StudentsMapper">
<select id="listAllStudents" resultType="com.cat.pojo.Students">
<delete id="deleteStudent" parameterType="int">
delete from students where id = #{id}
test文件
import com.cat.dao.StudentsMapper;
import com.cat.pojo.Students;
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.IOException;
import java.io.InputStream;
public class MyBatisTest {
public void test() throws IOException{
InputStream in = Resources.getResourceAsStream("MyBatisConfig.xml");
SqlSessionFactoryBuilder builder =new SqlSessionFactoryBuilder();
SqlSessionFactory factory=builder.build(in);
SqlSession session =factory.openSession();
List<Students> students =session.selectList("listAllStudents");
System.out.println(students);
public void deleteStudent() throws IOException{
InputStream in = Resources.getResourceAsStream("MyBatisConfig.xml");
SqlSessionFactoryBuilder builder =new SqlSessionFactoryBuilder();
SqlSessionFactory factory=builder.build(in);
SqlSession session =factory.openSession();
StudentsMapper studentsMapper = session.getMapper(StudentsMapper.class);
studentsMapper.deleteStudent(1000); //调用deleteStudent方法,删除id是1000的这个学生
结果:这样运行之后就完成了根据id删除一条信息的功能。
(2)类(JavaBean、List)
场景:我要新增一个学生的信息,就需要传入这个学生的整体信息。
注:我因为没有好的例子,所以把JavaBean和List都放这,实际用法不完全一样。
StudentsMapper:
import com.cat.pojo.Students;
public interface StudentsMapper {
int addStudent(Students students);
studentsMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cat.dao.StudentsMapper">
<insert id="addStudent" parameterType="com.cat.pojo.Students">
insert into students(stu_name,stu_sex,class_id,age)
values (#{stu_name},#{stu_sex},#{class_id},#{age})
test文件
import com.cat.dao.StudentsMapper;
import com.cat.pojo.Students;
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.IOException;
import java.io.InputStream;
public class MyBatisTest {
public void addStudent() throws IOException{
InputStream in = Resources.getResourceAsStream("MyBatisConfig.xml");
SqlSessionFactoryBuilder builder =new SqlSessionFactoryBuilder();
SqlSessionFactory factory=builder.build(in);
SqlSession session =factory.openSession();
StudentsMapper studentsMapper = session.getMapper(StudentsMapper.class);
Students s =new Students();
int res = studentsMapper.addStudent(s); //调用
session.commit(); //增删改必须提交事务
这样就可以增加一条学生信息了。
如果增加的信息中出现了乱码请点击这里参考。
(3)map
场景:传入参数符合 #{属性名} ,map中则是#{key} 。比如模糊查询,用户输入词,根据这个词进行模糊查询(实在想不到好例子了)
StudentsMapper:
import com.cat.pojo.Students;
public interface StudentsMapper {
Map <String, Object> findStudent( Map<String,Object> map);
studentsMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cat.dao.StudentsMapper">
<select id="findStudent" parameterType="map" resultType="map">
select * from students s where s.stu_name like '%${stu_name}%' and s.stu_sex like '%${stu_sex}%'
test文件
import com.cat.dao.StudentsMapper;
import com.cat.pojo.Students;
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.IOException;
import java.io.InputStream;
import java.util.HashMap;
public class MyBatisTest {
public void findStudent() throws IOException{
InputStream in = Resources.getResourceAsStream("MyBatisConfig.xml");
SqlSessionFactoryBuilder builder =new SqlSessionFactoryBuilder();
SqlSessionFactory factory=builder.build(in);
SqlSession session =factory.openSession();
StudentsMapper studentsMapper = session.getMapper(StudentsMapper.class);
Map<String,Object> map1= new HashMap<String, Object>();
map1.put("stu_name","张");
Map<String,Object> map2 =studentsMapper.findStudent(map1);
System.out.println(map2);
结果:
3.后记
文章中的例子不是特别恰当,后面整理增mybatis删改查的时候尽量详细一些。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具