Java Spring Boot VS .NetCore (一)来一个简单的 Hello World

Java Spring Boot VS .NetCore (二)实现一个过滤器Filter

Java Spring Boot VS .NetCore (三)Ioc容器处理

Java Spring Boot VS .NetCore (四)数据库操作 Spring Data JPA vs EFCore

Java Spring Boot VS .NetCore (五)MyBatis vs EFCore

Java Spring Boot VS .NetCore (六) UI thymeleaf vs cshtml

Java Spring Boot VS .NetCore (七) 配置文件

Java Spring Boot VS .NetCore (八) Java 注解 vs .NetCore Attribute

Java Spring Boot VS .NetCore (九) Spring Security vs .NetCore Security

Java Spring Boot VS .NetCore (十) Java Interceptor vs .NetCore Interceptor

Java Spring Boot VS .NetCore (十一)自定义标签 Java Tag Freemarker VS .NetCore Tag TagHelper

Java操作数据库的方式用很多可以用JDBC自己去处理 也可以用通过Spring Data JPA 中封装的Hibernate来完成

添加相关的配置

Spring.datasource.url=jdbc:mysql://192.168.0.233:3306/test1?useSSL=false
Spring.datasource.username=uoso
Spring.datasource.password=uosotech_123
Spring.datasource.driver-class-name=com.mysql.jdbc.Driver Spring.jpa.properties.hibernate.hbm2ddl.auto=update
Spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
Spring.jpa.show-sql= true

接下来通过.NetCore 中的EFCore 来对比下 两种语言操作数据库的方式

Java JDBC vs .NetCore ADO.NET

这些都是操作数据库底层的东西下面我们来看下有什么区别

先看下Java JDBC代码

下面说下步骤

首先   DriverManager.getConnection 来创建连接,然后通过 连接创建语句Statement, 最后执行 语句块中的sql 代码

主要方法大概就是 executeQuery 、executeUpdate

public class JDBCHelper  {
public static Connection getConnection() throws Exception
{
return DriverManager.getConnection("jdbc:mysql://192.168.0.233:3306/test1?useSSL=false","uoso",
"uosotech_123"); }
public static void ExcuteQuery(String sql) throws Exception
{
Connection connection=getConnection();
try {
Statement statement = connection.createStatement();
ResultSet data = statement.executeQuery(sql);
int count= data.getMetaData().getColumnCount();
System.out.println("============================");
while (data.next()) {
for (int i = 1; i <= count; i++) {
System.out.print(data.getString(i) + "\t");
if ((i == 2) && (data.getString(i).length() < 8)) {
System.out.print("\t");
}
}
System.out.println("");
}
System.out.println("============================");
statement.close();
connection.close();
}
catch (SQLException ex)
{
ex.printStackTrace(); } }
public static int ExcuteUpdate(String sql) throws Exception{
Connection connection=getConnection();
Statement statement= connection.createStatement();
int result= statement.executeUpdate(sql);
statement.close();
connection.close();
return result;
} }

ADO.NET 中怎么实现 下面以Sqlserver为例子,不同的数据库 操作类不同而已 简要的介绍下:

首先 创建  SqlConnection 对象 构造连接字符串

其次 创建 SqlCommond对象 执行相关SQL语句

最后通过 ExecuteNonQuery 、ExecuteScaler 执行相关操作

对于数据集上的问题 Java通过 ResultSet 而 .NetCore 中则是通过 SqlDataReader 读取 填充 DataSet  , ResultSet 与 DataSet 其实有异曲同工之妙

Spring Boot JPA Hibernate vs .NetCore  EFCore

EFCore中有CodeFirst方式  而 DataBase First  ,这里就以 CodeFirst来对比,其实这两个也是非常类是的

Hibernate代码

@Entity(name = "user_model")
public class UserModel implements Serializable { @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false,unique = true)
private String username;
@Column(nullable = true)
private String passWord;
@Column(nullable = true)
private String email;
@Column(nullable = true)
private String nickName;
@Column(nullable = true)
private String regTime;
@Column(nullable = true)
private String newname;
// 略 geter seter

EFCore 代码 ,原则上是可以不写标签的,这里就特殊自定义设置来说明

[Table("Tb_Room")]
public class RoomModel : EntityBase
{
/// <summary>
/// 教室名称
/// </summary>
[Column("Room_Name", Order = 1, TypeName = "varchar")]
public string RoomName { get; set; }
/// <summary>
/// 教室描述
/// </summary>
public string RoomDecription { get; set; }
/// <summary>
/// 排序
/// </summary>
public int Order { get; set; }
}

通过上面的代码可以看到 Hibernate中的 @Entity 注解 其实 很 EFCore中的 Table属性标签 是一个原理 包括用法其实都一样 列(Column)也是一样的用法,通过这些对比可以了解到Spring Data JPA 与 EFCore使用上的不同

在使用上 JPA 提供了相关的 仓储代码 JpaRepository 里面提供了很多操作方法 写法上有一些讲究(这里就略掉了.....)

同样 在 EFCore中 分装的DbSet<TEntity> 、IQueryable<TEntity> 也能提供相关操作

下面看下Java代码

public interface UserRepository extends JpaRepository<UserModel, Long> {
@Query("select u from user_model u")
Page<UserModel> findLYM(Pageable pageable); @Transactional(timeout = 10)
@Modifying
@Query("update user_model set username = ?1 where id = ?2")
int modifyById(String username, Long id);
}

这是自定义的方法,通过相关注解完成操作

