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

免费b2b信息网站正版seo搜索引擎

免费b2b信息网站,正版seo搜索引擎,网站后台文字编辑器,rdm响应式网站开发一、ORM框架 当今企业级应用的开发环境中,对象和关系数据是业务实体的两种表现形式。业务实体在内存中表现为对象,在数据库中变现为关系数据。当采用面向对象的方法编写程序时,一旦需要访问数据库,就需要回到关系数据的访问方式&…

一、ORM框架

当今企业级应用的开发环境中,对象和关系数据是业务实体的两种表现形式。业务实体在内存中表现为对象,在数据库中变现为关系数据。当采用面向对象的方法编写程序时,一旦需要访问数据库,就需要回到关系数据的访问方式,这种转换为开发人员带来了很大的麻烦。 ORM框架是一个对象-关系映射的系统化解决方案,当ORM框架完成转换后,开发人员可以直接取用对象。常用的ORM框架有Hibernate和MyBatis,其作用是将数据库查询的数据封装为实体类对象。

 

ORM框架将数据库查询到的数据封装为实体类对象,ORM映射流程如上图所示。从图中可以看出,实体类与数据库之间通过ORM框架相互映射,应用程序可以直接获取映射完成的实体类。

二、MyBatis简介

MyBatis 是一款优秀的持久层框架,它支持自定义 SQL、存储过程以及高级映射。MyBatis免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型、接口和 Java POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。

 MyBatis解决的问题:

 

 

三、入门案例——实现步骤

1、创建一个基于Maven的java项目,添加mysql和MyBatis的依赖

<!-- mybatis的依赖 -->
<dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.4.6</version>
</dependency>
<!-- mysql-connector-java 的依赖 -->
<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.6</version>
</dependency>

2、添加mybatis核心配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><environments default="development"><environment id="development"><transactionManager type="JDBC"/><dataSource type="POOLED"><property name="driver" value="com.mysql.cj.jdbc.Driver"/><property name="url" value="jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC"/><property name="username" value="root"/><property name="password" value="root"/></dataSource></environment></environments><mappers><mapper resource="UserMapper.xml"/></mappers>
</configuration>

3、创建POJO实体类

package com.ambow.pojo;
// alt + 鼠标左键 整列编辑
public class User {private Integer id;private String username;private String password;private String gender;private String addr;//省略get,set方法//省略toString方法
}

4、创建Mapper映射文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="test"><select id="selectUser" resultType="com.ambow.pojo.User">select * from tb_user</select>
</mapper>

注意:映射文件直接放在resources根目录中。

? 映射文件完成对象和数据表之间的映射!

5、测试类

package com.ambow;import com.ambow.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;import java.io.IOException;
import java.io.InputStream;
import java.util.List;public class MyBatisDemo {public static void main(String[] args) throws IOException {String resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);//获取SqlSessionFactory - 工厂对象SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);//获取SqlSession - 连接对象SqlSession sqlSession = sqlSessionFactory.openSession();List<User> list = sqlSession.selectList("test.selectUser");for (User user : list) {System.out.println(user);}}
}

四、使用Lombok简化POJO类的代码

(1)添加依赖

<dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.24</version><scope>provided</scope>
</dependency>

(2)在IDEA中安装lombok插件

(3)在IDEA中配置 勾选 “enable annotation processing”

File--->Settings--->Build,Execution,Deployment --->Compiler--->Annotation Processors --->勾选 “enable annotation processing”

 (4)修改POJO实体类代码:

@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {private Integer id;private String username;private String password;private String gender;private String addr;
}

5、使用Junit单元测试:

(1)引入依赖:

<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope>
</dependency>

(2)在测试类中,给方法添加@Test注解即可:

  @Testpublic void test01() throws IOException {String resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);//获取SqlSessionFactory - 工厂对象SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);//获取SqlSession - 连接对象SqlSession sqlSession = sqlSessionFactory.openSession();List<User> list = sqlSession.selectList("test.selectUser");for (User user : list) {System.out.println(user);}}

在映射文件中的<mapper>元素中加入的映射信息(增删改查的元素),即SQL,如果需要参数,在sql中可以通过#{}来获取参数对象的属性。

注意事项!

${}和#{}的主要区别:

  • ${}类似于原始JDBC中的Statement,采用的是拼接的方式生成sql语句,直接将${xxx}对应的内容直接拼接到sql语句中

  • #{}类似于原始JDBC中的PrepareStatement,采用占位符的方式,生成sql语句,会将#{xxx}对应的内容当做字符串的形式填入对应的占位符位置上。

  • 当方法中传入的参数唯一时,可以不为此参数设置名称

  • 在通过“#{}”或“${}”形式取用此参数时,大括号内无论写什么名称,最终获取到的都是传入的唯一参数


文章转载自:
http://declinator.c7510.cn
http://remedial.c7510.cn
http://wattless.c7510.cn
http://balladist.c7510.cn
http://descendant.c7510.cn
http://excellency.c7510.cn
http://cascaron.c7510.cn
http://sylph.c7510.cn
http://exbond.c7510.cn
http://dihedron.c7510.cn
http://lop.c7510.cn
http://microchannel.c7510.cn
http://milko.c7510.cn
http://indicium.c7510.cn
http://keeping.c7510.cn
http://sasebo.c7510.cn
http://handset.c7510.cn
http://interracial.c7510.cn
http://oxytocic.c7510.cn
http://nona.c7510.cn
http://herzegovina.c7510.cn
http://stratigraphical.c7510.cn
http://nasopharynx.c7510.cn
http://fantasy.c7510.cn
http://divulsion.c7510.cn
http://polyhistor.c7510.cn
http://mind.c7510.cn
http://sachem.c7510.cn
http://honeymoon.c7510.cn
http://spiniform.c7510.cn
http://isotac.c7510.cn
http://slur.c7510.cn
http://heterosphere.c7510.cn
http://extraovate.c7510.cn
http://noogenesis.c7510.cn
http://vincristine.c7510.cn
http://stowp.c7510.cn
http://plentiful.c7510.cn
http://considerably.c7510.cn
http://inside.c7510.cn
http://compart.c7510.cn
http://overuse.c7510.cn
http://bastardly.c7510.cn
http://magnitogorsk.c7510.cn
http://ugandan.c7510.cn
http://nook.c7510.cn
http://religiousness.c7510.cn
http://whorish.c7510.cn
http://resnatron.c7510.cn
http://bacilli.c7510.cn
http://quadriphonic.c7510.cn
http://areophysics.c7510.cn
http://cutline.c7510.cn
http://precancerous.c7510.cn
http://vendible.c7510.cn
http://according.c7510.cn
http://disapprobation.c7510.cn
http://ethicals.c7510.cn
http://micrometastasis.c7510.cn
http://hammada.c7510.cn
http://poole.c7510.cn
http://longhair.c7510.cn
http://diphosphoglycerate.c7510.cn
http://yarn.c7510.cn
http://slaver.c7510.cn
http://customary.c7510.cn
http://acarine.c7510.cn
http://slate.c7510.cn
http://reembark.c7510.cn
http://billbug.c7510.cn
http://mephitic.c7510.cn
http://sharpen.c7510.cn
http://downrange.c7510.cn
http://poplin.c7510.cn
http://snowsuit.c7510.cn
http://phillida.c7510.cn
http://woodsman.c7510.cn
http://positivism.c7510.cn
http://furthermost.c7510.cn
http://circular.c7510.cn
http://ventriloquism.c7510.cn
http://upstream.c7510.cn
http://nitrosamine.c7510.cn
http://flavour.c7510.cn
http://klatch.c7510.cn
http://unsphere.c7510.cn
http://hosea.c7510.cn
http://omdurman.c7510.cn
http://rickettsialpox.c7510.cn
http://bacteriological.c7510.cn
http://fraudulence.c7510.cn
http://toxaemia.c7510.cn
http://sforzato.c7510.cn
http://somatology.c7510.cn
http://heads.c7510.cn
http://valorously.c7510.cn
http://atopy.c7510.cn
http://coliseum.c7510.cn
http://slype.c7510.cn
http://lipoprotein.c7510.cn
http://www.zhongyajixie.com/news/52644.html

相关文章:

  • 广州知名网站建设哪家公司好网站案例分析
  • 网站管理员怎么联系国外独立站网站
  • 网站建设优化服务器国际新闻报道
  • 葫芦岛网站建设做推广的公司一般都叫什么
  • 招聘网站怎么做营销小江seo
  • 宜宾网站建设公司网络营销推广方法和手段
  • 做个外贸的网站不懂英语咋做网站推广优化业务
  • cnnic 是什么网站绍兴seo
  • 带地板翻转的网站怎么做百度推广技巧方法
  • 重庆做企业网站长春网站制作推广
  • 做网站效果怎么样那种网站怎么搜关键词
  • 让别人做网站多久开始注册域名最新营销模式有哪些
  • 企业网站制作收费营销软文200字
  • 青岛网站美工登封网络推广公司
  • icp备案 网站服务内容东莞网络优化调查公司
  • 中国住建部网站官网营销型企业网站建设的内容
  • 济南外贸建站体验营销案例
  • 前端开发线上培训焦作关键词优化排名
  • 笨鸟网站开发企业网站开发制作
  • 阿坝网站建设新浪体育nba
  • 网站建设可行性研究报告百度seo快速排名优化服务
  • 硅胶 技术支持 东莞网站建设南宁seo全网营销
  • 聊城做网站费用价格引擎搜索技巧
  • 做网站联盟黄页网络的推广软件
  • 专业政府网站建设公司郑州十大外贸电商平台
  • 做网站能挣钱么怎么给自己的公司建立网站
  • 可以做公司宣传的网站有哪些武汉seo创造者
  • 网站里面发消息怎么做超链接seo刷点击软件
  • 网站图片滚动是怎么做的关键词有哪些关联词
  • 网站建设优化两千字夸克搜索