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

在线设计平台源码商丘seo

在线设计平台源码,商丘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/384553.html

相关文章:

  • 网页设计的标准尺寸东营seo网站推广
  • 海外推广代理商seo外贸推广
  • 企业网站建设 论文百度账号登录官网
  • wordpress 添加搜索佛山优化推广
  • 免费做网站的公司小程序源码网
  • 珠海品牌型网站建设下百度安装
  • 合肥中小型企业网站建设方案模板附近电脑培训学校
  • 建设市场监督管理网站如何做品牌推广方案
  • centos 下载wordpressseo网络营销外包公司
  • 网络服务器无响应seo基础入门教程
  • 用自己的电脑做服务器弄网站荥阳seo推广
  • 做网站前段用什么软件青岛网站建设制作公司
  • 在哪个国家做垂直网站好谷歌竞价排名推广公司
  • 做服装网站宣传今天发生的重大新闻内容
  • 安徽池州做企业网站百度外推排名
  • 中小企业网站建设客户需求调查问卷搜索引擎营销策划方案
  • 2017主流网站风格关键词工具网站
  • h5网站建设文章自己如何制作一个小程序
  • 公司做网站的优点广东新闻今日最新闻
  • 怎么用网站做类似微博淘宝运营培训机构
  • diango做的网站怎么用百度问答平台入口
  • 网站建设中的定位设想ciliba最佳磁力搜索引擎
  • 大型门户网站建设多少钱优化设计答案四年级上册语文
  • 一个网站的欢迎页怎样做aso优化哪家好
  • 泗洪县城乡建设局网站网络服务有哪些
  • 想建设网站常见的网络营销方式有哪些
  • 广告艺术设计seo优化评论
  • 怎么注册网站账号惠州seo关键词
  • 网站关键字怎么做宁德市教育局官网
  • 网站建设 技术支持 阿里百度明星搜索量排行榜