下面来测试下 JDBC的代码以及 Spring Data JPA (Hibernate)的单元测试效果

@Test
public void testJDBCSql() throws Exception
{
int result= JDBCHelper.ExcuteUpdate("update user_model set username='123123123123' where id=4");
System.out.print(result);
} @Test
public void testJDBCQuery() throws Exception
{
JDBCHelper.ExcuteQuery("select * from user_model where id=1");
}

通过以上单元测试可以得到想要的效果

测试下 Hibernate 也能得到相关的效果,同时Hibernate还自动根据Enitty为我们创建好了相关的表结构 也是非常方便的 ,Hibernate中还分装了 分页 等各种操作

   @Test
public void addUser()
{
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
String formattedDate = dateFormat.format(date);
userRepository.save(new UserModel("zhangsan","zhangsan",
"805717775@qq.com","zhangsan",formattedDate,"ABC")); }
 @Test
public void getPager()
{
int page=1,size=10;
Sort sort = new Sort(Sort.Direction.DESC, "id");
Pageable pageable=new PageRequest(page,size,sort);
Page<UserModel> userModels= userRepository.findLYM(pageable);
for(UserModel item : userModels){
System.out.println(item.getNickName());
}
}

关于数据库操作这一块还可以用MyBatis ,下一章节继续对比 EFCore vs MyBatis  ORM 框架

Java Spring Boot VS .NetCore (四)数据库操作 Spring Data JPA vs EFCore的更多相关文章

  1. Java Spring Boot VS .NetCore (九) Spring Security vs .NetCore Security

    Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...

  2. Spring Boot系列(四):Spring Boot源码解析

    一.自动装配原理 之前博文已经讲过,@SpringBootApplication继承了@EnableAutoConfiguration,该注解导入了AutoConfigurationImport Se ...

  3. spring boot: spring-data-jpa (Repository/CrudRepository) 数据库操作, @Entity实体类持久化

    SpringBoot实现的JPA封装了JPA的特性, Repository是封装了jpa的特性(我是这么理解的) 1在pom.xml引入mysql, spring-data-jpa依赖 2.在src/ ...

  4. Spring Boot 实用MyBatis做数据库操作

    前言: 本项目基于maven构建,使用mybatis-spring-boot作为spring-boot项目的持久层框架 spring-boot中使用mybatis持久层框架与原spring项目使用方式 ...

  5. spring boot web 开发及数据库操作

    推荐网站http://springboot.fun/ 1.json 接口开发 2.自定义 filter 3.自定义 property 4.log 配置 5.数据库操作 6.测试

  6. Java Spring Boot VS .NetCore (一)来一个简单的 Hello World

    系列文章 Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filte ...

  7. Java Spring Boot VS .NetCore (二)实现一个过滤器Filter

    Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...

  8. Java Spring Boot VS .NetCore (三)Ioc容器处理

    Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...

  9. Java Spring Boot VS .NetCore (五)MyBatis vs EFCore

    Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...

随机推荐

  1. Java中反射机制详解

    序言 在学习java基础时,由于学的不扎实,讲的实用性不强,就觉得没用,很多重要的知识就那样一笔带过了,像这个马上要讲的反射机制一样,当时学的时候就忽略了,到后来学习的知识中,很多东西动不动就用反射, ...

  2. HTTP/1.0 vs HTTP/1.1 vs HTTP/2

    HTTP 1.0 vs 1.1 Proxy support and the Host field: HTTP 1.1 has a required Host header by spec. HTTP ...

  3. Fixing “Did you mean to run dotnet SDK commands?” error when running dotnet –version

    I recently installed the dotnet 1.11.0 Windows Server Hosting package which apparently installs the ...

  4. 【数学建模】偏最小二乘回归分析(PLSR)

    PLSR的基本原理与推导,我在这篇博客中有讲过. 0.偏最小二乘回归集成了多元线性回归.主成分分析和典型相关分析的优点,在建模中是一个更好的选择,并且MATLAB提供了完整的实现,应用时主要的问题是: ...

  5. [JSOI2008]Blue Mary开公司[李超线段树]

    题面 bzoj luogu 好久以前听lxl讲过 咕掉了.. 竟然又遇到了 安利blog #include <cmath> #include <cstring> #includ ...

  6. webpack学习记录 - 学习webpack-dev-server(三)

    怎么用最简单的方式搭建一个服务器? 首先安装插件 npm i --save-dev webpack-dev-server 然后修改 packet.json 文件 "scripts" ...

  7. JMeter 不同线程组间变量传递

    JMeter元件都是有作用域的,而变量大多使用正则表达式提取器,要想在不通过线程组件使用变量参数,则需要设置全部变量 JMeter函数助手就提供了一个函数用于设置全局变量属性,实现的功能类似于在用户自 ...

  8. linux log4j乱码问号的解决

    原因: linux本地设置的文件编码格式不是UTF-8 解决办法: 运行locale命令看一下结果: 把LC_CTYPE修改为“zh_CN.UTF-8”: cd ~/ vi .bashrc 添加: L ...

  9. DMA设计

    目录 DMA设计 DMA框架 手册请看英文手册 芯片特性 请求来源 协议简述 基本时序 模式 协议 数据大小的描述 具体完整的实例时序 代码设计 驱动程序 测试程序 测试 参考链接 title: DM ...

  10. Python实现聚类算法AP

    1.算法简介 AP(Affinity Propagation)通常被翻译为近邻传播算法或者亲和力传播算法,是在2007年的Science杂志上提出的一种新的聚类算法.AP算法的基本思想是将全部数据点都 ...