当前位置: 首页> 教育> 培训 > 微信商城首页_域名查询购买_竞价托管收费标准_百度sem竞价推广电子书

微信商城首页_域名查询购买_竞价托管收费标准_百度sem竞价推广电子书

时间:2025/7/13 0:37:32来源:https://blog.csdn.net/gege_0606/article/details/142894185 浏览次数:2次
微信商城首页_域名查询购买_竞价托管收费标准_百度sem竞价推广电子书

  在MyBatis中,参数处理是非常关键的部分,它负责将传入的参数正确映射到SQL语句中 

单个简单类型参数

简单类型对于mybatis来说都是可以自动类型识别的:

  • 也就是说对于mybatis来说,它是可以自动推断出ps.setXxxx()方法的。ps.setString()还是ps.setInt()。它可以自动推断。

简单类型包括:

  • byte short int long float double char

  • Byte Short Integer Long Float Double Character

  • String

  • java.util.Date

  • java.sql.Date

参数自动绑定
在执行 SQL 时,MyBatis 会根据参数的类型自动绑定到 PreparedStatement 对应的 setXxx() 方法。
示例:

public interface UserMapper {User getUserById(int id);
}

SQL 映射文件:

<select id="getUserById" resultType="User">SELECT * FROM user WHERE id = #{id}
</select>

在执行时,MyBatis 自动判断 idint 类型,因此会使用 PreparedStatement.setInt() 方法。

多个参数处理(使用@Param注解)

如果一个方法有多个参数,你可以使用 @Param 注解给参数命名,方便在 SQL 中引用:

示例:

   /*** 根据name和age查询* @param name* @param age* @return*/List<Student> selectByNameAndAge(@Param(value="name") String name, @Param("age") int age);

 value值可以省略不写

SQL映射文件:

    <select id="selectByNameAndAge" resultType="student">select * from t_student where name = #{name} and age = #{age}</select>

Java对象作为参数

查找:

你也可以将 Java 的对象传递给 MyBatis 方法,MyBatis 会自动将对象的属性与 SQL 中的字段进行映射:

public interface UserMapper {User getUserByObject(User user);
}

SQL映射文件:

<select id="getUserByObject" resultType="User">SELECT * FROM user WHERE name = #{name} AND age = #{age}
</select>

这里 #{name}#{age} 会自动对应 User 对象中的 nameage 属性。

使用pojo类保存数据 :

/*** 保存学生数据* @param student 实体类pojo* @return*/int insertPojo(Student student);

sql映射文件:

<!--    pojo#{}里面写的是属性名--><insert id="insertPojo" parameterType="mybatis.pojo.Student">insert into t_student (id, name, age, sex, birth, height)values (#{id}, #{name}, #{age}, #{sex}, #{birth}, #{height})</insert>

 测试类:

 @Testpublic void testInsertPojo(){Student student = new Student("小李子",23,1.67,new Date(),'男');SqlSession sqlSession = SqlSessionUtil.openSession();StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);mapper.insertPojo(student);sqlSession.commit();sqlSession.close();}

易错:

注意这里我的student类中存在不含有id的构造方式(也就是构造方式的重载),因为我id设置了自增主键,所以可以不需要指定id值,这里不能传入null会报错!!! 

Map 作为参数 

查找 

/**
* 根据name和age查询
* @param paramMap
* @return
*/
List<Student> selectByParamMap(Map<String,Object> paramMap);

测试类: 

@Test
public void testSelectByParamMap(){// 准备MapMap<String,Object> paramMap = new HashMap<>();paramMap.put("nameKey", "张三");paramMap.put("ageKey", 20);List<Student> students = mapper.selectByParamMap(paramMap);students.forEach(student -> System.out.println(student));
}

sql映射文件:

<select id="selectByParamMap" resultType="student">select * from t_student where name = #{nameKey} and age = #{ageKey}
</select>

这种方式是手动封装Map集合,将每个条件以key和value的形式存放到集合中。然后在使用的时候通过#{map集合的key}来取值。

通过map来保存数据到表格中

  /*** 保存学生数据* @param paramMap 使用map进行传参*/void insertStudent(Map<String, Object> paramMap);

 sql映射文件:

<!--这里设置自增主键(创建表格时也需要声明是自增主键),id值就可以传null值了,否则会报错--><insert id="insertStudent" parameterType="map" useGeneratedKeys="true" keyProperty="id">insert into t_student (id, name, age, sex, birth, height)values (null,#{name}, #{age}, #{sex}, #{birth}, #{height})</insert>

java测试类: 

    public void testInsertMapParam() {// 准备 MapMap<String, Object> paramMap = new HashMap<>();paramMap.put("name", "张三");paramMap.put("age", 20);paramMap.put("sex", '女'); paramMap.put("birth", new java.sql.Date(new Date().getTime()));  // 使用 java.sql.DateparamMap.put("height", 1.85);SqlSession sqlSession = SqlSessionUtil.openSession();StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);mapper.insertStudent(paramMap);sqlSession.commit();sqlSession.close();}

易错点:

如果不设置自增主键而且sql映射文件上面id值为null就会报错!!所以一定要记得设置自增主键

关键字:微信商城首页_域名查询购买_竞价托管收费标准_百度sem竞价推广电子书

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: