mybatis中的sql映射文件(2)-parameterType
作者:互联网
原文链接:这里
0.前言
前面的文章介绍resultType,除了resultType外,我们经常用到的还有parameterType。parameterType是参数类型,就是我们传递给数据库的这个参数的类型,
1.传入类型
- 基本数据类型:int、string、Date等基本数据类型都可以
- 类(JavaBean、List): (没有太好的例子,所以归为一类)
- Map
注意:
parameterType在查询全部方法不需要这个参数,也就是没有
parameterType添加返回的返回的是****实体类的全类名
parameterType在更新方法里,这个参数返回的也是实体类的全类名
parameterType在删除方法里,这个参数返回的也是实体类的Integer,就是成功影响一条数据的记录表示成功
2.例子
(1)基本类型
场景:比如我要根据学生的id去删除一条数据,那么我就要传入这个学生的id。
StudentsMapper文件
package com.cat.dao; import com.cat.pojo.Students; import java.util.List; import java.util.Map; public interface StudentsMapper { //所有学生 List<Students> listAllStudents(); //删除一名学生信息 int deleteStudent(int id); }Mapper.xml
<?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.cat.dao.StudentsMapper"> <select id="listAllStudents" resultType="com.cat.pojo.Students"> select * from students; </select> <!--删除一条学生信息--> <delete id="deleteStudent" parameterType="int"> delete from students where id = #{id} </delete> </mapper>test文件
package com.cat.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 org.junit.Test; import java.io.IOException; import java.io.InputStream; import java.util.List; import java.util.Map; public class MyBatisTest { @Test public void test() throws IOException{ //获得核心配置文件 InputStream in = Resources.getResourceAsStream("MyBatisConfig.xml"); //获得session工厂对象 SqlSessionFactoryBuilder builder =new SqlSessionFactoryBuilder(); SqlSessionFactory factory=builder.build(in); SqlSession session =factory.openSession(); //获得会话对象 List<Students> students =session.selectList("listAllStudents"); System.out.println(students); session.close(); //关闭 } //测试删除一名学生信息 @Test public void deleteStudent() throws IOException{ //获得核心配置文件 InputStream in = Resources.getResourceAsStream("MyBatisConfig.xml"); //获得session工厂对象 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的这个学生 session.close(); //关闭 } }结果:这样运行之后就完成了根据id删除一条信息的功能。
(2)类(JavaBean、List)
场景:我要新增一个学生的信息,就需要传入这个学生的整体信息。
注:我因为没有好的例子,所以把JavaBean和List都放这,实际用法不完全一样。
StudentsMapper:
package com.cat.dao; import com.cat.pojo.Students; import java.util.List; import java.util.Map; public interface StudentsMapper { //新增一名学生 int addStudent(Students students); }studentsMapper.xml
<?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.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}) </insert> </mapper>test文件
package com.cat.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 org.junit.Test; import java.io.IOException; import java.io.InputStream; import java.util.List; import java.util.Map; public class MyBatisTest { //新增一名学生信息 @Test public void addStudent() throws IOException{ //获得核心配置文件 InputStream in = Resources.getResourceAsStream("MyBatisConfig.xml"); //获得session工厂对象 SqlSessionFactoryBuilder builder =new SqlSessionFactoryBuilder(); SqlSessionFactory factory=builder.build(in); SqlSession session =factory.openSession(); //获得会话对象 StudentsMapper studentsMapper = session.getMapper(StudentsMapper.class); //构造一个新的学生 Students s =new Students(); s.setStu_name("呵呵"); s.setClass_id(1042); s.setAge(16); s.setStu_sex("男"); int res = studentsMapper.addStudent(s); //调用 session.commit(); //增删改必须提交事务 session.close(); //关闭 } }这样就可以增加一条学生信息了。
(3)map
场景:传入参数符合 #{属性名} ,map中则是#{key} 。比如模糊查询,用户输入词,根据这个词进行模糊查询(实在想不到好例子了)
StudentsMapper:
package com.cat.dao; import com.cat.pojo.Students; import java.util.List; import java.util.Map; public interface StudentsMapper { //测试输入参数map Map <String, Object> findStudent( Map<String,Object> map); }studentsMapper.xml
<?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.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}%' </select> </mapper>test文件
package com.cat.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 org.junit.Test; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.List; import java.util.Map; public class MyBatisTest { //测试输入参数是map @Test public void findStudent() throws IOException{ //获得核心配置文件 InputStream in = Resources.getResourceAsStream("MyBatisConfig.xml"); //获得session工厂对象 SqlSessionFactoryBuilder builder =new SqlSessionFactoryBuilder(); SqlSessionFactory factory=builder.build(in); SqlSession session =factory.openSession(); //获得会话对象 StudentsMapper studentsMapper = session.getMapper(StudentsMapper.class); //需求是找一个姓张的男性。 //输入参数的map Map<String,Object> map1= new HashMap<String, Object>(); map1.put("stu_name","张"); map1.put("stu_sex","男"); //返回结果的map,也可以用list等 Map<String,Object> map2 =studentsMapper.findStudent(map1); System.out.println(map2); session.close(); //关闭 } }结果:
3.后记
文章中的例子不是特别恰当,后面整理增mybatis删改查的时候尽量详细一些。
标签:parameterType,java,StudentsMapper,org,session,sql,mybatis,import,com 来源: https://www.cnblogs.com/longkui-site/p/15858442.html