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

凤台县美丽乡村建设网站做gif有什么网站

凤台县美丽乡村建设网站,做gif有什么网站,wordpress首页弹出公告,学院网站建设报价MyBatis 实践标签#xff1a; Java与存储 Configuration mybatis-configuration.xml是MyBatis的全局配置文件(文件名称随意),其配置内容和顺序例如以下: properties : 属性(文件)载入/配置settings : 全局配置參数typeAliases : 定义类型别名typeHandlers : 类型处理器objectF… MyBatis 实践 标签 Java与存储 Configuration mybatis-configuration.xml是MyBatis的全局配置文件(文件名称随意),其配置内容和顺序例如以下: properties : 属性(文件)载入/配置settings : 全局配置參数typeAliases : 定义类型别名typeHandlers : 类型处理器objectFactory : 对象工厂plugins : 插件environments : 环境集合属性对象 environment transactionManager : 事务管理dataSource : 数据源databaseIdProvider:P数据库厂商标识mappers : 映射器properties 方便对配置參数统一管理,供其它XML引用,我们能够将数据库的连接參数抽取出来: db.properties## Data Source mysql.driver.classcom.mysql.jdbc.Driver mysql.urljdbc:mysql://host:port/db?characterEncodingutf-8 mysql.useruser mysql.passwordpassword mybatis-configuration.xmlproperties resourcedb.properties/environments defaultdevelopmentenvironment iddevelopment!-- 配置JDBC事务管理--transactionManager typeJDBC/!-- 配置数据源--dataSource typePOOLEDproperty namedriver value${mysql.driver.class}/property nameurl value${mysql.url}/property nameusername value${mysql.user}/property namepassword value${mysql.password}//dataSource/environment /environments 注: MyBatis依照例如以下顺序载入properties: 1) 在properties标签内定义的属性; 2) .properties文件里定义的属性; 3) 最后读取作为方法參数传递的属性. settings MyBatis全局配置參数,会影响MyBatis执行时行为(如:开启二级缓存/延迟载入).见MyBatis文档. typeAliases MyBatis默认支持的类型别名可參考MyBatis文档,我们也能够自己定义别名,但并不推荐,使用PO对象的全限定名能够提高Statement的可读性. typeHandlers typeHandlers用于Java类型和JDBC类型转换,MyBatis提供了非常多默认的类型处理器(详见MyBatis文档),并且也基本满足日常开发需求,因此一般就不再须要单独定义. mappers 前面已经将SQL语句定义到了mapper文件里,那么mappers/属性就是告诉MyBatis到哪里去寻找mapper文件,MyBatis提供了例如以下几种配置方法: 配置描写叙述mapper resource/使用类路径的资源(Resources/java文件夹下)mapper url/使用全然限定路径mapper class/使用mapper接口类路径package name/注冊指定包下的全部mapper接口 注意:后两种方式要求mapper接口名和mapper映射文件名称称同样,且放在同一个文件夹中(不推荐). 其它关于MyBatis的配置信息可參考MyBatis文档. 整合Spring 实现MyBatis与Spring整合之后,能够使用Spring来管理SqlSessionFactory和mapper接口,Spring自己主动使用SqlSessionFactory创建SqlSession,并将实现好DAO接口注冊到Spring容器中, 供Autowired使用. 1. 加入依赖 加入Spring支持dependencygroupIdorg.springframework/groupIdartifactIdspring-core/artifactIdversion${spring.version}/version /dependency dependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion${spring.version}/version /dependency dependencygroupIdorg.springframework/groupIdartifactIdspring-beans/artifactIdversion${spring.version}/version /dependency dependencygroupIdorg.springframework/groupIdartifactIdspring-expression/artifactIdversion${spring.version}/version /dependency dependencygroupIdorg.springframework/groupIdartifactIdspring-aop/artifactIdversion${spring.version}/version /dependency dependencygroupIdorg.springframework/groupIdartifactIdspring-test/artifactIdversion${spring.version}/version /dependency dependencygroupIdorg.springframework/groupIdartifactIdspring-jdbc/artifactIdversion${spring.version}/version /dependency dependencygroupIdorg.springframework/groupIdartifactIdspring-tx/artifactIdversion${spring.version}/version /dependency 加入MyBatis-Spring包dependencygroupIdorg.mybatis/groupIdartifactIdmybatis-spring/artifactIdversion${mybatis-spring.version}/version /dependency 加入Hikaricp数据库连接池dependencygroupIdcom.zaxxer/groupIdartifactIdHikariCP/artifactIdversion${hikaricp.version}/version /dependency 不要忘了MySQL数据库驱动dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion${mysql.version}/version /dependency 2. 配置文件 精简mybatis-configuration.xml 能够将数据源的配置移到以下的applicationContext-datasource.xml中.?xml version1.0 encodingUTF-8 ? !DOCTYPE configuration PUBLIC -//mybatis.org//DTD Config 3.0//ENhttp://mybatis.org/dtd/mybatis-3-config.dtd configurationmappersmapper resourcemybatis/mapper/UserDAO.xml//mappers /configuration 定义applicationContext-datasource.xmlbeans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdcontext:property-placeholder locationclasspath:db.properties/!-- 配置数据源 --bean idhikariConfig classcom.zaxxer.hikari.HikariConfigproperty namedriverClassName value${mysql.driver.class}/property namejdbcUrl value${mysql.url}/property nameusername value${mysql.user}/property namepassword value${mysql.password}/property namemaximumPoolSize value5/property namemaxLifetime value700000/property nameidleTimeout value600000/property nameconnectionTimeout value10000/property namedataSourcePropertiespropsprop keydataSourceClassNamecom.mysql.jdbc.jdbc2.optional.MysqlDataSource/propprop keycachePrepStmtstrue/propprop keyprepStmtCacheSize250/propprop keyprepStmtCacheSqlLimit2048/prop/props/property/beanbean iddataSource classcom.zaxxer.hikari.HikariDataSource destroy-methodcloseconstructor-arg refhikariConfig//bean!-- 配置SqlSessionFactory --bean idsqlSessionFactory classorg.mybatis.spring.SqlSessionFactoryBeanproperty namedataSource refdataSource/property nameconfigLocation valueclasspath:mybatis/mybatis-configuration.xml//bean!-- 依据mapper接口生成代理对象 --bean iddao classorg.mybatis.spring.mapper.MapperFactoryBeanproperty namemapperInterface valuecom.fq.mybatis.UserDAO/property namesqlSessionFactory refsqlSessionFactory//bean /beans 上面的配置存在一个问题:须要针对每一个mapper配置一个MapperFactoryBean(繁琐),因此这段依据mapper接口生成代理对象的配置可更改例如以下: !-- 基于包扫描的mapper配置 -- bean classorg.mybatis.spring.mapper.MapperScannerConfigurerproperty namebasePackage valuecom.fq.mybatis/property namesqlSessionFactoryBeanName valuesqlSessionFactory/ /bean 附: applicationContext-database.xml完整配置可參考: Git地址 定义Spring主配置文件applicationContext.xml 定义注解驱动及载入静态配置文件datasource:?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beans xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlns:contexthttp://www.springframework.org/schema/context xsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd !-- 注解驱动 -- context:annotation-config/ !-- 载入静态配置文件 -- import resourceapplicationContext-datasource.xml/ /beans Client/*** author jifang* since 16/2/22 上午10:20.*/ RunWith(SpringJUnit4ClassRunner.class) ContextConfiguration(locations classpath:spring/applicationContext.xml) public class UserDAOClient {Autowiredprivate UserDAO dao;Testpublic void client() throws Exception {User user dao.selectUserById(1);System.out.println(user);} } 缓存 与大多数持久层框架一样,MyBatis也支持一级缓存和二级缓存. 缓存作用是提升系统总体性能(不是提升数据库性能:由于缓存将数据库中的数据存放到内存,下次查询同样内容时直接从内存读取,减轻数据库压力,并且直接从内存中读取数据要比从数据库检索快非常多,因此能够提升系统总体性能). 缓存数据更新:当一个作用域(一级缓存为SqlSession/二级缓存为namespace)进行了C/U/D操作后,默认该作用域下全部缓存都被清空. 一级缓存 MyBatis默认开启了一级缓存.一级缓存是基于org.apache.ibatis.cache.impl.PerpetualCache的HashMap本地缓存,其存储作用域为SqlSession,同一个SqlSession几次执行同样SQL,后面的查询会直接从缓存中载入,从而提高查询效率/减轻数据库压力.当SqlSession经flush/close后,该SqlSession中的全部Cache数据被清空. 二级缓存 与一级缓存机制相似,MyBatis二级缓存默认也是採用PerpetualCache的HashMap存储,不同在于二级缓存存储作用域为namespace/mapper,并且能够自己定义缓存实现,如Ehcache. MyBatis默认没有开启二级缓存,须要经过以下步骤才干使用: 启用二级缓存(可选) 其须要在mybatis-configuration.xml的settings全局參数中开启:settingssetting namecacheEnabled valuetrue/ /settings cacheEnabled对此配置文件下的全部cache进行全局性开/关设置(默觉得true). 配置缓存策略 在mapper映射文件里加入cache/标签,以指定该namespace开启二级缓存, 并指定缓存策略:cache evictionLRU flushInterval60000 size512 readOnlytrue/ 1) eviction:缓存淘汰算法: 算法描写叙述释义LRU近期最少使用移除最长时间不被使用的对象(默认).FIFO先进先出按对象进入缓存的顺序移除.SOFT软引用移除基于垃圾回收器状态和软引用规则的对象.WEAK弱引用更积极地移除基于垃圾收集器状态和弱引用规则的对象.2) flushInterval:刷新间隔(缓存过期时间),单位为毫秒,MyBatis会每隔一段时间自己主动清空缓存(默认刷新间隔为空, 即永只是期,仅调用语句时刷新). 3) size:引用数目,要记住你缓存的对象的数目和执行环境可用内存资源数目(默认1024). 4) readOnly: 仅仅读.假设为true,则全部同样SQL返回同一对象(因此这些对象不能改动,有助于提高性能,但并发操作同一条数据时,可能不安全);假设为false,则同样SQL后面返回的是cache的clone副本(通过序列化,慢一些但更是安全,因此默认是false). 序列化 PO对象要实现Serializable序列化,由于二级缓存的存储介质不一定仅仅是内存:public class User implements Serializable {//... } ClientTest public void cacheClient() throws Exception {testCache(factory.openSession());testCache(factory.openSession());testCache(factory.openSession()); }private void testCache(SqlSession session) throws Exception {UserDAO dao session.getMapper(UserDAO.class);dao.selectUserById(1);// 须要将SqlSession关闭才干将数据写入缓存.session.close(); } 执行代码, 并观察log输出的命中率(Cache Hit Ratio). Statement配置1) 禁用缓存: 在Statement中设置useCachefalse能够禁用当前select语句的二级缓存(默觉得true:该SQL启用二级缓存). select idselectUserById parameterTypejava.lang.Integer resultTypecom.fq.domain.User useCachetrueSELECT *FROM userWHERE id #{id}; /select 2)刷新缓存: 同一个namespace中,假设还有其它insert/update/delete操作,须要刷新缓存,使用flushCachetrue属性设置(默觉得true刷新缓存). insert idinsertUserList parameterTypejava.util.List flushCachetrueINSERT INTO user(name, password) VALUESif testlist ! null and list.size ! 0foreach collectionlist itemuser separator,(#{user.name}, #{user.password})/foreach/if /insert 整合Ehcache MyBatis暴露一个org.apache.ibatis.cache.Cache接口出来,通过实现该接口,能够实现各类缓存产品(如Ehcache/Redis/Memcached)与MyBatis的整合(MyBatis的特长操作数据库,缓存管理并非其擅长,因此整合其它缓存产品能够提高系统总体性能). Ehcache是一个纯Java开发的进程内缓存框架,具有开源/高速/灵活等特点,是Hibernate默认的CacheProvider.使用Ehcache须要在pom.xml中加入例如以下依赖: dependencygroupIdnet.sf.ehcache/groupIdartifactIdehcache-core/artifactIdversion2.6.11/version /dependency dependencygroupIdorg.mybatis.caches/groupIdartifactIdmybatis-ehcache/artifactIdversion1.0.3/version /dependency 配置Ehcache 在Resources文件夹下加入ehcache.xml配置文件ehcachediskStore path/data/cache/defaultCachemaxElementsInMemory1000maxElementsOnDisk10000000eternalfalseoverflowToDiskfalsetimeToIdleSeconds120timeToLiveSeconds120diskExpiryThreadIntervalSeconds120memoryStoreEvictionPolicyLRU/defaultCache /ehcache 属性描写叙述diskStore指定缓存数据在磁盘的存储位置maxElementsInMemory在内存中缓存element的最大数目maxElementsOnDisk在磁盘上缓存element的最大数目,0表示无穷大eternal设定缓存的elements是否永远只是期.true,则缓存的数据始终有效,假设为false那么还要依据timeToIdleSeconds,timeToLiveSeconds推断overflowToDisk设定当内存缓存溢出的时候是否将过期的element缓存到磁盘上timeToIdleSeconds刷新间隔:缓存数据前后两次訪问时间超过timeToIdleSeconds时,这些数据便会删除(默觉得0,时间间隔无穷大)timeToLiveSeconds缓存element的有效生命期(默觉得0,时间无限)diskSpoolBufferSizeMB设置DiskStore(磁盘缓存)缓存区大小.默认是30MB.diskPersistent在JVM重新启动时是否使用磁盘保存Ehcache数据,默认是false.diskExpiryThreadIntervalSeconds磁盘缓存的清理线程执行间隔,默认是120秒.memoryStoreEvictionPolicy当内存缓存达到最大,有新的element加入的时候, 移除缓存中element的策略.默认是LRU(近期最少使用),可选的有LFU(最不常使用)和FIFO(先进先出)mapper配置ehcachecache typeorg.mybatis.caches.ehcache.EhcacheCache evictionLRU flushInterval60000 size1024readOnlytrue/ 还能够依据需求调整当前namespace的缓存參数: cache typeorg.mybatis.caches.ehcache.EhcacheCacheproperty nametimeToIdleSeconds value3600/property nametimeToLiveSeconds value3600/!-- 同ehcache參数maxElementsInMemory --property namemaxEntriesLocalHeap value1000/!-- 同ehcache參数maxElementsOnDisk --property namemaxEntriesLocalDisk value10000000/property namememoryStoreEvictionPolicy valueLRU/ /cache 二级缓存小结 适用场景 对于查询请求多且对查询结果实时性要求不高的场景,可採用二级缓存减少数据库负担,提高訪问速度(业务场景如:微博/动态/订单信息等).局限 二级缓存对细粒度级别的缓存实现不好,如”缓存全部的商品信息时,二级缓存就无法实现当一个商品信息变化时仅仅刷新该商品缓存而不刷新全部商品缓存“,由于二级缓存区域以namespace为单位划分,当一个商品发生变化会将全部商品缓存清空,因此解决此类问题须要在上层对数据进行业务划分. 转载于:https://www.cnblogs.com/jhcelue/p/7142311.html
http://www.zqtcl.cn/news/451278/

