当前位置: 首页 > wzjs >正文

php网站换服务器北京网站推广

php网站换服务器,北京网站推广,站长工具seo综合查询源码,南通网站制作建设学习视频资料来源:https://www.bilibili.com/video/BV1R14y1W7yS 文章目录 1.问题2. 解决思路分析3. 生成代理对象 1.问题 上一章框架使用端在测试类中使用框架查询数据库的代码如下: public class IpersistentTest {Testpublic void test1() throws …

学习视频资料来源:https://www.bilibili.com/video/BV1R14y1W7yS

文章目录

  • 1.问题
  • 2. 解决思路分析
  • 3. 生成代理对象

1.问题

上一章框架使用端在测试类中使用框架查询数据库的代码如下:

public class IpersistentTest {@Testpublic void test1() throws Exception {InputStream resource = Resources.getResource("sqlMapConfig.xml");SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resource);// 创建SqlSession,过程中创建了executor,executor用于执行sql语句SqlSession sqlSession = sqlSessionFactory.openSession();User user = new User();user.setId(1);user.setUsername("xiaoming");Object o = sqlSession.selectOne("user.selectOne", user);System.out.println("selectOne返回结果:" + o.toString());List<Object> users = sqlSession.selectList("user.selectList", null);System.out.println("selectLit返回结果:" + users.toString());sqlSession.close();}
}

不过这是在测试类中执行的,如果要在项目中实际使用,需要定义UserDao 接口并实现,将上述操作封装进实现类方法中。如下:
接口

public interface UserDao {List<User> findAll() throws Exception;User findByCondition(User user) throws Exception;
}

实现类


public class UserDaoImpl implements UserDao {@Overridepublic List<User> findAll() throws Exception {InputStream resource = Resources.getResource("sqlMapConfig.xml");SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resource);// 创建SqlSession,过程中创建了executor,executor用于执行sql语句SqlSession sqlSession = sqlSessionFactory.openSession();List<User> users = sqlSession.selectList("user.selectList", null);sqlSession.close();return users;}@Overridepublic User findByCondition(User user) throws Exception {InputStream resource = Resources.getResource("sqlMapConfig.xml");SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resource);// 创建SqlSession,过程中创建了executor,executor用于执行sql语句SqlSession sqlSession = sqlSessionFactory.openSession();User res= sqlSession.selectOne("user.selectOne", user);sqlSession.close();return res;}
}

可以很明显这种方式查询数据库存在的3个问题:
1. 实现类中存在重复代码。包括:加载配置文件,创建会话,关闭会话。
2. selectOne和selectList参数statmentId存在硬编码问题。如果配置文件中改了,代码也要改。
3. 对于每一个表,都需要一个dao。每个dao都需要也给实现类,显然这是比较麻烦的。这一点视频中没有提到,但我觉得是很重要的一个问题。

2. 解决思路分析

  1. 第1个问题比较好解决,因为我们可以使用单例模式保证执行一次后生成的sqlSessionFactory 对象,可以复用 。而且项目大概率会集成spring,所以加载配置文件,执行一次就可以了。
  2. 第2 个问题我们想到可以通过将mapper.xml中的namespace和select标签id 与类全路径和方法名进行绑定(这是非常关键的一点,有一种依赖导致的感觉!!之前是xml里配置什么,我们写代码时候就要传什么,现在是代码写什么,xml里就配置什么)。即xml中的namespace为dao接口的全路径,select标签id 为方法名。这样再调用方法的时候,就能根据类和方法名拼接出statmentId,委托给excutor执行。
  3. 解决了1和2,就会发现我们不需要dao接口的实现类了。只需要创建一个接口的代理对象即可。代理对象去执行真正的操作。同时,就解决了问题3。

3. 生成代理对象

生成代理的对象具体执行逻辑也有2个小问题:

  1. 怎么知道是调哪种SQL类型的方法?如select ? update? delete等
    这个可以在解析xml加下标签类型,比如是select标签,那就是调用selectOne或者selectList。
  2. 怎么知道是调用selectOne还是selectList,如果选择的不对,返回值就会和方法的返回值类型不一致而报错。
    这个可以根据调用方法的返回值确定。获取方法的返回值类型,如果返回值类型是个泛型。如List 、List、Map<String,String>等,那就是 selectList,否则selectOne。

