接上篇文章:mybatis3.2.7事务查询和插入例子
首先定义一个MySQL存储过程:
DROP PROCEDURE IF EXISTS test ;
CREATE PROCEDURE test (IN p1 VARCHAR(26), OUT pResult VARCHAR(512))
BEGIN
SET pResult := NULL;
SET pResult :=CONCAT ( 'test',p1);
SELECT * FROM tb2 WHERE commet like Concat('%',p1, '%'); -- this 返回一个结果集
END;
tb2的表结构和数据如下:
DROP TABLE IF EXISTS `tb2`;
CREATE TABLE `tb2` (
`t_id` int(10) NOT NULL AUTO_INCREMENT COMMENT '主键',
`Name` varchar(255) DEFAULT NULL COMMENT '字段名',
`type` int(1) DEFAULT NULL COMMENT '类型,0-正常,1-异常,2-传输,3-退单',
`commet` varchar(255) DEFAULT NULL COMMENT '备注',
`optime` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '操作时间',
PRIMARY KEY (`t_id`)
) ;
继续增加新的类:
Stb2.java内容:
package com.springdemo.usermgr.vo;
import java.util.Date;
/**
* tb2表实体类
* @author zhouxj
* @date 2014-09-10 下午03:29:32
*/
public class Stb2{
private Integer t_id;
private String name;
private Integer type;
private String commet;
private java.util.Date optime;
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getType() {
return type;
}
public void setType(Integer type) {
this.type = type;
}
public String getCommet() {
return commet;
}
public void setCommet(String commet) {
this.commet = commet;
}
public java.util.Date getOptime() {
return optime;
}
public void setOptime(java.util.Date optime) {
this.optime = optime;
}
}
修改SUserMapper.java内容:
package com.springdemo.usermgr.vo;
import java.util.List;
import java.util.Map;
public interface SUserMapper {
public int insertSUser(SUser user);
public SUser getSUser(String name);
public List<Stb2> getTestProc(Map<String, Object> param);
}
Test2测试类内容:
package domain;
import java.io.IOException;
import java.io.Reader;
import java.util.HashMap;
import java.util.List;
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.springdemo.usermgr.vo.SUserMapper;
import com.springdemo.usermgr.vo.Stb2;
public class Test2 {
public static void main(String[] args) throws IOException {
String resource = "config.xml";
Reader reader = Resources.getResourceAsReader(resource);
SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = ssf.openSession(true); //true 为自动提交事务
try {
Map<String, Object> parms = new HashMap<String, Object>();
parms.put("queryStr", "的");
SUserMapper spMapper = session.getMapper(SUserMapper.class);
List<Stb2> as=spMapper.getTestProc(parms);
String outPara=(String) parms.get("retStr");
System.out.println(outPara);
System.out.println(as.toString());
//org.apache.ibatis.type.JdbcType.VARCHAR
// org.apache.ibatis.mapping.ParameterMode.OUT
} catch (Exception e) {
e.printStackTrace();
} finally {
session.close();
}
}
}
config.xml配置文件修改内容:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC
"-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<typeAlias alias="SUser" type="com.springdemo.usermgr.vo.SUser" />
<typeAlias alias="Stb2" type="com.springdemo.usermgr.vo.Stb2" />
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root" />
<property name="password" value="pass" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="SUser.xml" />
<mapper resource="Stb2.xml" />
</mappers>
</configuration>
增加配置文件Stb2.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.springdemo.usermgr.vo.SUserMapper">
<resultMap type="com.springdemo.usermgr.vo.Stb2" id="itemResult">
</resultMap>
<select id="getTestProc" parameterType="java.util.Map" statementType="CALLABLE"
resultMap="itemResult">
{call test.test(
#{queryStr,jdbcType=VARCHAR,mode=IN},
#{retStr,jdbcType=VARCHAR,mode=OUT})
}
</select>
</mapper>
运行Test2类,可能的结果:
test的
[com.springdemo.usermgr.vo.Stb2@6900bf61, com.springdemo.usermgr.vo.Stb2@3014af22]
返回了参数内容,和两条记录。