package TestMapper;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import net.sf.json.JSON;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;
import com.platform.common.json.JSONArray;
import com.platform.common.json.JSONObject;
import com.platform.common.json.JSONWriter;
import com.platform.data.DataPreparedStatementQuery;
import bean.User;
import Mapper.UserMapper;
import Util.MyBatisUtil;
public class TestMapper {
static SqlSessionFactory sqlSessionFactory = null;
static {
sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();
}
// @Test
// public void testAdd() {
// SqlSession sqlSession = sqlSessionFactory.openSession();
// try {
// UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
// User user = new User("yy", new Integer(5));
// userMapper.insertUser(user);
// sqlSession.commit();// 这里一定要提交,不然数据进不去数据库中
// } finally {
// sqlSession.close();
// }
// }
//
// @Test
// public void getUser() {
// SqlSession sqlSession = sqlSessionFactory.openSession();
// try {
// UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
// User user = userMapper.getUser("eee");
// System.out.println("name: " + user.getName() + "|age: "
// + user.getAge());
//
// } finally {
// sqlSession.close();
// }
// }
/**
* 测试返回HashMap,多条数据
*/
@SuppressWarnings("unchecked")
@Test
public void testSelectBlogRetHashMap() {
SqlSession session = sqlSessionFactory.openSession();
List<HashMap<String, Object>> User = (List<HashMap<String, Object>>) session
.selectList("Mapper.UserMapper.selectUserRetHashMap", 11);
session.close();
// System.out.println(User.get(0));
JSONObject jsobj = new JSONObject();
String str ="",str1="",str2="";
for (int i = 0; i < User.size(); i++) {
HashMap<String, Object> parameters = User.get(i);
for (Iterator<?> iter = parameters.entrySet().iterator(); iter.hasNext();) {
Entry<?, ?> element = (Entry<?, ?>) iter.next();
Set set = User.get(i).entrySet();
Iterator it = set.iterator();
while (it.hasNext()) {
Map.Entry me = (Map.Entry) it.next();
jsobj.put(me.getKey().toString(), me.getValue());
}
}
//System.out.println(jsobj);
//System.out.println(jsobj.length());
str += jsobj.get("NAME").toString()+"#,#";
str1 += jsobj.get("AGE").toString()+"#,#";
str2 += jsobj.get("EMAIL").toString()+"#,#";
}
str+="";
//System.out.println(str1);
int rowcount = User.size();
jsobj.put("rowcount", rowcount);
jsobj.put("NAME", str);
jsobj.put("AGE", str1);
jsobj.put("EMAIL", str2);
System.out.println(jsobj);
// for (Map<String, Object> map : User) {
// for (Entry<String, Object> entry : map.entrySet()) {
// // System.out.println(entry.getKey() + " " + entry.getValue());
//
// }
// }
}
}
package bean;
public class User {
private String name;
private Integer age;
private String email;
public String getName() {
return name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public User(String name, Integer age) {
super();
this.name = name;
this.age = age;
}
public User() {
super();
}
}
package Mapper;
import bean.User;
public interface UserMapper {
public void insertUser(User user);
public User getUser(String name);
}
<?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="Mapper.UserMapper">
<!-- 这里namespace必须是UserMapper接口的路径,不然要运行的时候要报错 “is not known to the MapperRegistry” -->
<insert id="insertUser" parameterType="User">
insert into vincent_user(name,age,email)
values(#{name},#{age},#{email})
<!-- 这里sql结尾不能加分号,否则报“ORA-00911”的错误 -->
</insert>
<!-- 这里的id必须和UserMapper接口中的接口方法名相同,不然运行的时候也要报错 -->
<select id="getUser" resultType="User" parameterType="java.lang.String">
select *
from vincent_user where name=#{name}
</select>
<select id="selectUserRetHashMap" parameterType="int"
resultType="map">
select * from vincent_user t where age=#{age}
</select>
</mapper>
<?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>
<settings>
<!-- changes from the defaults for testing -->
<setting name="cacheEnabled" value="false" />
<setting name="useGeneratedKeys" value="true" />
<setting name="defaultExecutorType" value="REUSE" />
</settings>
<typeAliases>
<typeAlias alias="User" type="bean.User"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="jdbc"/>
<dataSource type="POOLED">
<property name="driver" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@10.1.2.133:1521:test"/>
<property name="username" value="scott"/>
<property name="password" value="tiger"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="Mapper/UserMapper.xml" />
</mappers>
</configuration>