gpt4 book ai didi

java - spring mvc hibernate中如何从表单获取数据并将其存储在多个表中

转载 作者:行者123 更新时间:2023-12-01 14:33:57 25 4
gpt4 key购买 nike

我有这些类(class):

文章类别:

@Entity
@Table(name = "imei")
public class Article {
@Id
//@GeneratedValue
@Column(name = "imei1",nullable = false)
private Long imeiNo;
@Column(name = "date_added")
@Temporal(javax.persistence.TemporalType.TIMESTAMP)
private Date addedDate;
public Article() {
}
public Long getImeiNo() {
return imeiNo;
}
public void setImeiNo(Long imeiNo) {
this.imeiNo = imeiNo;
}
public Date getAddedDate() {
return addedDate;
}

public void setAddedDate(Date addedDate) {
this.addedDate = addedDate;
}

ArticleDao 类:

public interface ArticleDao {
// To Save the article detail
public void saveArticle(Article article );

// To get list of all articles
public List<Article> listArticles();
}

ArticleDaoImpl 类:

@Repository("articleDao")
public class ArticleDaoImpl implements ArticleDao {

@Autowired
private SessionFactory sessionFactory;

// To Save the article detail
public void saveArticle(Article article) {
article.setAddedDate(new Date());

sessionFactory.getCurrentSession().saveOrUpdate(article);
}

// To get list of all articles
@SuppressWarnings("unchecked")
public List<Article> listArticles() {
return (List<Article>) sessionFactory.getCurrentSession().createCriteria(Article.class).list();
}
}

文章 Controller :

@Controller
@RequestMapping("/articles")
public class ArticleController {

@Autowired
private ArticleService articleService;

@RequestMapping(value = "/save", method = RequestMethod.POST)
public ModelAndView saveArticle(@ModelAttribute(" article") Article article,
BindingResult result) {
articleService.addArticle( article);
return new ModelAndView("redirect:/articles.html");
}

@RequestMapping(method = RequestMethod.GET)
public ModelAndView listArticles() {
Map<String, Object> model = new HashMap<String, Object>();
model.put("articles", articleService.listArticles());

return new ModelAndView("articlesList", model);
}

@RequestMapping(value = "/add", method = RequestMethod.GET)
public ModelAndView addArticle(@ModelAttribute("article") Article article,
BindingResult result) {
return new ModelAndView("addArticle");
}

}

文章服务:

public interface ArticleService {

public void addArticle(Article article);

public List<Article> listArticles();
}

ArticleServiceImpl 类:

@Service("articleService")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class ArticleServiceImpl implements ArticleService {

@Autowired
private ArticleDao articleDao;

public ArticleServiceImpl() {
}

@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public void addArticle(Article article) {
articleDao.saveArticle(article);
}

public List<Article> listArticles() {
return articleDao.listArticles();
}

}

现在创建了另一个类,仅Imei.java

@Entity
@Table(name = "onlyimei")
public class OnlyImei {
@Id
@Column(name = "imei1",nullable = false)
private Long imeiNo;
public OnlyImei() {
}
public Long getImeiNo() {
return imeiNo;
}
public void setImeiNo(Long imeiNo) {
this.imeiNo = imeiNo;
}
}

你能告诉我如何同时在两个表中输入值吗?我必须在一个表中输入 IMEI 号码,在第二个表中输入 IMEI 以及日期和时间。我已经制作了第二个表,但我不知道如何在两个表中同时输入值。

最佳答案

使用您的服务方法。写入事务内的两个表。我没有看到第二个 DAO 类,因此我为此示例编写了一个。

@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public void addArticleToBook(Article article, Book book) {
articleDao.saveArticle(article);
bookDao.addArticle(article, book.getId());
}

工作将在同一事务中完成。

实际上,上面的可能不是最好的。鉴于 Hibernate 支持关系的方式,如果您正确配置了 Hibernate 并使用了 Spring Hibernate 模板,您应该能够执行此操作:

@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public void addArticleToBook(Article article, Long bookId) {
this.sessionFactory.getCurrentSession()
.loadBookById(bookId)
.getArticles()
.add(article);
}

关于java - spring mvc hibernate中如何从表单获取数据并将其存储在多个表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16637238/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com