首页  

Mybatis常用工具类     所属分类 mybatis 浏览量 830
org.apache.ibatis.jdbc.SQL   构造 sql  
org.apache.ibatis.jdbc.ScriptRunner 执行脚本
org.apache.ibatis.jdbc.SqlRunner    操作数据库

MetaObject 获取和设置对象的属性值
MetaClass  获取类相关的信息

ObjectFactory   创建结果对象
ProxyFactory    懒加载 代理对象生成


String newSql = new SQL() .INSERT_INTO("PERSON") .INTO_COLUMNS("ID", "NAME") .INTO_VALUES("#{id}", "#{name}") .toString(); org.apache.ibatis.jdbc.ScriptRunner 执行脚本 org.apache.ibatis.jdbc.SqlRunner 操作数据库 Connection connection = DriverManager.getConnection("jdbc:hsqldb:mem:testdb001","sa", ""); ScriptRunner scriptRunner = new ScriptRunner(connection); scriptRunner.runScript(Resources.getResourceAsReader("init.sql")); Map<String, Object> selectOne(String sql, Object... args) 查询行数不为1时,抛 SQLException 异常 List<Map<String, Object>> selectAll(String sql, Ojbect... args) insert(String sql, Object... args) update(String sql, Object... args) delete(String sql, Object... args) run(String sql) 执行 SQL 语句,没有占位符 closeConnection() 关闭Connection对象 SqlRunner sqlRunner = new SqlRunner(connection); String sql = new SQL() .SELECT("*") .FROM("person") .WHERE("person_id = ?") .toString(); Map<String, Object> resultMap = sqlRunner.selectOne(sql, 1); System.out.println(resultMap);
Person person = new Person() MetaObject metaObject = SystemMetaObject.forObject(person); metaObject.hasGetter("name") metaObject.getGetterNames() metaObject.getGetterType("name") MetaClass metaClass = MetaClass.forClass(Person.class, new DefaultReflectorFactory()); metaClass.hasDefaultConstructor() Invoker invoker = metaClass.getGetInvoker("name"); Object name = invoker.invoke(person, null); Invoker invoker = metaClass.getSetInvoker("name"); invoker.invoke(person, new String[]{"cat"});
ObjectFactory 默认实现 DefaultObjectFactory create(type) 处理无参构造方法 create(type, constructorArgTypes, constructorArgs) 处理有参构造方法 setProperties(properties) 设置配置属性 isCollection(type) 判断是否为集合类型 ObjectFactory objectFactory = new DefaultObjectFactory(); Person person = objectFactory.create(Person.class); ObjectFactory objectFactory = new DefaultObjectFactory(); Person person = objectFactory.create(Person.class, Arrays.asList(Integer.class, String.class), Arrays.asList(1, "tiger")); ProxyFactory JavassistProxyFactory CglibProxyFactory org.apache.ibatis.executor.loader.ProxyFactory public interface ProxyFactory { default void setProperties(Properties properties) { // NOP } Object createProxy(Object target, ResultLoaderMap lazyLoader, Configuration configuration, ObjectFactory objectFactory, List< Class> constructorArgTypes, List< Object> constructorArgs); }
测试代码 import java.sql.Connection; import java.sql.DriverManager; import java.util.Date; import java.util.Map; import org.apache.commons.lang3.StringUtils; import org.apache.ibatis.io.Resources; import org.apache.ibatis.jdbc.SQL; import org.apache.ibatis.jdbc.ScriptRunner; import org.apache.ibatis.jdbc.SqlRunner; import org.apache.ibatis.reflection.DefaultReflectorFactory; import org.apache.ibatis.reflection.MetaClass; import org.apache.ibatis.reflection.MetaObject; import org.apache.ibatis.reflection.SystemMetaObject; import org.apache.ibatis.reflection.invoker.Invoker; import dyyx.data.UserDTO; public class UtilTest { public static void main(String[] args) throws Exception { System.out.println("hello,UtilTest,"+new Date()); Connection connection = DriverManager.getConnection("jdbc:h2:mem:testdb","sa", ""); ScriptRunner scriptRunner = new ScriptRunner(connection); scriptRunner.runScript(Resources.getResourceAsReader("db/data.sql")); SQL sql = new SQL(); sql.SELECT("id,name,birth,height,weight") .FROM("t_user") .WHERE("id=?"); SqlRunner sqlRunner = new SqlRunner(connection); Map<String,Object> map = sqlRunner.selectOne(sql.toString(), 1); connection.close(); System.out.println(sql); System.out.println(map); MetaClass metaClass = MetaClass.forClass(UserDTO.class, new DefaultReflectorFactory()); System.out.println(metaClass.hasDefaultConstructor()); System.out.println(metaClass.hasGetter("userName")); System.out.println(StringUtils.join(metaClass.getGetterNames(),",")); UserDTO user = new UserDTO(); Invoker invoker = metaClass.getGetInvoker("userName"); Object userName = invoker.invoke(user, null); System.out.println(userName); invoker = metaClass.getSetInvoker("userName"); invoker.invoke(user, new Object[] {"tiger"}); System.out.println(user); MetaObject metaObject = SystemMetaObject.forObject(user); System.out.println(metaObject.hasGetter("userName")); System.out.println(StringUtils.join(metaObject.getGetterNames(),",")); System.out.println(metaObject.getGetterType("userName")); } }
完整代码 https://gitee.com/dyyx/demos/blob/master/mybatis/src/main/java/dyyx/UtilTest.java

上一篇     下一篇
GO自定义模块实例

GO printf 格式化输出

mybatis 结果集处理

MySQL jdbc ResultSet

key忽略大小写的TreeMap

go select 与 定时器