我想知道社区认为使用Spring JDBC映射类层次结构的"最佳实践".
我们没有能力使用完整的ORM工具,但我们使用Spring JDBC来缓解JDBC的一些乏味特性.我们非常经常使用的一个类是BeanPropertyRowMapper,因为它易于使用,并且能够从我们的结果集中访问类型不敏感的bean属性.
我有一个类层次结构,它都映射回一个表(对于这个小类层次结构采用table-per-hiearchy方法).因此,该表包含一个classId列,可用于确定实际应该实例化哪个类.防爆.1 =经理,2 =员工,3 =承包商.所有这些都是"人",但每个人的子类都有一些属性,这些属性对于他们的类是唯一的.
我最初的想法是创建一个BeanPropertyRowMapper的子类,并尝试注入这个逻辑,说"如果列A = 1然后实例化一个管理器,然后进行你的标题绑定".
这看起来像是一种合理的方法吗?人们可能有任何其他建议吗?
提前感谢您的回复,
贾斯汀N.
我不确定发生了什么.我有以下基类:
public class MyRow : IStringIndexable, System.Collections.IEnumerable,
ICollection<KeyValuePair<string, string>>,
IEnumerable<KeyValuePair<string, string>>,
IDictionary<string, string>
{
ICollection<string> IDictionary<string, string>.Keys { }
}
Run Code Online (Sandbox Code Playgroud)
然后我有这个派生类:
public class MySubRow : MyRow, IXmlSerializable, ICloneable,
IComparable, IEquatable<MySubRow>
{
public bool Equals(MySubRow other)
{
// "MyRow does not contain a definition for 'Keys'"
foreach (string key in base.Keys) { }
}
}
Run Code Online (Sandbox Code Playgroud)
为什么我会收到这个错误?"'MyNamespace.MyRow'不包含'Keys'的定义".这两个类都在MyNamespace命名空间中.我试过访问this.Keys,base.Keys并且都没有从内部工作MySubRow.我尝试将Keys属性标记为public,MyRow但得到"修饰符'公共'对此项无效",我认为因为有必要实现一个接口.
我在一个文件中定义了一个抽象超类,在另一个文件中定义了一个子类.我需要超类文件和堆栈跟踪报告来查找包含它.
然而,当它到达'extends'行时它会返回一个错误:Fatal error: Class 'HTMLBuilder' not found in View/Markup/HTML/HTML4.01/HTML4_01Builder.php on line 7.
我刚才使用了另一个使用工厂的类树.我刚刚在工厂和消费者之间添加了构建器层.就包含和依赖性而言,工厂层看起来几乎完全相同.
所以这让我觉得我必须做一些愚蠢的事情,导致HTMLBuilder.php文件没有被正确包含或正确解释或其他一些.
这是完整的堆栈跟踪(路径略有改变):
# Time Memory Function Location
1 0.0001 53904 {main}( ) ../index.php:0
2 0.0002 67600 require_once( 'View/Page.php' ) ../index.php:3
3 0.0003 75444 require_once( 'View/Sections/SectionFactory.php' ) ../Page.php:4
4 0.0003 81152 require_once( 'View/Sections/HTML/HTMLSectionFactory.php' ) ../SectionFactory.php:3
5 0.0004 92108 require_once( 'View/Sections/HTML/HTMLTitlebarSection.php' ) ../HTMLSectionFactory.php:5
6 0.0005 99716 require_once( 'View/Markup/HTML/HTMLBuilder.php' ) ../HTMLTitlebarSection.php:3
7 0.0005 103580 require_once( 'View/Markup/MarkupBuilder.php' ) ../HTMLBuilder.php:3
8 0.0006 124120 require_once( 'View/Markup/HTML/HTML4.01/HTML4_01Builder.php' ) ../MarkupBuilder.php:3
Run Code Online (Sandbox Code Playgroud)
这是有问题的代码:
父类(View/Markup/HTML/HTMLBuilder.php):
<?php …Run Code Online (Sandbox Code Playgroud) 我想从存储在另一个系统中的树中检索值.例如:
GetValue("Vehicle.Car.Ford.Focus.Engine.Oil.Color")
Run Code Online (Sandbox Code Playgroud)
为了避免键入错误和无效键,我想在编译时通过创建一个具有树结构的对象或类来检查名称:
GetValue(Vehicle.Car.Ford.Focus.Engine.Oil.Color)
Run Code Online (Sandbox Code Playgroud)
有没有一种简单的方法在C#中执行此操作而不为每个树节点创建类?我可以使用匿名类或子类吗?我应该自动创建代码吗?
我有一个容器,它包含一堆指向基类的指针,以及一个接受一些输入并返回一个类的函数,该类是基类的子类.它返回的子类取决于输入.
现在,我有一个像这样的巨型开关语句:
class Base { ... }
class A : public Base { ... }
class B : public Base { ... }
...
class Z : public Base { ... }
Base* depends(int input) {
switch (input) {
case 1:
return new A(...);
case 2:
return new B(...);
...
case 26:
return new Z(...);
default:
...
}
}
Run Code Online (Sandbox Code Playgroud)
我想知道是否有更好的方法来设计它.我不知道很多"设计模式"(我认为这就是他们所谓的)所以我不知道是否有一种(明显的)更好的方法来设计它.
c++ inheritance construction class-hierarchy switch-statement
假设我想看看如何在Dictionary类中实现"复制".目前我使用系统浏览器并手动遍历继承层次结构(自下而上),直到找到实现给定消息的类.是否有工作空间的单行,这将在正确的位置打开系统浏览器?
我有一个场景如下:
@Entity
@Table(name = "ANIMAL")
@Inheritance(strategy = InheritanceType.JOINED)
public class Animal implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "S_ANIMAL")
@SequenceGenerator(name = "S_ANIMAL", sequenceName = "S_ANIMAL", allocationSize = 1)
public int getNumero() {
return numero;
}
public void setNumero(int numero) {
this.numero = numero;
}
.
.
.
}
Run Code Online (Sandbox Code Playgroud)
并作为子类:
@Entity
@Table(name = "DOG")
public class Dog extends Animal {
private static final long serialVersionUID = -7341592543130659641L;
.
.
.
}
Run Code Online (Sandbox Code Playgroud)
我有一个像这样的 JPA Select 语句:
SELECT a FROM Animal …Run Code Online (Sandbox Code Playgroud) 我最近在查看一些开源代码PicketLink代码.如果你看一下这个类,你会在抽象类中看到一些什么都不做的具体方法.这有什么用途吗?
我想到了两件事:
当类层次结构不是线性时,在基接口上定义时不会触发方面.
最有趣的是:当将实现的委托实现(参见最后一个代码块)添加到实现的父类时,测试变为绿色(Aspect按预期触发).
问题:为什么它不能像示例中所描述的那样工作,为什么它与委托实现一起工作?
示例(抱歉,没有找到更短的示例):
测试:
@Autowired
private TheInterface underTest;
private static boolean aspectCalled;
private static boolean implementationCalled;
@Test
public void aspectTest() throws Exception {
aspectCalled = false;
implementationCalled = false;
underTest.doSomething();
assertTrue("Implementation not called!", implementationCalled);
assertTrue("Aspect not called!", aspectCalled);
}
Run Code Online (Sandbox Code Playgroud)
方面:
@Aspect
@Component
public static class MyAspect {
@Before("execution(* *..SpecializedInterface+.doSomething())")
public void applyAspect() {
aspectCalled = true;
}
}
Run Code Online (Sandbox Code Playgroud)
接口:
public static interface TheInterface {
void doSomething();
}
public static interface SpecializedInterface extends TheInterface {
// inherits doSomething
// defines …Run Code Online (Sandbox Code Playgroud) 考虑下面的一段代码
#include <algorithm>
#include <iostream>
#include <memory>
#include <vector>
struct Base {
int x;
Base(int x) : x(x) {}
};
struct Derived : public Base {
int y, z;
Derived(int x) : Base(x), y(x + 1), z(x + 2) {}
};
void update(const std::vector<std::shared_ptr<const Base>>& elements) {
for (const auto elem : elements) {
std::cout << elem->x << "\n";
}
}
int main(int, char**) {
std::vector<std::shared_ptr<Derived>> elements(4);
{
int ctr = 0;
std::generate(begin(elements), end(elements), [&ctr]() { return std::make_shared<Derived>(++ctr); });
} …Run Code Online (Sandbox Code Playgroud) class-hierarchy ×10
java ×4
c# ×2
c++ ×2
inheritance ×2
spring ×2
aop ×1
aspect ×1
compile-time ×1
construction ×1
hibernate ×1
ide ×1
interface ×1
jpa ×1
oop ×1
orm ×1
pharo ×1
php ×1
require-once ×1
smalltalk ×1
spring-jdbc ×1
squeak ×1
tree ×1