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. Nginx整合tomcat,实现反向代理和负载均衡

    1.Nginx与Tomcat整合,通过Nginx反向代理Tomcat. Nginx安装路径为:/usr/local//nginx 首先切换路径到:/usr/local//nginx/conf通过命令  ...

  2. Insert Into select 与 Select Into 哪个更快?

    在平常数据库操作的时候,我们有时候会遇到表之间数据复制的情况,可能会用到INSERT INTO SELECT 或者 SELECT INTO : 那么二者语法上有什么区别?性能上又如何呢? 围绕着这两个 ...

  3. Django+Vue打造购物网站(十)

    首页.商品数量.缓存和限速功能开发 将环境切换为本地,vue也切换为本地 轮播图 goods/serializers.py class BannerSerializer(serializers.Mod ...

  4. python之路day02--格式化输出、初始编码、运算符

    格式化输出 格式化输出替换字符串.字符串中%占位符,,%%s就是代表百分号,不代表占位符s 字符串 stringd 数字 dight name = input('请输入你的名字:') age = in ...

  5. Tomcat 部署java web项目直接ip地址访问项目

    正常情况下,在访问在Tomcat中部署的项目是 http://localhost:8080/demo 方式 其中,IP,端口,项目名(Demo)都是必须的. 那么,怎么样才能通过 http://loc ...

  6. C#图片操作公共库

    存一下,以后找起来方便 包括图片加载.压缩.base64等 public static class ImageFun { #region 图片 public static EncoderParamet ...

  7. A Graph Partitioning Game Theoretical Approach for the VNF Service Chaining Problem

    文章名称:A Graph Partitioning Game Theoretical Approach for the VNF Service Chaining Problem 发表时间:2017 期 ...

  8. 结合别人的文章,做RocketMQ的一点原理分析,结合源码(尽量)----未完待续

    Broker 与Namesrv的关系 1.从namesrv获取配置信息 /** * BrokerConfig类 * * broker每隔30秒(此时间无法更改)向所有nameserver发送心跳,心跳 ...

  9. JavaScript Date日期对象以及日期格式化方法

    前言 Date对象是javascript语言中内置的数据类型,用于提供日期和时间的操作接口.Date对象是在早期java中的java.util.Date类基础上创建的,为此,Date类型使用自UTC1 ...

  10. Jupyter NoteBook功能介绍

    一.Jupyter Notebook 介绍 文学编程 在介绍 Jupyter Notebook 之前,让我们先来看一个概念:文学编程 ( Literate programming ),这是由 Dona ...