当前位置: 首页 > news >正文

漂亮产品网站绍兴seo推广公司

漂亮产品网站,绍兴seo推广公司,cms开源系统,网站建设关键词优化价格在MyBatis中,参数处理是非常关键的部分,它负责将传入的参数正确映射到SQL语句中 单个简单类型参数 简单类型对于mybatis来说都是可以自动类型识别的: 也就是说对于mybatis来说,它是可以自动推断出ps.setXxxx()方法的。ps.setSt…

  在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就会报错!!所以一定要记得设置自增主键

http://www.ds6.com.cn/news/11800.html

相关文章:

  • 海南的房产网站建设百度网盘官方网站
  • 个人微信支付宝做购物网站成功的营销案例及分析
  • 网页设计与网站建设第06章在线测试江苏企业网站建设
  • wordpress关键字过滤神马seo服务
  • 常用网站png关键词排名工具有哪些
  • 做电影网站为什么查封不了网络营销案例ppt
  • 学校网站框架交换友情链接
  • 做网站用dramwaver还是vs线上拓客渠道有哪些
  • 怎么保证网站安全性整站seo免费咨询
  • 苏州模板建站哪家好写软文能赚钱吗
  • 北京海淀财政局网站东莞网站制作
  • 个人做网站广州百度网站排名优化
  • 互联网个人用户网站重庆seo顾问
  • 小语种网站怎么做长沙seo优化首选
  • 做6个页面的网站营销怎么做
  • 泰州网站建设网站关键词排名优化软件
  • 长春网站建设电话seo常用工具有哪些
  • 有什么可以做cad赚钱的网站竞价推广课程
  • 在哪可以找到做网站的常见的搜索引擎
  • 中怎么做网站上下载图片的功能阳西网站seo
  • 仿站小工具下载危机公关
  • wordpress wamp济南seo排名优化推广
  • 优秀的门户网站现在什么app引流效果好
  • 泰坦科技网站建设网站搜索排名优化软件
  • 中英文企业网站模板成人厨师短期培训班
  • 重庆做商城网站建设游戏行业seo整站优化
  • 网站设计销售最近的热点新闻
  • 专注大连网站建设怎么让百度收录
  • 网站开发的策划书南召seo快速排名价格
  • wordpress文章显示时间长沙seo培训班