也许已经晚了,但我在这里留下了一些对我有用的东西,也许有人可以帮忙。
我也在使用 Spring MVC 和 Hibernate
在模型(实体类)中创建一个 String 类型的变量,以使用 Base64 将类型 byte 转换为 String。
我为我拥有的带有各自国旗的国家表做了这个,我想要在一个表格中列出所有国家和国家的国旗。
模型(实体)
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
@Entity
@Table(name = "country")
public class Country implements java.io.Serializable {
private int id;
private String name;
private byte[] flag;
private String base64; //Variable to store the conversion of a data byte type to String
@Transient //Annotation so it does not persist in the database
public String getBase64() {
//Convert the data type byte to String, store it in the variable and return it
return this.base64 = Base64.encode(this.flag);
}
public void setBase64(String base64) {
this.base64 = base64;
}
public Country() {
}
public Country(int id, String name, byte[] flag, String base64) {
this.id = id;
this.name = name;
this.flag = this.flag
this.base64 = this.base64;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
@Column(name = "name")
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "flag")
public byte[] getFlag() {
return this.flag;
}
public void setFlag(byte[] flag) {
this.flag = flag;
}
}
Repository - Implements 是一个接口 - AbstractDao 是一个类 Abstract
import org.springframework.stereotype.Repository; 导入应用程序.model.Country;导入 application.repository.dao.AbstractDao;导入 application.repository.dao.CountryDao;导入 org.hibernate.Criteria;
@Repository("countryDao")
public class CountryDaoImpl extends AbstractDao<Integer, Country> implements CountryDao {
@Override
@SuppressWarnings("unchecked")
public List<Country> listCountries() {
Criteria criteria = createEntityCriteria(); //Country.class
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
List<Country> listCountries = criteria.list();
return listCountries;
}
}
服务 - 实现是一个接口
import application.model.Country;
import application.repository.dao.CountryDao;
import application.service.dao.CountryService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service("countryService")
public class CountryServiceImpl implements CountryService {
@Autowired
private CountryDao countryDao;
@Override
@Transactional(readOnly = true)
public List<Country> listCountries() {
return countryDao.listCountries();
}
}
控制器
import application.model.Country;
import application.service.dao.CountryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping(value = "/countries")
public class CountryController {
@Autowired
private CountryService countryService;
@RequestMapping(value = "/list", method = RequestMethod.GET)
public String ListCountries(Model model) {
model.addAttribute("listcont", countryService.listCountry());
return "countries/countries"; //view
}
}
查看 - countries/countries.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h3>List Countries</h3>
<table>
<thead>
<tr>
<th>Name</th>
<th>Flag</th>
</tr>
</thead>
<tbody>
<c:forEach items="${listcont}" var="country">
<tr>
<td>${country.name}</td>
<td><img src="data:image/png;base64,${country.base64}" /></
</tr>
</c:forEach>
</tbody>
</table>
</body>
</html>