6、原生SQL
JPA除了对JPQL提供支持外,还对原生SQL语句也提供了支持。下面小节一起来看看吧。
6、1 查询单个
示例代码:
@Test
public void getSingle() {
EntityManager entityManager = JPAEntityFactory.getEntityManager();// 等同于 获取连接
String sql = "select cid, cname, credit, end, num, start " +
"from yootk.course where cid = ?1";
Query query = entityManager.createNativeQuery(sql); // 预处理sql
query.setParameter(1, 1);
Course course = (Course) query.getSingleResult();
loggerFactory.info("【执行结果】:{}",course ); // 执行sql
JPAEntityFactory.close();// 关闭连接
}
执行结果:
java.lang.ClassCastException: class [Ljava.lang.Object; cannot be cast to class look.word.po.Course
意思是 query.getSingleResult() 不能转换成 Course类
修改代码
执行结果:
【执行结果】:[1, Spring编程实战, 5, 2022-12-30, 88, 2022-09-19]
可以发现,query.getSingleResult() 返回的是一个数组对象。不能转换成Course也是正常的。后续文章会解决这个问题
6、2 查询多个
实例代码:
@Test
public void getAllCourse() {
EntityManager entityManager = JPAEntityFactory.getEntityManager();// 等同于 获取连接
String sql = "select cid, cname, credit, end, num, start " +
"from yootk.course ";
Query query = entityManager.createNativeQuery(sql); // 预处理sql
List list = query.getResultList();// 执行sql
for (Object o : list) {
loggerFactory.info("【执行结果】:{}", o);
}
JPAEntityFactory.close();// 关闭连接
}
执行结果:
【执行结果】:[1, Spring编程实战, 5, 2022-12-30, 88, 2022-09-19]
【执行结果】:[5, SpringCloud编程实战, 5, 2022-12-30, 88, 2022-09-19]
【执行结果】:[6, SSM编程实战, 5, 2022-12-30, 88, 2022-09-19]
6、3 分页模糊查询
示例代码:
@Test
public void getAllCourseSplit() {
int current = 2;
int lineSize = 3;
String keyWord = "%Spring%";
EntityManager entityManager = JPAEntityFactory.getEntityManager();// 等同于 获取连接
String sql = "select cid, cname, credit, end, num, start " +
"from yootk.course where cname like :k1";
Query query = entityManager.createNativeQuery(sql); // 预处理sql
query.setFirstResult((current - 1) * lineSize); // 等同于 limit ?
query.setMaxResults(lineSize); // // 等同于 limit ?,?
query.setParameter("k1", keyWord);
List list = query.getResultList();// 执行sql
for (Object o : list) {
loggerFactory.info("【执行结果】:{}", o);
}
JPAEntityFactory.close();// 关闭连接
执行结果:
执行的sql: select cid,cname,credit,end,num,start from yootk.course where cname like ? limit ?,?
6、4 count
统计 模糊匹配到的行数
// 统计行数
@Test
public void getAllCourseCount() {
String keyWord = "%Spring%";
EntityManager entityManager = JPAEntityFactory.getEntityManager();// 等同于 获取连接
String sql = "select count(*) from yootk.course where cname like :k1";
Query query = entityManager.createNativeQuery(sql); // 预处理sql
query.setParameter("k1", keyWord);
loggerFactory.info("【执行结果】:{}", query.getSingleResult());// 执行sql
JPAEntityFactory.close();// 关闭连接
}
执行结果
执行的sql: select count(*) from yootk.course where cname like ?
6、5 更新
例如:我们想把学分小于5的课程全部提高到5
- 注意到:我们当前是未开启事务的,观察程序执行结果。
@Test
public void getUpdateCourse() {
// 例如:我们想把学分小于5的课程全部提高到5
EntityManager entityManager = JPAEntityFactory.getEntityManager();// 等同于 获取连接
//entityManager.getTransaction().begin(); // 开启事务
String sql = "update yootk.course as c set c.credit = ?1 where c.credit <?2";
Query query = entityManager.createNativeQuery(sql); // 预处理sql
query.setParameter(1, 5);
query.setParameter(2, 5);
loggerFactory.info("【执行结果】:{}", query.executeUpdate());// 执行sql
//entityManager.getTransaction().commit(); // 提交事务
JPAEntityFactory.close();// 关闭连接
}
执行结果:
异常提示信息为:说我们的 update / delete 操作需要事务的支持
- 把上叙代码注释掉的事务,放开执行即可。
6、6 删除指定id
示例代码:
// 删除指定id
@Test
public void deleteById() {
EntityManager entityManager = JPAEntityFactory.getEntityManager(); // 等同于 获取连接
entityManager.getTransaction().begin(); // 开启事务
String deleteSql = "delete from yootk.course where cid = ?1";
Query query = entityManager.createNativeQuery(deleteSql);
query.setParameter(1, 1); // 预处理sql 赋值
loggerFactory.info("【执行结果】:{}", query.executeUpdate());// 执行sql
entityManager.getTransaction().commit(); // 提交事务
JPAEntityFactory.close();// 关闭连接
}
执行结果
执行的sql: delete from yootk.course where cid = ?
6、7 新增
实例代码:
@Test
public void addCourse() {
EntityManager entityManager = JPAEntityFactory.getEntityManager();// 等同于获取连接
entityManager.getTransaction().begin(); //开启事务
String addSql = "insert into yootk.course(cname, credit, end, num, start) " +
"value(:cname,:credit,:end,:num,:start )";
Query query = entityManager.createNativeQuery(addSql);
query.setParameter("cname", "java就业课程实战"); // 对预处理参数赋值
query.setParameter("credit", 10); // 对预处理参数赋值
query.setParameter("start", DateUtil.stringToDate("2021-10-01")); // 对预处理参数赋值
query.setParameter("end", DateUtil.stringToDate("2022-10-01")); // 对预处理参数赋值
query.setParameter("num", 999); // 对预处理参数赋值
query.executeUpdate(); // 执行最终sql
entityManager.getTransaction().commit();// 提交事务
}
执行结果:
标签:entityManager,JPA,SQL,改查,JPAEntityFactory,course,sql,query,执行 From: https://www.cnblogs.com/look-word/p/16739856.html