MyBatis运行动态sql及存储过程
1 利用MyBatis进行模糊查询
在利用MyBatis进行模糊查询时候。须要用到concat,注意在Oracle中concat连接的是两个參数。不是三个參数。
持久化类:
package com.web.entity;public class Student {private Integer studentId;private String studentName;private Classes classes;public Integer getStudentId() {return studentId;}public void setStudentId(Integer studentId) {this.studentId = studentId;}public String getStudentName() {return studentName;}public void setStudentName(String studentName) {this.studentName = studentName;}public Classes getClasses() {return classes;}public void setClasses(Classes classes) {this.classes = classes;}}
dao中方法:
package com.web.dao;import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;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 com.web.entity.Student;public class StudentDao {private int res=-1;private SqlSession sqlSession=null;public SqlSession getSqlSession(){try{String resource="mybatis-config.xml";InputStream is=Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(is);sqlSession=sqlSessionFactory.openSession(true);}catch(Exception e){System.out.println("出错");e.printStackTrace();}return sqlSession;}public Student findTStudentByName(Student student){sqlSession=getSqlSession();Student s=sqlSession.selectOne("lk", student);return s;}/*public int getCount(Integer classesid){int result=-1;Map<String, Integer> map=new HashMap<String, Integer>();map.put("classesid", classesid);map.put("studentcount", -1);sqlSession=getSqlSession();sqlSession.selectOne("gc", map);result=map.get("studentcount");return result;}*/
}
映射文件:
<?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.pb.dao.StudentDao"> <!-- <resultMap type="Student" id="ts"> <id property="studentId" column="studentId" /> <result property="studentName" column="studentName" /> <association property="classes" column="classesId" javaType="Classes"> <id property="classesId" column="classesId" /> <result property="classesName" column="classesName" /> </association> </resultMap> <select id="selectonetoone" resultMap="ts" parameterType="Student"> select s.studentid,s.studentname,c.classesname from student s left join classes c on s.classesid=c.classesid where s.studentid=#{studentId} </select> --> <select id="lk" resultMap="ts" parameterType="Student"> select * from student s where s.studentname like concat(concat('%','h'),'%') </select> <!-- <select id="gc" parameterMap="gm" statementType="CALLABLE"> call pro_student(?,?
) </select> <parameterMap type="java.util.Map" id="gm"> <parameter property="classesid" mode="IN" jdbcType="INTEGER"/> <parameter property="studentcount" mode="OUT" jdbcType="INTEGER"/> </parameterMap> --> </mapper>
2 利用MyBatis运行存储过程:
在映射文件里定义一个parameterMap作为參数,设置statementType="CALLABLE",过程的參数放置?站位符。
在方法中也创建一个Map。放置參数。SqlSession调用方法的时候传入该Map就可以。
存储过程:
create or replace procedure pro_student(
cid in number,
coun out number
)is
begin
select count(*) into coun from student s where s.classesid=cid;
end;
配置文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.pb.dao.StudentDao">
<!-- <resultMap type="Student" id="ts"><id property="studentId" column="studentId" /><result property="studentName" column="studentName" /><association property="classes" column="classesId"javaType="Classes"><id property="classesId" column="classesId" /><result property="classesName" column="classesName" /></association></resultMap><select id="selectonetoone" resultMap="ts" parameterType="Student">selects.studentid,s.studentname,c.classesnamefromstudent sleft join classes con s.classesid=c.classesidwhere s.studentid=#{studentId}</select> --><select id="lk" resultMap="ts" parameterType="Student">select * from student s where s.studentname like concat(concat('%','h'),'%')</select><select id="gc" parameterMap="gm" statementType="CALLABLE">call pro_student(?,?)</select><parameterMap type="java.util.Map" id="gm"><parameter property="classesid" mode="IN" jdbcType="INTEGER"/><parameter property="studentcount" mode="OUT" jdbcType="INTEGER"/></parameterMap></mapper>
dao中方法:
package com.web.dao;import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;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 com.web.entity.Student;public class StudentDao {private int res=-1;private SqlSession sqlSession=null;public SqlSession getSqlSession(){try{String resource="mybatis-config.xml";InputStream is=Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(is);sqlSession=sqlSessionFactory.openSession(true);}catch(Exception e){System.out.println("出错");e.printStackTrace();}return sqlSession;}public Student findTStudentByName(Student student){sqlSession=getSqlSession();Student s=sqlSession.selectOne("lk", student);return s;}public int getCount(Integer classesid){int result=-1;Map<String, Integer> map=new HashMap<String, Integer>();map.put("classesid", classesid);map.put("studentcount", -1);sqlSession=getSqlSession();sqlSession.selectOne("gc", map);result=map.get("studentcount");return result;}
}
转载于:https://www.cnblogs.com/jzdwajue/p/6924053.html
MyBatis运行动态sql及存储过程相关推荐
- MyBatis(三)——动态SQL
文章目录 1. 简介 2. 搭建环境 2.1 在MySQL中创建blog表 2.2 编写实体类 2.3 编写实体类对应Mapper接口 2.4 编写Mapper接口对应的Mapper.xml文件 2. ...
- java mysql 动态sql_Java下拼接运行动态SQL语句
Java拼接动态SQL的一般做法有 1.使用动态语句 非常多数据库都提供了处理动态SQL的语法,如Oracle的EXECUTE IMMEDIATE语句.MSSQL的EXEC和SP_EXECUTESQL ...
- MyBatis的动态SQL详解
MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑. MyBatis中用于实现动态SQL的元素主要有: if choose(when,otherwis ...
- Mybatis解析动态sql原理分析
前言 废话不多说,直接进入文章. 我们在使用mybatis的时候,会在xml中编写sql语句. 比如这段动态sql代码: <update id="update" parame ...
- MyBatis中动态sql实现时间范围比较的查询
场景 前端传递两个时间参数,开始时间和结束时间,然后从数据库中筛选出某个时间属性在此范围的数据. Mybatis的动态sql的写法. 注: 博客: https://blog.csdn.net/bada ...
- MyBatis中动态sql的模糊搜索、foreach实现In集合的用法
场景 在使用MyBatis的动态sql时,常见的是传递一个ID的数组,查询记录的 ID在这个数组中的记录和模糊搜索这两种场景. 注: 博客: https://blog.csdn.net/badao_l ...
- mybatis的动态sql的一些记录
动态sql的作用:传统的使用JDBC的方法,相信大家在组合复杂的的SQL语句的时候,需要去拼接,稍不注意哪怕少了个空格,都会导致错误.Mybatis的动态SQL功能正是为了解决这种问题, 其通过 if ...
- 9、mybatis中动态sql的使用
对于初学者,如何进行mybatis的学习呢?我总结了几点,会慢慢的更新出来.首先大家需要了解mybatis是什么.用mybatis来做什么.为什么要用mybatis.有什么优缺点:当知道了为什么的时候 ...
- 利用MyBatis的动态SQL特性抽象统一SQL查询接口
1. SQL查询的统一抽象 MyBatis制动动态SQL的构造,利用动态SQL和自定义的参数Bean抽象,可以将绝大部分SQL查询抽象为一个统一接口,查询参数使用一个自定义bean继承Map,使用映射 ...
最新文章
- Docker安装(Mac)
- 设置在桌面上不显示计算机,怎么在桌面上显示我的电脑 我的电脑桌面不显示怎么办...
- dbconn Java_DbConnection.java实现简单的MySQL数据库连接
- Dockerfile详解(二)
- AsyncTask的使用半解--!
- HDU 6127 Hard challenge(极角 + 二分)
- arm linux gif 显示_100ASK_IMX6ULL arm板子如何显示图片、汉字、划线、背景色
- 女同志50岁退休可以早领养老金,55岁退休拿的养老金比较高,哪个更划算?
- 这场直播,我们把 Apache 顶级项目盛会搬来了!
- php图片发送另一台,PHP从一台服务器将图片发送到另一台服务器并保存
- 软件工程的经典书籍有哪些?
- 这5个开源和免费静态代码分析工具,你一个都没有用过吗?不会吧
- java web 自己开发NAS个人网盘、视频网站视频播放服务端大视频文件处理逻辑解决方案和思路
- Lua踩坑记录(持续更新)
- 语句摘抄——第22周
- ファイルダウンロード
- 管廊复杂网格参数化算法及其大数据在线渲染
- MeiShe Face Sticker Design Plugin AR Scene Editor Instruction
- Excel对比两行数据是否相同?
- 国内市场主流音视频产品主要功能分析
热门文章
- python 去除panda安装包_沉淀,再出发:python中的pandas包
- 负载均衡轮询算法和服务器性能,负载均衡算法
- 求向量的垂线_高考数学填空题如何快速求直线关于直线对称的直线方程
- 阵列函数 java_Java复制阵列– Java中的阵列复制
- r语言中paste函数_R中的paste()函数-简要指南
- wordpress主题_2014年十大免费WordPress主题
- swift 的init_Swift init()
- Android AlarmManager广播接收器和服务
- 开课吧Java教程之什么是Arrays
- Java基础篇:switch语句