Mybatis的Dao为什么都是接口
2020-05-20 本文已影响0人
木子苇苇
我们在使用mybatis做orm时, 只需要告知框架我们的mapper映射和domian所在位置即可, 不需要实现类, mybatis是如何帮我们访问数据库的呢 ?
debug查看dao接口的mybatis的实现类
// 追踪代码每个接口的实现类是MapperProxy
// 该类实现了 `InvocationHandler` 使用的jdk的动态代理
public class MapperProxy<T> implements InvocationHandler, Serializable {
}
我们自己来实现一个简单的mybatis
- 创建接口
- 创建代理类
- 向mvc中注入代理类
- 测试调用
创建接口类
public interface UserDao {
public User getById(String id);
}
创建代理类
public class IBatisProxy implements InvocationHandler
{
private HikariDataSource pool;
public IBatisProxy(HikariDataSource pool)
{
this.pool = pool;
}
@ Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
{
Connection connection = pool.getConnection();
PreparedStatement statement = connection.prepareStatement("select * from dem_user where id =?");
statement.setString(1, args[0].toString());
ResultSet resultSet = statement.executeQuery();
resultSet.next();
String id = resultSet.getString("id");
String name = resultSet.getString("name");
String account = resultSet.getString("account");
String password = resultSet.getString("password");
String auth = resultSet.getString("auth");
String organId = resultSet.getString("organ_id");
User user = new User(id, name, account, password, auth, organId);
connection.close();
return user;
}
}
向ApplicationContext中注入代理类
@ Configuration
public class UserDaoConfig
{
@ Autowired
private HikariDataSource hikariDataSource;
@ Bean
public UserDao userDao2()
{
Class<UserDao> clazz = UserDao.class;
UserDao userDao = (UserDao)Proxy.newProxyInstance(clazz.getClassLoader(), new Class<?>[]{clazz},
new IBatisProxy(hikariDataSource));
return userDao;
}
}
测试调用
@ RestController
public class HelloWorldController
{
@ Autowired
@ Qualifier("userDao2")
private UserDao userDao;
@ RequestMapping("/user/{id}")
@ Transactional
public com.inus.domain.User getUser(@ PathVariable String id)
{
if (StringUtils.isNotEmpty(id))
{
return userDao.getById(id);
}
return null;
}
}
image-20200520154830013.png