最终核心代码如下:

public class DefaultSqlSession implements SqlSession {//......@Overridepublic <T> T getMapper(Class<?> mapperClass) {Object o = Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class[]{mapperClass}, new InvocationHandler() {/*** porxy代理对象的引用,* method被代理对象的方法* objects*/@Overridepublic Object invoke(Object proxy, Method method, Object[] objects) throws Throwable {// 具体的逻辑:执行JDBCString methodName = method.getName();//接口中的全路径要和namespace的值一致String className = method.getDeclaringClass().getName();String statementId = className + "." + methodName;MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementId);String sqlCommandType = mappedStatement.getSqlCommandType();if ("select".equals(sqlCommandType)) {Type genericReturnType = method.getGenericReturnType();// 通过判断方法的返回值是否为泛型,决定调用selectOne或者selectListif (genericReturnType instanceof ParameterizedType) {return selectList(statementId, objects == null ? null : objects[0]);}return selectOne(statementId, objects == null ? null : objects[0]);} else {return null;}}});return (T) o;}
}
public class IpersistentTest {@Testpublic void test1() throws Exception {InputStream resource = Resources.getResource("sqlMapConfig.xml");SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resource);// 创建SqlSession,过程中创建了executor,executor用于执行sql语句SqlSession sqlSession = sqlSessionFactory.openSession();User user = new User();user.setId(1);user.setUsername("xiaoming");Object o = sqlSession.selectOne("user.selectOne", user);System.out.println("selectOne返回结果:" + o.toString());List<Object> users = sqlSession.selectList("user.selectList", null);System.out.println("selectLit返回结果:" + users.toString());sqlSession.close();}// 使用代理类的方式调用@Testpublic void test2() throws Exception {InputStream resource = Resources.getResource("sqlMapConfig.xml");SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resource);// 创建SqlSession,过程中创建了executor,executor用于执行sql语句SqlSession sqlSession = sqlSessionFactory.openSession();UserDao userDaoProxy = sqlSession.getMapper(UserDao.class);List<User> all = userDaoProxy.findAll();System.out.println("findAll返回结果:" + all.toString());User user = new User();user.setId(1);user.setUsername("xiaoming");User use1 = userDaoProxy.findByCondition(user);System.out.println("findByCondition返回结果:" + use1.toString());}
}

可以看到结果也可以正确返回了
在这里插入图片描述

http://www.dtcms.com/wzjs/313497.html

相关文章:

  • 深圳网站公司招聘新媒体运营培训课程
  • 电子商务网站建设书籍普通话手抄报文字内容
  • 网站开发需求分析报告百度广告竞价排名
  • php 社交网站模板源码app开发多少钱
  • 做seo的网站有那些网络seo首页
  • 网站开发和商城的科目公司网站建设费用多少
  • 游戏网站开发文档挖掘关键词爱站网
  • 如何解析网站太原seo排名优化公司
  • 北京个人网站制作在线种子资源库
  • 东莞网站建设 烤活鱼sem是什么缩写
  • 大连模板网站制作服务如何自己建立一个网站
  • 做网站那些好黄冈网站推广软件免费下载
  • visual c 网站开发广州百度快速优化排名
  • 留学公司网站怎么做泰安做网站公司
  • 公司做网站费用会计处理竞价代运营公司
  • wordpress降级插件南宁网站seo排名优化
  • 做网站用哪种编程语言提高工作效率的句子
  • 枣庄手机网站开发公司网站设计模板
  • 徐州关键词优化seo查询源码
  • 政府网站建设进展情况外国人b站
  • 做网站前端要会什么网站开发的基本流程
  • 新公司董事长致辞做网站百度推广怎么样才有效果
  • wordpress做定制T恤的网站河南网站排名
  • 360云盘做服务器建设网站seo教程论坛
  • 江门网站建设设计如何在百度上做产品推广
  • 教人做素食的网站网站seo置顶 乐云践新专家
  • 潍坊网站建设网超专业制作网页的公司
  • 微信公众号免费模板网站百度网站搜索排名
  • 邢台建设网站公司百度快照有什么用
  • 筹划建设智慧海洋门户网站百度竞价返点开户