C# xaml框架以及Java的ORM介绍

2024-03-22 05:04

本文主要是介绍C# xaml框架以及Java的ORM介绍,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

c#有ASP.Net,.NET以及EF Core这几个重要的运行时和框架.分别用于web,应用以及数据库的ORM.

img

目前跨平台的有Avalonia UI,.Net MAUI以及Uno Platform,至于WPF等本身不是跨平台的,但可以依靠其他库实现跨平台.这里面Avalonia应该是认为bug比较少的.

当然目前最火的跨平台解决方案应该是Flutter,其次是React Native.

至于EF Core,看看代码就知道有多么简洁优雅了.

using var db = new BloggingContext();// Inserting data into the database
db.Add(new Blog { Url = "http://blogs.msdn.com/adonet" });
db.SaveChanges();// Querying
var blog = db.Blogs.OrderBy(b => b.BlogId).First();// Updating
blog.Url = "https://devblogs.microsoft.com/dotnet";
blog.Posts.Add(new Post{Title = "Hello World",Content = "I wrote an app using EF Core!"});
db.SaveChanges();// Deleting
db.Remove(blog);
db.SaveChanges();

而Java的ORM有Mybatis,Mybatis-plus,Hibernate,Spring Data JPA以及更原始的Jdbctemplate和query dsl.