相关文章:

  • 在建工程查询网站怎么自己开发网站
  • 旧电脑怎么做网站如何自己弄个免费网站
  • 聊城网站营销WordPress工作发布
  • 建造网站需要什么汽车网站建设
  • 网站建设app郑州发布评论
  • 福州网站制作建设网页设计图片是怎么显示的
  • 天津通用网站建设收费网站建设怎么在png上写文字
  • 浏阳做网站报价高校网站站群建设公司
  • 海口网站提升排名网站建设与管理考试题
  • 做网站的算什么行业ui视觉设计常用软件是什么
  • 成都网站建设公司哪家好西安搬家公司哪家便宜
  • 程序员自己做网站怎么能来钱上海猎头公司哪家好
  • 无忧网站建设哪家好手机网站php开发
  • 如何仿制一个网站wordpress+主题课堂
  • 公明做网站渭南网站开发
  • 网站优化排名多少钱查备案网站备案
  • 北京网站建设市场培训机构参与课后服务
  • wordpress如何添加网站地图上海网站开发设计公司
  • 网站设置反爬虫的主要原因深圳外贸公司上班工资高吗
  • 济南建站价格同仁网站建设公司
  • 石家庄建站软件中国纪检监察报怎么订阅
  • 国内建网站费用厦门房地产网站建设
  • 宝山网站制作网站优化待遇
  • 网站建设项目竞争性招标文件界面设计的重要性
  • 网站建设合同机械设备网络推广方案
  • 阿里巴巴做网站的绿色的医疗资讯手机网站wap模板html源码下载
  • 怎么样自己做企业网站dz采集wordpress
  • 欧 美 做 爱 视频网站阿里巴巴电子商务网站建设目的
  • 动易网站后台修改栏目的字定制型网站设计价格
  • 设计网站页面临夏州建设厅官方网站