<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"> <sqlMapConfig> <settings useStatementNamespaces="true"/> <typeHandler javaType="edu.sdust.xujsh.test.type.ChineseString" callback="edu.sdust.xujsh.test.type.handler.CnStringTypeHandler" /><!--注意这里的自定义类型处理器--> <!-- 使用JDBC的事务管理 --> <transactionManager type="JDBC"> <!-- 数据源 --> <dataSource type="SIMPLE"> <property name="JDBC.Driver" value="com.mysql.jdbc.Driver" /> <property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost:3306/test" /> <property name="JDBC.Username" value="root" /> <property name="JDBC.Password" value="123456" /> </dataSource> </transactionManager> <!-- 这里可以写多个实体的映射文件 --> <sqlMap resource="User.xml" /> </sqlMapConfig>User.xml:
<?xml version="1.0" encoding="GB2312"?> <!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://www.ibatis.com/dtd/sql-map-2.dtd"> <sqlMap namespace="User"> <typeAlias alias="user" type="com.xujsh.test.dto.User"/> <resultMap id="userResult" class="user"> <result property="id" column="id" jdbcType="DECIMAL"/> <result property="name" column="name" jdbcType="VARCHAR"/> <result property="birth" column="birth" jdbcType="TIMESTAMP"/> </resultMap> <insert id="insertUser" parameterClass="user"><![CDATA[ insert into USER (id,name,birth) values (#id#,#name#,#birth#) ]]></insert> <select id="getUserByUserId" resultMap="userResult" parameterClass="int"><![CDATA[ select id,name,birth from USER where id = #id# ]]></select> </sqlMap>
public class ChineseString { private String value;//简单的对原始的字符串做一个包装 public ChineseString(){} public ChineseString(String value){ this.value = value; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } public String toString() { return value; } }edu.sdust.xujsh.test.type.handler.CnStringTypeHandler.java:
public class CnStringTypeHandler implements TypeHandlerCallback { // 中文数据被保存到数据库所使用的字符集 private static final String STORE_CHARSET = "GBK"; // 系统使用的字符集 private String systemEncoding = "iso-8859-1"; /** * 从数据库中取数据 */ public Object getResult(ResultGetter getter) throws SQLException { String value = getter.getString(); if (value == null) { return null; } else { try { return new ChineseString(new String(value.getBytes(systemEncoding), STORE_CHARSET)); } catch (UnsupportedEncodingException ue) { return value; } } } /** * 往数据库写数据 */ public void setParameter(ParameterSetter setter, Object parameter) throws SQLException { if (parameter != null) { ChineseString value = (ChineseString) parameter; if (value.getValue() != null) { try { setter.setString(new String(value.getValue().getBytes(STORE_CHARSET), systemEncoding)); } catch (UnsupportedEncodingException ue) { setter.setString(value.getValue()); } return; } } setter.setNull(Types.VARCHAR); } public Object valueOf(String s) { return s; } }com.xujsh.test.dto.User.java:
public class User { private int id; private ChineseString name; private Date birth; public int getId() { return id; } public void setId(int id) { this.id = id; } public ChineseString getName() { return name; } public void setName(ChineseString name) { this.name = name; } public Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", birth=" + birth + "]"; } }com.xujsh.test.client.Main.java:
public class Main { private static SqlMapClient sqlMapClient = null; // 读取配置文件 static { try { InputStream in = Main.class.getClassLoader().getResourceAsStream("sqlmap-config.xml"); sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(in); in.close(); } catch (IOException e) { e.printStackTrace(); } } public static User selectUserById(int id) throws Exception { return (User) sqlMapClient.queryForObject("User.getUserByUserId", id); } public static void insertUser(User user)throws Exception { sqlMapClient.insert("User.insertUser", user); } public static void main(String[] args) throws Exception { User u = new User(); u.setId(123456); u.setName(new ChineseString("中国")); u.setBirth(new Date()); Main.insertUser(u); u = Main.selectUserById(u.getId()); System.out.println(u); } }
http://blog.csdn.net/lovingprince/article/details/2768849
http://www.cnblogs.com/doit8791/archive/2012/05/28/2522556.html
源码下载地址:http://download.csdn.net/detail/goldenfish1919/6403209