参考文章
https://blog.csdn.net/coding_1994/article/details/84575943
https://blog.csdn.net/m0_37776094/article/details/78419669
import org.springframework.stereotype.Service; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.PersistenceUnit; import javax.persistence.Query; import java.util.List; @Service public class GetCodeService { private EntityManagerFactory emf; @PersistenceUnit//使用这个标记来注入EntityManagerFactory public void setEntityManagerFactory(EntityManagerFactory emf) { this.emf = emf; } /** * @param * @return */ public String getCode(Integer type) { EntityManager em = emf.createEntityManager(); em.getTransaction().begin(); Query query = null; //定义SQL,可以多个表关联查询 String sql = "SELECT getCode(?1)"; //创建原生SQL查询QUERY实例 query = em.createNativeQuery(sql); query.setParameter(1,type); //执行查询,sql语句执行的查询结果只有一列时就会出现该问题! // 当有多个列时用数组取没有问题,如果只有一列会默认为string或者其他类型! List<String> result = query.getResultList(); em.close(); return result.get(0).toString(); } }
来源:https://www.cnblogs.com/lick468/p/11207775.html