做个游戏网站多少钱,代发货网站建设,该如何建设和优化一个网站,网上服务大厅12333文章目录 使用javassist动态生成类使用javassist动态生成类并实现接口实现接口中所有的方法工具类GenerateDaoProxy的编写 使用javassist动态生成类 Testpublic void testGennerateFirstClass() throws Exception{//获取类池#xff0c;这个类池就是用来生成class的ClassPool … 文章目录 使用javassist动态生成类使用javassist动态生成类并实现接口实现接口中所有的方法工具类GenerateDaoProxy的编写 使用javassist动态生成类 Testpublic void testGennerateFirstClass() throws Exception{//获取类池这个类池就是用来生成class的ClassPool pool ClassPool.getDefault();//制造类需要告诉javassist类名是啥CtClass ctClass pool.makeClass(类名);//制造方法String methodCode publi coid insert(){System.out.println(123);};CtMethod ctMethod CtMethod.make(methodCode, ctClass);//将方法添加到类中ctClass.addMethod(ctMethod);//在内存中生成classctClass.toClass();//类加载到JVM当中返回类的字节码Class? clazz Class.forName(类名);//创建对象Object obj clazz.newInstance();//获取类中的方法Method insertMethod clazz.getDeclaredMethod(insert);//调用方法insertinsertMethod.invoke(obj);}使用javassist动态生成类并实现接口 Testpublic void testGenerateImpl() throws Exception{//获取类池ClassPool pool ClassPool.getDefault();//制造类CtClass ctClass pool.makeClass(类地址)//制造接口CtClass ctInterface pool.makeInterface(类接口地址);// 添加接口到类中ctClass.addInterface(ctInterface);//实现接口中的方法//制造方法CtMethod ctMethod CtMethod.make(条件执行语句,ctClass);//将方法添加到类中ctClass.addMethod(ctMethod);//在内存中生成类同时将生成的类加载到JVM当中。Class? clazz ctClass.toClass();AccountDao accountDao (AccountDao) clazz.newInstance();accountDao.delete();}实现接口中所有的方法
public class javassistTest {Testpublic void testGenerateAccountDaoImpl() throws Exception{//获取类池ClassPool pool ClassPool.getDefault();//制造类CtClass ctClass pool.makeClass(类名地址);//制造接口CtClass ctInterface pool.makeInterface(接口地址);//实现接口ctClass.addInterface(ctInterface);//实现接口中所有的方法Method[] methods AccountDao.class.getDeclaredMethods();Arrays.stream(methods).forEach(method - {//method是接口中的抽象方法//把method抽象方法给实现了try{//public void delete(){}//public int update(String actno, Double balance){}StringBuilder methodCode new StringBuilder();methodCode.append(public);//追加修饰符列表methodCode.append(method.getReturnType().getName());//追加返回值类型methodCode.append( );methodCode.append(method.getName());//追加方法名methodCode.append(();//拼接参数 String actno Double balanceClass?[] paramterTypes method.getParameterTypes();for (int i 0; i paramterTypes.length; i) {Class? parameterType paramterTypes[i];methodCode.append(parameterType.getName());methodCode.append( );methodCode.append(arg i);if(i ! paramterTypes.length - 1){methodCode.append(,);}}methodCode.append(){System.out.println(1111);return 1;}});//动态的添加return语句String returnTypeSimpleName method.getReturnType().getSimpleName();if (void.equals(returnTypeSimpleName)){}else if (int.equals(returnTypeSimpleName)){methodCode.append(return 1;);}else if(String.equals(returnTypeSimpleName)){methodCode.append(return \hello\;);}methodCode.append(});System.out.println(methodCode);CtMethod ctMethod CtMethod.make(methodCode.toString(), ctClass);ctClass.addMethod(ctMethod);}catch (Exception e){e.printStackTrace();}});//在内存中生成class并且加载到JVM当中Class? clazz ctClass.toClass();//创建对象AccountDao accountDao (AccountDao) clazz.newInstance();//调用方法//...}工具类GenerateDaoProxy的编写
package com.example.bank.utils;import org.apache.ibatis.javassist.ClassPool;
import org.apache.ibatis.javassist.CtClass;
import org.apache.ibatis.javassist.CtMethod;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.session.SqlSession;import java.lang.reflect.Method;
import java.util.Arrays;public class GenerateDaoProxy{public static Object generate(SqlSession sqlSession, Class daoInterface){//获取类池ClassPool pool ClassPool.getDefault();//制造类CtClass ctClass pool.makeClass(daoInterface.getName() Proxy);//制造接口CtClass ctInterface pool.makeInterface(daoInterface.getName());//实现接口ctClass.addInterface(ctInterface);//实现接口中所有的方法Method[] methods daoInterface.getDeclaredMethods();Arrays.stream(methods).forEach(method - {//method是抽象方法//将method这个抽象方法进行实现try{// Account selectBtActno(String actno);//public Account selectByActno(String arg0, int arg1, int arg2){代码; }StringBuilder methodCode new StringBuilder();methodCode.append(public );methodCode.append(method.getReturnType().getName());methodCode.append( );methodCode.append(method.getName());methodCode.append(();//需要方法的形式参数列表Class?[] paramterTypes method.getParameterTypes();for(int i 0; i paramterTypes.length; i){Class? paramterType paramterTypes[i];methodCode.append(paramterType.getName());methodCode.append( );methodCode.append(arg i);if(i ! paramterTypes.length - 1){methodCode.append(,);}}methodCode.append());methodCode.append({);//需要方法体当中的代码methodCode.append(org.apache.ibatis.session.SqlSession sqlSession com.example.bank.utils.SqlSessionUtil.openSession(););//需要知道是什么类型的sql语句//sql语句的id是框架使用者提供的具有多变性等于我框架的开发人员来说我不知道//既然我框架开发者不知道sqlId怎么办呢mybatis框架的开发者于是就出台了一个归档凡是使用GenerateDaoProxy机制的。//sqlId都不能随便写namespace必须是dao接口的全限定名称id必须是dao接口中的方法名String sqlId daoInterface.getName() . method.getName();SqlCommandType sqlCommandType sqlSession.getConfiguration().getMappedStatement(sqlId).getSqlCommandType();if(sqlCommandType SqlCommandType.INSERT){}if(sqlCommandType SqlCommandType.DELETE){}if(sqlCommandType SqlCommandType.UPDATE){methodCode.append(return sqlSession.update(\sqlId\,arg0););}if(sqlCommandType SqlCommandType.SELECT){String returnType method.getReturnType().getName();methodCode.append(return (returnType) sqlSession.selectOne(\sqlId\,arg0););}methodCode.append(});CtMethod ctMethod CtMethod.make(methodCode.toString(), ctClass);ctClass.addMethod(ctMethod);}catch(Exception e){e.printStackTrace();}});//创建对象Object obj null;try{Class? clazz ctClass.toClass();obj clazz.newInstance();} catch (Exception e){e.printStackTrace();}return obj;}
}