详解System.Nullable<值类型>

.net framework 2.0包含了一个泛型类型定义,它满足了两种情况的要求:可以将一个值类型的实例赋值为null,并且可以判断该实例的值是否为null,该泛型类型就是System.Nullable<T>,其中T被限制为值类型。从System.Nullable<T>构造而来的类型称为可控值类型(nullable value type).

System.Nullable<T>有一个属性value,当一个可空值类型的实例的值不为null时,可以通过这个属性获得该类型实例的值。所以我们可以写出类似下面这样的代码:

System.Nullable<int> myNullableInteger=null

myNullableInteger=1;

if(myNullableInteger!=null)

{

  Console.WriteLine(myNullableInteger.Value);

}

C#语言提供了一种简化语法来声明从System.Nullable<T>构造的类型。该

语法允许将:

System.Nullable<int> myNullableInteger;

简写成:

int? myNullableInteger;

 

编译器不允许将一个可空值类型的值直接赋值给普通的值类型,例如下面的代码是不能通过编译的:

int? myNullabelInteger=null;

int myInteger=myNullabelInteger;

因为可空值类型的值可以是null,而null不可以赋给普通值类型。

 

虽然编译器允许下面这样的代码:

int? myNullableInteger=null;

int myInteger=myNullableInteger.Value;

但是第二行代码的运行将会抛出异常,因为当从System.NUllable<T> 构造的类型没有赋予一个有效的T类型的值时,任何对其System.Nullable<T>.Value属性的访问都是无效操作,上面这个例子就是事先没有赋值。

 

 

将一个可空值类型的值赋给普通类型的方法是:先用System.Nullable<T>.Hasvalue属性判断这个可空值类型是否被赋过一个类型为T的有效值:

int? myNullableInteger=null;

if(myNullableInteger.HasValue)

{

  int myInteger=myNullableInteger.Value;

}

 

 

另外一种方法是使用下面的语法:

int? myNullableInteger=null;

int myInteger=myNullableInteger??-1;

如果后者(myNullableInteger)被赋予有效的整型值,那么普通整型myInteger将被赋予可空整型值myNullableInteger的值,否则myInteger将被赋值为-1;

