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. Shell命令-文件及内容处理之diff、vimdiff

    文件及内容处理 - diff.vimdiff 1. diff:比较两个文件区别 diff命令的功能说明 diff命令用比较文件的差异.diff以逐行的方式,比较文本文件的异同处.如果指定要比较目录,则 ...

  2. VMware Workstation 12 Pro安装CentOs图文教程(超级详细)

    本文记录了VMware Workstation 12 Pro安装CentOs的整个过程,具体如下: VMware Workstation 12: CENTOS 6.4 : 创建虚拟机 1.首先安装好V ...

  3. Flask上下文管理、session原理和全局g对象

    一.一些python的知识 1.偏函数 def add(x, y, z): print(x + y + z) # 原本的写法:x,y,z可以传任意数字 add(1,2,3) # 如果我要实现一个功能, ...

  4. ueditor 插件集成到 xadmin 中的相关操作

    安装 点击这里下载源码包 在相关的虚拟环境下安装源码方式安装 切入解压后路径进行 python setup.py install 注册 安装成功按照普通app一般注册在 django 程序的app 中 ...

  5. LoadRunner开发ftp协议接口之上传文件脚本

    Action() { //建立一个ftp对象 FTP ftp1=0; //建立FTP连接并登录 ftp_logon_ex(&ftp1,"ftpLogon", "U ...

  6. Excel将一列数据变为两列

    如下表可将第一列分散到第二列和第三列 A B C 1 =OFFSET($A$1,(ROW(A1)-1)*2+COLUMN(A1)-1,) &"" =OFFSET($A$2, ...

  7. python操作文件练习,配置haproxy

    在使用python操作文件的时候,特别是对于网络设备,通常操作配置文件,会简化配置量,配置文件加载到内存中,运行时使用的是内存中的配置,内存中配置修改后立即生效,如果不将配置内容保存到硬盘中,则下次重 ...

  8. MongoDB 高可用集群副本集+分片搭建

    MongoDB 高可用集群搭建 一.架构概况 192.168.150.129192.168.150.130192.168.150.131 参考文档:https://www.cnblogs.com/va ...

  9. json字符串CSS格式化

    其实JSON.stringify本身就可以将JSON格式化,具体的用法是: JSON.stringify(res, null, 2); //res是要JSON化的对象,2是spacing 如果想要效果 ...

  10. Java中谈尾递归--尾递归和垃圾回收的比较

    一.首先我们讲讲递归 1.递归的本质是,某个方法中调用了自身,本质还是调用了一个方法,只是这个方法正好是自身而已 2.递归因为是在自身中调用自身,所以会带来以下三个显著特点:    1.调用的是同一个 ...