我想使用mongodb实现通用DAO。我的观点是不要只创建一个多个存储库。这样代码就是实用和对象。我能够创建几个类和interfesów,但得到错误。
我的代码:
Dao.java
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mainLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Profile"
android:id="@+id/ProfHead"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:scaleType="fitCenter"
android:id="@+id/imageProfile"
android:src="@drawable/profile_icon"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:layout_above="@+id/contactsBtn"
android:layout_below="@+id/ProfHead" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Home Phone a"
android:id="@+id/anchor1"
android:visibility="invisible"
android:layout_alignLeft="@+id/nameHead"
android:layout_alignStart="@+id/nameHead" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Name"
android:id="@+id/nameHead"
android:layout_below="@+id/patProfHead"
android:layout_marginTop="200dp"
android:layout_marginLeft="20dp"
android:layout_alignParentLeft="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Contacts >"
android:id="@+id/contactsBtn"
android:layout_above="@+id/editName"
android:layout_toRightOf="@id/anchor1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:id="@+id/editName"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_below="@+id/nameHead"
android:layout_marginLeft="20dp"
android:layout_alignParentLeft="true"
android:text="Name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Surname"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginRight="20dp"
android:id="@+id/editSurname"
android:layout_below="@+id/nameHead"
android:layout_toRightOf="@id/editName"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Sex"
android:id="@+id/sexHead"
android:layout_marginTop="10dp"
android:layout_below="@id/editName"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/anchor1"
android:text="Sex"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_alignBottom="@id/sexHead"
android:id="@+id/radioGroup">
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Age"
android:id="@+id/ageHead"
android:layout_below="@id/sexHead"
android:layout_marginTop="10dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:text="Age"
android:id="@+id/editAge"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_toRightOf="@id/anchor1"
android:layout_alignBaseline="@id/ageHead"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="DOB"
android:id="@+id/dobHead"
android:layout_below="@id/ageHead"
android:layout_marginTop="10dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:text="DOB"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/editDob"
android:layout_toRightOf="@id/anchor1"
android:layout_alignBaseline="@id/dobHead"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Height"
android:id="@+id/heightHead"
android:layout_below="@id/dobHead"
android:layout_marginTop="10dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Height"
android:id="@+id/editHeight"
android:layout_toRightOf="@id/anchor1"
android:layout_alignBaseline="@id/heightHead"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Weight"
android:id="@+id/weightHead"
android:layout_below="@id/heightHead"
android:layout_marginTop="10dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Weight"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/editWeight"
android:layout_toRightOf="@id/anchor1"
android:layout_marginRight="20dp"
android:layout_alignBaseline="@id/weightHead"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Notes"
android:id="@+id/notesHead"
android:layout_below="@id/weightHead"
android:layout_marginTop="10dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Template >"
android:id="@+id/notesBtn"
android:layout_alignBaseline="@id/notesHead"
android:layout_toRightOf="@id/anchor1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/editNotes"
android:layout_below="@+id/notesBtn"
android:layout_marginLeft="20dp"
android:text="The notes go here"
android:layout_marginRight="20dp"
android:layout_alignParentLeft="true" />
</RelativeLayout>
</ScrollView>
NewsDao.java
public interface Dao<T, ID extends Serializable> extends MongoRepository<T, ID>{
}
AbstractService.java
public interface NewsDao extends Dao<News, Long> {
}
NewsService.java
public abstract class AbstractService<T, ID extends Serializable> implements
Service<T, ID> {
protected final Logger logger = LoggerFactory.getLogger(getClass());
protected Dao<T, ID> dao;
public AbstractService(Dao<T, ID> dao) {
this.dao = dao;
}
@Override
public T save(T entity) {
this.logger.debug("Create a new {} with information: {}", entity.getClass(),
entity.toString());
return this.dao.save(entity);
}
}
Service.java
@Service
public class NewsService extends AbstractService<News, Long> {
@Autowired
public NewsService(NewsDao moduleDao) {
super(moduleDao);
}
}
NewsController.java
public interface Service<T, ID extends Serializable> {
T save(T entity);
}
错误:
@RestController
@RequestMapping("/news")
public class NewsController {
private final NewsService newsService;
@Autowired
public NewsController(NewsService newsService) {
this.newsService = newsService;
}
@RequestMapping(method = RequestMethod.GET)
public void save(){
newsService.save(new News("elo", "kot", "cos"));
}
}
我走对了路?
答案 0 :(得分:1)
试试这个
Spring数据无法找到您的mongo包存储库 告诉spring你的存储库位置
你应该使用
@EnableMongoRepositories(basePackages="com.newssystem.lab.dao")
在SpringBootCofiguration文件中
答案 1 :(得分:0)
可能是你需要定义一个bean,指定你正确的impl?
类似的东西:
com.tiemens.secretshare.main.cli
并导入此配置?
答案 2 :(得分:0)
最好的方法是:
@NoRepositoryBean
public interface Dao<T, ID extends Serializable> extends MongoRepository<T, ID>{
}
那就是你要做的一切。否则Spring会认为它是一个豆子。