package com.siti.test;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import com.siti.domain.User;
public class HiberTest {
static SessionFactory sessionFactory = null;
static {
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
.configure() // configures settings from hibernate.cfg.xml
.build();
sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory();
/*sessionFactory = new Configuration()
.configure()
.buildSessionFactory();*/
}
public static void main(String[] args) {
testInsertUser();
}
/**
* 7. Criteria 方式投影分组查询
* Hibernate: select this_.userName as y0_, count(this_.password) as y1_ from user this_ group by this_.userName
*
* [[Ljava.lang.Object;@1f44ec7, [Ljava.lang.Object;@602b6b, [Ljava.lang.Object;@1c297a3, [Ljava.lang.Object;@717323, [Ljava.lang.Object;@3e1bc8]
*
* 从这几个例子我们不难发现,如果没有指定select语句(没有投影),那么将返回表中的所有字段,返回结果会被封装到Entity实体对象User中,一但提供select语句(投影)后,
* 返回的结果类型,将不再封装到User对象,而是根据投影的实际类型返回,这就是投影对结果封装策略的影响。
*/
public static void testCriteriaSelectProjectUser(){
Session session = sessionFactory.openSession();
List<Object> userList = session.createCriteria(User.class)//
.setProjection(
Projections.projectionList()
.add(Projections.groupProperty("userName"))
.add(Projections.count("password"))
)
.list();
System.out.println(userList);
session.close();
}
/**
* 6. SQL 方式测试查询附加条件id>?
* Hibernate: select * from user where id > 5 // 注意这里用的是id>5而不是userId
*
* [User [userId=11, userName=zhangsan, password=zs]]
*
*/
public static void testSQLSelectUser(){
Session session = sessionFactory.openSession();
List<User> userList = session.createSQLQuery("select * from user where id > 5")//
.addEntity(User.class)//
.list();
System.out.println(userList);
session.close();
}
/**
* 6. HQL 方式测试查询附加条件id>?
* Hibernate: select user0_.id as id0_, user0_.userName as userName0_, user0_.password as password0_ from user user0_ where user0_.id>5
*
* [User [userId=11, userName=zhangsan, password=zs]]
*
*/
public static void testHQLSelectUser(){
Session session = sessionFactory.openSession();
List<User> userList = session.createQuery("from User user where user.userId > 5")
.list();
System.out.println(userList);
session.close();
}
/**
* 6. Criteria 方式测试查询附加条件id>?
* Hibernate: select this_.id as id0_0_, this_.userName as userName0_0_, this_.password as password0_0_ from user this_ where this_.id>?
*
* [User [userId=11, userName=zhangsan, password=zs]]
*
*/
public static void testCriteriaSelectUser(){
Session session = sessionFactory.openSession();
List<User> userList = session.createCriteria(User.class)//
.add(Restrictions.gt("userId", 5L))//
.list();
System.out.println(userList);
session.close();
}
/**
* 5. SQL 方式测试查询like附加条件
* Hibernate: select * from user where userName like 'zhan%'
*
* [User [userId=11, userName=zhangsan, password=zs]]
*
*/
public static void testSQLSelectPartUser(){
Session session = sessionFactory.openSession();
List<User> userList = session.createSQLQuery("select * from user where userName like 'zhan%'")
.addEntity(User.class)
.list();
System.out.println(userList);
session.close();
}
/**
* 5. HQL 方式测试查询like附加条件
* Hibernate: select user0_.id as id0_, user0_.userName as userName0_, user0_.password as password0_ from user user0_ where user0_.userName like 'zhan%'
*
* [User [userId=11, userName=zhangsan, password=zs]]
*
*/
public static void testHQLSelectPartUser(){
Session session = sessionFactory.openSession();
List<User> userList = session.createQuery("from User user where user.userName like 'zhan%'")
.list();
System.out.println(userList);
session.close();
}
/**
* 5. Criteria 方式测试查询like附加条件
* Hibernate: select this_.id as id0_0_, this_.userName as userName0_0_, this_.password as password0_0_ from user this_ where this_.userName like ?
*
* [User [userId=11, userName=zhangsan, password=zs]]
*
*/
public static void testCriteriaSelectPartUser(){
Session session = sessionFactory.openSession();
List<User> userList = session.createCriteria(User.class)
.add(Restrictions.like("userName", "zhan%"))
.list();
System.out.println(userList);
session.close();
}
/**
* 4. SQL 方式测试查询所有的user数据
* Hibernate: select * from user
*
* [User [userId=1, userName=wy, password=1234],
* User [userId=2, userName=yy, password=123],
* User [userId=3, userName=sd, password=12],
* User [userId=4, userName=test, password=sffs],
* User [userId=11, userName=zhangsan, password=zs]]
*/
public static void testSQLSelectAllUser(){
Session session = sessionFactory.openSession();
List<User> userList = session.createSQLQuery("select * from user").addEntity(User.class).list();
System.out.println(userList);
session.close();
}
/**
* 4. HQL 方式测试查询所有的user数据
* Hibernate: select user0_.id as id0_, user0_.userName as userName0_, user0_.password as password0_ from user user0_
*
* [User [userId=1, userName=wy, password=1234],
* User [userId=2, userName=yy, password=123],
* User [userId=3, userName=sd, password=12],
* User [userId=4, userName=test, password=sffs],
* User [userId=11, userName=zhangsan, password=zs]]
*/
public static void testHQLSelectAllUser(){
Session session = sessionFactory.openSession();
List<User> userList = session.createQuery("from User").list();
System.out.println(userList);
session.close();
}
/**
* 4. Criteria 方式测试查询所有的user数据
* Hibernate: select this_.id as id0_0_, this_.userName as userName0_0_, this_.password as password0_0_ from user this_
*
* [User [userId=1, userName=wy, password=1234],
* User [userId=2, userName=yy, password=123],
* User [userId=3, userName=sd, password=12],
* User [userId=4, userName=test, password=sffs],
* User [userId=11, userName=zhangsan, password=zs]]
*/
public static void testCriteriaSelectAllUser(){
Session session = sessionFactory.openSession();
List<User> userList = session.createCriteria(User.class).list();
System.out.println(userList);
session.close();
}
/**
* 3. 删除用户
* Hibernate: delete from user where id=?
*/
public static void testDeleteUser(){
User user = new User();
user.setUserId(5L);
user.setUserName("zhanan");
user.setPassword("zb");
Session session = sessionFactory.openSession();
session.beginTransaction(); // 开启事务
session.delete(user);
session.getTransaction().commit();// 提交事务
session.close();
}
/**
* 2. 更新用户
* Hibernate: update user set userName=?, password=? where id=?
*/
public static void testUpdateUser(){
User user = new User();
user.setUserId(5L);
user.setUserName("zhanan");
user.setPassword("zb");
Session session = sessionFactory.openSession();
session.beginTransaction(); // 开启事务
session.update(user);
session.getTransaction().commit();// 提交
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
HiberTest.zip (31个子文件)
HiberTest
.project 2KB
.mymetadata 300B
src
hibernate.cfg.xml 1KB
com
siti
domain
User.java 1KB
test
HiberTest.java 8KB
hbm
UserCount.hbm.xml 654B
WebRoot
WEB-INF
classes
hibernate.cfg.xml 1KB
com
siti
domain
User.class 2KB
test
HiberTest.class 6KB
hbm
UserCount.hbm.xml 654B
lib
dom4j-1.6.1.jar 307KB
geronimo-jta_1.1_spec-1.1.1.jar 16KB
mysql-connector-java-5.1.38-bin.jar 961KB
javassist-3.20.0-GA.jar 733KB
jboss-logging-3.3.0.Final.jar 65KB
hibernate-jpa-2.1-api-1.0.0.Final.jar 111KB
antlr-2.7.7.jar 435KB
jandex-2.0.0.Final.jar 183KB
classmate-1.3.0.jar 63KB
hibernate-commons-annotations-5.0.1.Final.jar 74KB
hibernate-core-5.1.0.Final.jar 5.42MB
web.xml 404B
index.jsp 834B
META-INF
MANIFEST.MF 36B
.myeclipse
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.common.project.facet.core.xml 252B
org.eclipse.jdt.core.prefs 364B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.component 465B
.jsdtscope 500B
.classpath 1KB
共 31 条
- 1
资源评论
低调的洋仔
- 粉丝: 1154
- 资源: 50
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功