mybatis如何操作text类型(mysql)数据呢?

  • Post author:
  • Post category:mysql



Mybatis在预处理语句中设置一个参数,或者从结果集中获取一个值时,会使用类型处理器typeHandler将获取的值以合适的方式转换成Java类型。数据库中的基本类型之所以能被转化成JAVA类型,是因为Mybatis已经内置了这些类型的处理器,


另外。Mybatis同事提供了类型处理器的扩展功能,程序可以自定义类型处理器,或者替换内置的类型处理器,只需集成TypeHandler借口即可,然后再XML配置文件配置一下。


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58


import


java.sql.CallableStatement;



import


java.sql.PreparedStatement;



import


java.sql.ResultSet;



import


java.sql.SQLException;



import


org.apache.ibatis.type.JdbcType;



import


org.apache.ibatis.type.TypeHandler;



import


com.alibaba.fastjson.JSON;





public


class


JSONHandler


implements


TypeHandler<Object> {






/**





* json数据和类名的分隔符号





* */





private


static


final


char


SPLIT =


'/'


;





public


void


setParameter(PreparedStatement ps,


int


i, Object parameter,





JdbcType jdbcType)


throws


SQLException {






if


(parameter ==


null


){






ps.setString(i,


null


);





return


;





}





String json = JSON.toJSONString(parameter);





json  = json + SPLIT + parameter.getClass().getName();





ps.setString(i, json);





}





public


Object getResult(ResultSet rs, String columnName)





throws


SQLException {






String  json = rs.getString(columnName);





return


jsonToObject(json);





}





public


Object getResult(CallableStatement cs,


int


columnIndex)





throws


SQLException {






String  json = cs.getString(columnIndex);





return


jsonToObject(json);





}






/**





* json 转换成对象





* */





private


Object jsonToObject(String json){






if


(json ==


null


){






return


null


;





}





int


index = json.lastIndexOf(SPLIT);





if


(index  <


0


){





return


null


;





}





String key = json.substring(index +


1


, json.length());





json   = json.substring(


0


, index);





Class<?> cls =


null


;





try


{






cls = Class.forName(key);





}


catch


(ClassNotFoundException e) {






throw


new


RuntimeException(


"序列化成json时找不到指定的类"


, e);





}





Object ob = JSON.parseObject(json, cls);





return


ob;





}


}


1

给你一段例子代码。看看


1

2

3

4

5

6

7

8


<


typeHandlers


>





<


typeHandler


javaType


=


"com.jianbai.learn.ibatis.domain.UserExtDO"


jdbcType


=


"TEXT"





handler


=


"com.jianbai.learn.ibatis.handler.JSONHandler"


/>





<


typeHandler


javaType


=


"java.util.Map"


jdbcType


=


"TEXT"





handler


=


"com.jianbai.learn.ibatis.handler.JSONHandler"


/>





<


typeHandler


javaType


=


"java.util.List"


jdbcType


=


"TEXT"





handler


=


"com.jianbai.learn.ibatis.handler.JSONHandler"


/>



</


typeHandlers


>


转载:http://www.oschina.net/question/615983_81328?fromerr=DTEWGH7N







mybatis-generator 无法自动生成字段类型为text的属性


解决思路: <columnOverride column=”your column” jdbcType=”VARCHAR” />




The fully qualified Java Type of the property for this column. This can be used to override the type calculated by the


JavaTypeResolver


if required. For some databases, this is necessary to handle “odd” database types (e.g.

MySQL

‘s unsigned bigint type should be mapped to java.lang.Object).


转载:http://blog.csdn.net/javaious/article/details/20557019