在基于SpringBoot的社区养老健康服务系统设计中,使用Eclipse连接MySQL数据库可按以下步骤进行: ### 1. 创建Spring Boot项目 在Eclipse中,可通过Spring Initializr创建一个新的Spring Boot项目。选择合适的项目元数据,如Group、Artifact等,并添加必要的依赖,主要包括Spring Web、Spring Data JPA、MySQL Driver等。这些依赖能支持项目进行Web开发、数据库交互等操作。 ### 2. 配置项目的pom.xml文件 确保在`pom.xml`文件中包含以下依赖,以支持Spring Boot与MySQL数据库的交互: ```xml <dependencies> <!-- Spring Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Data JPA --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- MySQL Driver --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies> ``` ### 3. 配置数据库连接信息 在`src/main/resources`目录下找到`application.properties`或`application.yml`文件,配置MySQL数据库的连接信息。以`application.properties`为例,配置如下: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/community_elderly_health_service?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC spring.datasource.username=your_username spring.datasource.password=your_password spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true ``` 上述配置中,`spring.datasource.url`指定了数据库的连接地址,需要将数据库名`community_elderly_health_service`根据实际情况修改;`spring.datasource.username`和`spring.datasource.password`分别为数据库的用户名和密码;`spring.datasource.driver-class-name`指定了MySQL数据库的驱动类;`spring.jpa.hibernate.ddl-auto`设置为`update`,表示Hibernate会自动根据实体类更新数据库表结构;`spring.jpa.show-sql`设置为`true`,会在控制台打印执行的SQL语句,方便调试。 ### 4. 测试数据库连接 可以创建一个简单的测试类来验证数据库连接是否成功。例如: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import javax.sql.DataSource; import java.sql.Connection; import java.sql.SQLException; @SpringBootApplication public class CommunityElderlyHealthServiceApplication implements CommandLineRunner { @Autowired private DataSource dataSource; public static void main(String[] args) { SpringApplication.run(CommunityElderlyHealthServiceApplication.class, args); } @Override public void run(String... args) throws Exception { try (Connection connection = dataSource.getConnection()) { System.out.println("Database connection successful!"); } catch (SQLException e) { System.out.println("Database connection failed: " + e.getMessage()); } } } ``` 运行该Spring Boot应用程序,如果控制台输出`Database connection successful!`,则表示Eclipse成功连接到了MySQL数据库。
10-31
Exception setting up node for Apparel_Pants on Robert: System.ArgumentException: Illegal characters in path. [Ref 67275643] Duplicate stacktrace, see ref for original UnityEngine.StackTraceUtility:ExtractStackTrace () (wrapper dynamic-method) MonoMod.Utils.DynamicMethodDefinition:Verse.Log.Error_Patch3 (string) (wrapper dynamic-method) MonoMod.Utils.DynamicMethodDefinition:Verse.PawnRenderTree.SetupApparelNodes_Patch0 (Verse.PawnRenderTree) (wrapper dynamic-method) MonoMod.Utils.DynamicMethodDefinition:Verse.PawnRenderTree.SetupDynamicNodes_Patch1 (Verse.PawnRenderTree) (wrapper dynamic-method) MonoMod.Utils.DynamicMethodDefinition:Verse.PawnRenderTree.TrySetupGraphIfNeeded_Patch1 (Verse.PawnRenderTree) (wrapper dynamic-method) MonoMod.Utils.DynamicMethodDefinition:Verse.PawnRenderTree.EnsureInitialized_Patch1 (Verse.PawnRenderTree,Verse.PawnRenderFlags) (wrapper dynamic-method) MonoMod.Utils.DynamicMethodDefinition:Verse.PawnRenderer.RenderCache_Patch0 (Verse.PawnRenderer,Verse.Rot4,single,UnityEngine.Vector3,bool,bool,bool,bool,System.Collections.Generic.IReadOnlyDictionary`2<RimWorld.Apparel, UnityEngine.Color>,System.Nullable`1<UnityEngine.Color>,bool) RimWorld.PawnCacheRenderer:OnPostRender () (wrapper dynamic-method) MonoMod.Utils.DynamicMethodDefinition:RimWorld.PawnCacheRenderer.RenderPawn_Patch1 (RimWorld.PawnCacheRenderer,Verse.Pawn,UnityEngine.RenderTexture,UnityEngine.Vector3,single,single,Verse.Rot4,bool,bool,bool,bool,UnityEngine.Vector3,System.Collections.Generic.IReadOnlyDictionary`2<RimWorld.Apparel, UnityEngine.Color>,System.Nullable`1<UnityEngine.Color>,bool) RimWorld.PortraitsCache/PortraitParams:RenderPortrait (Verse.Pawn,UnityEngine.RenderTexture) RimWorld.PortraitsCache:Get (Verse.Pawn,UnityEngine.Vector2,Verse.Rot4,UnityEngine.Vector3,single,bool,bool,bool,bool,System.Collections.Generic.IReadOnlyDictionary`2<RimWorld.Apparel, UnityEngine.Color>,System.Nullable`1<UnityEngine.Color>,bool,System.Nullable`1<Verse.PawnHealthState>) (wrapper dynamic-method) MonoMod.Utils.DynamicMethodDefinition:RimWorld.ColonistBarColonistDrawer.DrawColonist_Patch1 (RimWorld.ColonistBarColonistDrawer,UnityEngine.Rect,Verse.Pawn,Verse.Map,bool,bool) RimWorld.ColonistBar:ColonistBarOnGUI () (wrapper dynamic-method) MonoMod.Utils.DynamicMethodDefinition:RimWorld.MapInterface.MapInterfaceOnGUI_BeforeMainTabs_Patch1 (RimWorld.MapInterface) (wrapper dynamic-method) MonoMod.Utils.DynamicMethodDefinition:RimWorld.UIRoot_Play.UIRootOnGUI_Patch3 (RimWorld.UIRoot_Play) (wrapper dynamic-method) MonoMod.Utils.DynamicMethodDefinition:Verse.Root.OnGUI_Patch2 (Verse.Root)
03-19
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值