entity:
package com.itmayiedu.entity;
public class User {
private Integer id;
private String userName;
private Integer userAge;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Integer getUserAge() {
return userAge;
}
public void setUserAge(Integer userAge) {
this.userAge = userAge;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
mapper
package com.itmayiedu.mapper;
import java.util.List;
import com.itmayiedu.entity.User;
import com.itmayiedu.orm.annotation.ExtInsert;
import com.itmayiedu.orm.annotation.ExtParam;
import com.itmayiedu.orm.annotation.ExtSelect;
public interface UserMapper {
@ExtInsert("insert into user(userName,userAge) values(#{userName},#{userAge})")
public int insertUser(@ExtParam("userName") String userName, @ExtParam("userAge") Integer userAge);
@ExtSelect("select * from User where userName=#{userName} and userAge=#{userAge} ")
User selectUser(@ExtParam("userName") String name, @ExtParam("userAge") Integer userAge);
}
package com.itmayiedu.orm.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 自定义插入注解
* 作者: 每特教育-余胜军
* 联系方式:QQ644064779|WWW.itmayiedu.com
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ExtInsert {
String value();
}
package com.itmayiedu.orm.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 自定义参数注解
* 作者: 每特教育-余胜军
* 联系方式:QQ644064779|WWW.itmayiedu.com
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface ExtParam {
String value();
}
package com.itmayiedu.orm.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ExtSelect {
String value();
}
package com.itmayiedu.orm.mybatis.aop;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import com.itmayiedu.orm.annotation.ExtInsert;
import com.itmayiedu.orm.annotation.ExtParam;
import com.itmayiedu.orm.annotation.ExtSelect;
import com.itmayiedu.orm.utils.JDBCUtils;
import com.itmayiedu.utils.SQLUtils;
/**
* 使用反射动态代理技术 拦截接口防范 作者: 每特教育-余胜军
* 联系方式:QQ644064779|WWW.itmayiedu.com
*/
public class MyInvocationHandlerMbatis implements InvocationHandler {
private Object object;
public MyInvocationHandlerMbatis(Object object) {
this.object = object;
}
// proxy 代理对象 method拦截方法 args方法上的参数值
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("使用动态代理技术拦截接口方法开始");
// 使用白话问翻译,@ExtInsert封装过程
// 1. 判断方法上是否存在@ExtInsert
ExtInsert extInsert = method.getDeclaredAnnotation(ExtInsert.class);
if (extInsert != null) {
return extInsert(extInsert, proxy, method, args);
}
// 2.查询的思路
// 1. 判断方法上是否存 在注解
ExtSelect extSelect = method.getDeclaredAnnotation(ExtSelect.class);
if (extSelect != null) {
// 2. 获取注解上查询的SQL语句
String selectSQL = extSelect.value();
// 3. 获取方法上的参数,绑定在一起
ConcurrentHashMap
package com.itmayiedu.orm.mybatis.aop;
import com.itmayiedu.entity.User;
import com.itmayiedu.mapper.UserMapper;
import com.itmayiedu.sql.SqlSession;
public class Test0003 {
public static void main(String[] args) {
// 使用动态代理技术虚拟调用方法
UserMapper userMapper = SqlSession.getMapper(UserMapper.class);
User selectUser = userMapper.selectUser("张三", 644064);
System.out.println(
"结果:" + selectUser.getUserName() + "," + selectUser.getUserAge() + ",id:" + selectUser.getId());
// // 先走拦截invoke
// int insertUserResult = userMapper.insertUser("张三", 644064);
// System.out.println("insertUserResult:" + insertUserResult);
}
}
package com.itmayiedu.sql;
import java.lang.reflect.Proxy;
import com.itmayiedu.orm.mybatis.aop.MyInvocationHandlerMbatis;
public class SqlSession {
// 加载Mapper接口
public static T getMapper(Class classz) {
return (T) Proxy.newProxyInstance(classz.getClassLoader(), new Class[] { classz },
new MyInvocationHandlerMbatis(classz));
}
}