具体来说,当使用Java和MyBatis编写示例代码时,需要配置MyBatis的环境和数据库连接,并编写映射器接口和SQL映射文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//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.jdbc.Driver"/><property name="url" value="jdbc:mysql://localhost:3306/mydatabase"/><property name="username" value="root"/><property name="password" value="password"/></dataSource></environment></environments><mappers><mapper resource="com/example/MyMapper.xml"/></mappers>
</configuration>
package com.example;public class User {private int id;private String name;private int age;// getters and setters
}import java.util.List;public interface UserMapper {void insertUser(User user);void updateUser(User user);void deleteUser(int id);User getUserById(int id);List<User> getAllUsers();
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.UserMapper"><insert id="insertUser" parameterType="com.example.User">INSERT INTO users (name, age) VALUES (#{name}, #{age})</insert><update id="updateUser" parameterType="com.example.User">UPDATE users SET name = #{name}, age = #{age} WHERE id = #{id}</update><delete id="deleteUser" parameterType="int">DELETE FROM users WHERE id = #{id}</delete><select id="getUserById" parameterType="int" resultType="com.example.User">SELECT * FROM users WHERE id = #{id}</select><select id="getAllUsers" resultType="com.example.User">SELECT * FROM users</select>
</mapper>
package com.example;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 Main {public static void main(String[] args) {try {// 加载 MyBatis 配置文件String resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);// 创建 SqlSessionSqlSession sqlSession = sqlSessionFactory.openSession();// 获取 UserMapper 接口的实例UserMapper userMapper = sqlSession.getMapper(UserMapper.class);// 插入用户User newUser = new User();newUser.setName("John");newUser.setAge(25);userMapper.insertUser(newUser);System.out.println("Inserted user: " + newUser);// 更新用户User existingUser = userMapper.getUserById(newUser.getId());existingUser.setName("John Doe");userMapper.updateUser(existingUser);System.out.println("Updated user: " + existingUser);// 获取所有用户List<User> allUsers = userMapper.getAllUsers();System.out.println("All users:");for (User user : allUsers) {System.out.println(user);}// 删除用户userMapper.deleteUser(existingUser.getId());System.out.println("Deleted user with ID: " + existingUser.getId());// 提交事务sqlSession.commit();// 关闭 SqlSessionsqlSession.close();} catch (IOException e) {e.printStackTrace();}}
}

使用MyBatis-Plus编写示例代码时,可以省去编写SQL映射文件,因为MyBatis-Plus提供了便捷的CRUD操作方法和自动生成SQL语句的功能。

package com.example;import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;@TableName("users")
public class User {@TableId(type = IdType.AUTO)private Long id;private String name;private Integer age;// getters and setters
}import com.baomidou.mybatisplus.core.mapper.BaseMapper;public interface UserMapper extends BaseMapper<User> {
}
package com.example;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;@SpringBootApplication
public class Main {public static void main(String[] args) {// 启动 Spring Boot 应用ConfigurableApplicationContext context = SpringApplication.run(Main.class, args);// 获取 UserMapper BeanUserMapper userMapper = context.getBean(UserMapper.class);// 插入用户User newUser = new User();newUser.setName("John");newUser.setAge(25);userMapper.insert(newUser);System.out.println("Inserted user: " + newUser);// 更新用户User existingUser = userMapper.selectById(newUser.getId());existingUser.setName("John Doe");userMapper.updateById(existingUser);System.out.println("Updated user: " + existingUser);// 查询所有用户QueryWrapper<User> queryWrapper = new QueryWrapper<>();queryWrapper.orderByAsc("id");IPage<User> userPage = userMapper.selectPage(new Page<>(1, 10), queryWrapper);System.out.println("All users:");for (User user : userPage.getRecords()) {System.out.println(user);}// 删除用户userMapper.deleteById(existingUser.getId());System.out.println("Deleted user with ID: " + existingUser.getId());// 关闭应用上下文context.close();}
}

使用了MyBatis-Plus的注解 @TableName 来指定实体类与数据库表的映射关系,使用了 @TableId 注解来定义主键字段。UserMapper 接口继承了 BaseMapper<User>,这样就可以直接使用MyBatis-Plus提供的CRUD操作方法。

使用Hibernate编写示例代码时,需要配置Hibernate的环境和数据库连接,并编写实体类和Hibernate映射文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration><session-factory><property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property><property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property><property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mydatabase</property><property name="hibernate.connection.username">root</property><property name="hibernate.connection.password">password</property><property name="hibernate.hbm2ddl.auto">update</property><property name="hibernate.show_sql">true</property><mapping resource="com/example/User.hbm.xml"/></session-factory>
</hibernate-configuration>
package com.example;import javax.persistence.*;@Entity
@Table(name = "users")
public class User {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;@Column(name = "name")private String name;@Column(name = "age")private Integer age;// getters and setters
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping><class name="com.example.User" table="users"><id name="id" column="id"><generator class="identity"/></id><property name="name" column="name"/><property name="age" column="age"/></class>
</hibernate-mapping>
package com.example;import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;import java.util.List;public class Main {public static void main(String[] args) {// 加载 Hibernate 配置Configuration configuration = new Configuration().configure();SessionFactory sessionFactory = configuration.buildSessionFactory();// 创建 SessionSession session = sessionFactory.openSession();// 开启事务Transaction transaction = session.beginTransaction();// 插入用户User newUser = new User();newUser.setName("John");newUser.setAge(25);session.save(newUser);System.out.println("Inserted user: " + newUser);// 更新用户User existingUser = session.get(User.class, newUser.getId());existingUser.setName("John Doe");session.update(existingUser);System.out.println("Updated user: " + existingUser);// 查询所有用户List<User> allUsers = session.createQuery("FROM User", User.class).getResultList();System.out.println("All users:");for (User user : allUsers) {System.out.println(user);}// 删除用户session.delete(existingUser);System.out.println("Deleted user with ID: " + existingUser.getId());// 提交事务transaction.commit();// 关闭 Sessionsession.close();// 关闭 SessionFactorysessionFactory.close();}
}

使用Hibernate的注解 @Entity@Table@Id@Column 来定义实体类与数据库表的映射关系和字段信息。Hibernate映射文件 User.hbm.xml 也定义了实体类与数据库表的映射关系。

当使用Spring Data JPA编写示例代码时,可以利用Spring Data JPA提供的接口和方法来进行数据库操作,而无需编写大量的重复代码。

package com.example;import javax.persistence.*;@Entity
@Table(name = "users")
public class User {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;@Column(name = "name")private String name;@Column(name = "age")private Integer age;// getters and setters
}
import org.springframework.data.jpa.repository.JpaRepository;public interface UserRepository extends JpaRepository<User, Long> {
}
package com.example;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;import java.util.List;@SpringBootApplication
public class Main {public static void main(String[] args) {// 启动 Spring Boot 应用ConfigurableApplicationContext context = SpringApplication.run(Main.class, args);// 获取 UserRepository BeanUserRepository userRepository = context.getBean(UserRepository.class);// 插入用户User newUser = new User();newUser.setName("John");newUser.setAge(25);userRepository.save(newUser);System.out.println("Inserted user: " + newUser);// 更新用户User existingUser = userRepository.findById(newUser.getId()).orElse(null);existingUser.setName("John Doe");userRepository.save(existingUser);System.out.println("Updated user: " + existingUser);// 查询所有用户List<User> allUsers = userRepository.findAll();System.out.println("All users:");for (User user : allUsers) {System.out.println(user);}// 删除用户userRepository.deleteById(existingUser.getId());System.out.println("Deleted user with ID: " + existingUser.getId());// 关闭应用上下文context.close();}
}

此外使用JPA与querydsl结合的也很多.QueryDSL仅仅是一个通用的查询框架,专注于通过 JavaAPI 构建类型安全的 Sql 查询

package com.example;import javax.persistence.*;@Entity
@Table(name = "users")
public class User {@Id@GeneratedValue(strategy = GenerationType.IDENTITY)private Long id;@Column(name = "name")private String name;@Column(name = "age")private Integer age;// getters and setters
}
package com.example;import com.querydsl.core.types.dsl.StringExpression;
import com.querydsl.core.types.dsl.StringPath;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.stereotype.Repository;@Repository
public interface UserRepository extends JpaRepository<User, Long>, QuerydslPredicateExecutor<User> {// 定义自定义查询方法default User findByName(String name) {JPAQueryFactory queryFactory = new JPAQueryFactory(getEntityManager());QUser user = QUser.user;return queryFactory.selectFrom(user).where(user.name.eq(name)).fetchOne();}// 使用Querydsl的字符串表达式进行模糊查询default Iterable<User> findByNameLike(String name) {JPAQueryFactory queryFactory = new JPAQueryFactory(getEntityManager());QUser user = QUser.user;StringExpression nameExpression = user.name;return queryFactory.selectFrom(user).where(nameExpression.likeIgnoreCase("%" + name + "%")).fetch();}
}
package com.example;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;import java.util.List;@SpringBootApplication
public class Main {public static void main(String[] args) {// 启动 Spring Boot 应用ConfigurableApplicationContext context = SpringApplication.run(Main.class, args);// 获取 UserRepository BeanUserRepository userRepository = context.getBean(UserRepository.class);// 插入用户User newUser = new User();newUser.setName("John");newUser.setAge(25);userRepository.save(newUser);System.out.println("Inserted user: " + newUser);// 更新用户User existingUser = userRepository.findByName("John");existingUser.setName("John Doe");userRepository.save(existingUser);System.out.println("Updated user: " + existingUser);// 查询所有用户List<User> allUsers = userRepository.findAll();System.out.println("All users:");for (User user : allUsers) {System.out.println(user);}// 根据名称模糊查询用户List<User> usersWithNameLike = userRepository.findByNameLike("John");System.out.println("Users with name like 'John':");for (User user : usersWithNameLike) {System.out.println(user);}// 删除用户userRepository.delete(existingUser);System.out.println("Deleted user with ID: " + existingUser.getId());// 关闭应用上下文context.close();}
}

这篇关于C# xaml框架以及Java的ORM介绍的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/834583

相关文章

Java中的String.valueOf()和toString()方法区别小结

《Java中的String.valueOf()和toString()方法区别小结》字符串操作是开发者日常编程任务中不可或缺的一部分,转换为字符串是一种常见需求,其中最常见的就是String.value... 目录String.valueOf()方法方法定义方法实现使用示例使用场景toString()方法方法

Java中List的contains()方法的使用小结

《Java中List的contains()方法的使用小结》List的contains()方法用于检查列表中是否包含指定的元素,借助equals()方法进行判断,下面就来介绍Java中List的c... 目录详细展开1. 方法签名2. 工作原理3. 使用示例4. 注意事项总结结论:List 的 contain

C#使用SQLite进行大数据量高效处理的代码示例

《C#使用SQLite进行大数据量高效处理的代码示例》在软件开发中,高效处理大数据量是一个常见且具有挑战性的任务,SQLite因其零配置、嵌入式、跨平台的特性,成为许多开发者的首选数据库,本文将深入探... 目录前言准备工作数据实体核心技术批量插入:从乌龟到猎豹的蜕变分页查询:加载百万数据异步处理:拒绝界面

Java实现文件图片的预览和下载功能

《Java实现文件图片的预览和下载功能》这篇文章主要为大家详细介绍了如何使用Java实现文件图片的预览和下载功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... Java实现文件(图片)的预览和下载 @ApiOperation("访问文件") @GetMapping("

C#数据结构之字符串(string)详解

《C#数据结构之字符串(string)详解》:本文主要介绍C#数据结构之字符串(string),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录转义字符序列字符串的创建字符串的声明null字符串与空字符串重复单字符字符串的构造字符串的属性和常用方法属性常用方法总结摘

C#如何动态创建Label,及动态label事件

《C#如何动态创建Label,及动态label事件》:本文主要介绍C#如何动态创建Label,及动态label事件,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C#如何动态创建Label,及动态label事件第一点:switch中的生成我们的label事件接着,

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis

SpringCloud动态配置注解@RefreshScope与@Component的深度解析

《SpringCloud动态配置注解@RefreshScope与@Component的深度解析》在现代微服务架构中,动态配置管理是一个关键需求,本文将为大家介绍SpringCloud中相关的注解@Re... 目录引言1. @RefreshScope 的作用与原理1.1 什么是 @RefreshScope1.

Java并发编程必备之Synchronized关键字深入解析

《Java并发编程必备之Synchronized关键字深入解析》本文我们深入探索了Java中的Synchronized关键字,包括其互斥性和可重入性的特性,文章详细介绍了Synchronized的三种... 目录一、前言二、Synchronized关键字2.1 Synchronized的特性1. 互斥2.

Spring Boot 配置文件之类型、加载顺序与最佳实践记录

《SpringBoot配置文件之类型、加载顺序与最佳实践记录》SpringBoot的配置文件是灵活且强大的工具,通过合理的配置管理,可以让应用开发和部署更加高效,无论是简单的属性配置,还是复杂... 目录Spring Boot 配置文件详解一、Spring Boot 配置文件类型1.1 applicatio