@Test
public void query()
{
EntityManagerFactory entityFactory=Persistence.createEntityManagerFactory("person");
EntityManager entity=entityFactory.createEntityManager();
//select o from Person o where o.name=?1 --> 位置参数查询query.setParameter(1,"liyong");
//select o from Person o where o.name=:name and o.id=:id--->命名参数query.setParameter("name", "liyong");
//select o from Person o where o.name=? and o.id=?-->位置参数查询 query.setParameter(1, "liyong");query.setParameter(2, new Integer(3));
Query query=entity.createQuery("select o from Person o where o.name=? and o.id=?");
query.setParameter(1, "liyong");
query.setParameter(2, new Integer(2));
//只返回一条数据
Person person=(Person)query.getSingleResult();
//返回多条数据
/*List<Person> list=query.getResultList();
for(Person person2 : list)
{
System.out.println(person2.getName());
}*/
System.out.println(person.getName());
entity.close();
entityFactory.close();
}
@SuppressWarnings("unchecked")
@Test
public void queryall()
{
EntityManagerFactory entityFactory=Persistence.createEntityManagerFactory("person");
EntityManager entity=entityFactory.createEntityManager();
Query query=entity.createQuery("select o from Person o ");
//返回多条数据
List<Person> list=query.getResultList();
for(Person person : list)
{
System.out.println(person.getName());
}
entity.close();
entityFactory.close();
}
@Test
public void querydelete()
{
EntityManagerFactory entityFactory=Persistence.createEntityManagerFactory("person");
EntityManager entity=entityFactory.createEntityManager();
entity.getTransaction().begin();
Query query=entity.createQuery("delete from Person o where o.id=?1");
query.setParameter(1, new Integer(2));
query.executeUpdate();
entity.getTransaction().commit();
entity.close();
entityFactory.close();
}
@Test
public void queryupdate()
{
EntityManagerFactory entityFactory=Persistence.createEntityManagerFactory("person");
EntityManager entity=entityFactory.createEntityManager();
entity.getTransaction().begin();
Query query=entity.createQuery("update Person o set o.name=:name where o.id=:id");
query.setParameter("name","李勇");
query.setParameter("id", new Integer(3));
query.executeUpdate();
entity.getTransaction().commit();
entity.close();
entityFactory.close();
}
使用JPQL语句进行增、删、该、查
原创青年IT男1 ©著作权
文章标签 使用JPQL语句进行增、删、该、查 数据 文章分类 前端开发
上一篇:大数据字段映射与字段延迟加载
下一篇:使用JPA加载_更新_删除对象
-
实践使用Spring JDBC API JdbcTemplate来对MySQL数据库进行增删改查操作
实践使用Spring JDBC API JdbcTemplate来对MySQL数据库进行增删改查操作
sql JdbcTemplate Spring JDBC API MySQL -
6.5版 Mysql编译安装及增、删、该、查
6.5版 Mysql编译安装及增、删、该、查
linux mysql windows 配置文件 local -
LinkedList集合对元素进行增、查、删操作
ArrayList集合在查询元素时速度很快,但在增删元素时效率较低,为了克服这种局限
网络爬虫 python 前端 删除元素 System -
SQL增、删、改、查
SQL增、删、改、查
SQL增、删、改、查 -
jpql语句
jpql语句
jpql语句