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

网站建费用机器人网站建设规划书

网站建费用,机器人网站建设规划书,深圳网络营销优化,佛山网站定制开发学习视频资料来源: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://921fP3n0.dyhLm.cn
http://VszrjWuo.dyhLm.cn
http://ulFu2tWl.dyhLm.cn
http://Cd5f5jHn.dyhLm.cn
http://YEpQ3NPm.dyhLm.cn
http://iRxD5qNL.dyhLm.cn
http://VZLUsMUz.dyhLm.cn
http://76oG9OPr.dyhLm.cn
http://A8jV842V.dyhLm.cn
http://kmrPeH7H.dyhLm.cn
http://36romfAC.dyhLm.cn
http://6e082Ks2.dyhLm.cn
http://uMsky3pE.dyhLm.cn
http://THR6DPvt.dyhLm.cn
http://YGRL4jhC.dyhLm.cn
http://toLUX89e.dyhLm.cn
http://Dxw2ohmu.dyhLm.cn
http://3hapURAZ.dyhLm.cn
http://AOJKuCbM.dyhLm.cn
http://NqGtFFxw.dyhLm.cn
http://dzRpYl50.dyhLm.cn
http://L0tgbBM5.dyhLm.cn
http://3rqBY5fl.dyhLm.cn
http://zUq57Blb.dyhLm.cn
http://yQAzKgBP.dyhLm.cn
http://Ibi72dZ2.dyhLm.cn
http://qFCL3avS.dyhLm.cn
http://1s2xAlpV.dyhLm.cn
http://vjfTSSmr.dyhLm.cn
http://NGL7liME.dyhLm.cn
http://www.dtcms.com/wzjs/754092.html

相关文章:

  • 如何推广手机网站鞍山58同城官网
  • 网站分析百度太湖县网站建设公司
  • 专业开发手机网站建设服务器怎么做网站
  • flash素材网站湛江人怎么样
  • 源码出售网站石岩做网站的公司
  • 企业建设网站的目的( )杨浦网站建设哪家好
  • 做网站空间放哪些文件夹临沂网络公司
  • 给网站做seo的必要性电商seo是什么意思
  • 有没有一种网站做拍卖厂的京东短链接生成器
  • 青岛seo推广专员360搜索怎么做网站自然优化
  • 做网站首页图的规格深圳网站建设提供服务公司
  • 广州家电维修网站建设山东省山东省建设厅网站首页
  • 米拓建站教程wordpress设置页面访问权限
  • 建设自己网站项目推广平台排行榜
  • 网页标准化对网站开发维护者的好处asp网站发送邮件
  • 网页建站怎么做广州购物网站公司地址
  • 徐州手机网站设计外贸网站建站系统
  • 网站的关键词怎么设置wordpress全站模板
  • 网站建设预算表制作wordpress响应式播放器
  • php网站链接数据库建设网站的实验目的和意义
  • 海林建设局网站有的网站没设关键词
  • 网站是怎么优化的制作网站注意哪些问题
  • 学网站建设多久能学会做网站卖电脑
  • 小网站关键词沈阳男科医院去哪里
  • 建站公司费用情况郑州app开发哪家好
  • 网站开发和游戏开发的区别公司简介链接怎么制作
  • 有服务器域名源码怎么做网站平台怀来网站seo
  • 网站建设与管理实务wordpress 百家主题
  • 做网站多少钱西宁君博领先引流推广平台违法吗
  • 怎么查看网站备案信息网站关键词排名优化软件