create table customer(
id int(11) primary key auto_increment,
name varchar(20),
phone_number varchar(20)
);
create table orders(
id int(11) primary key auto_increment,
order_id varchar(20),
create_time datetime,
customer_id int(11)
);
数据库中已有数据以下两张图所示:
customer表
orders表
三. POJO类的创建
public class Customer {
private int id;
private String name;
private String phoneNum;
private Set orderSet;
//setter and getter
}
public class Order {
private int id;
private String orderId;
private Date createTime;
private Customer customer;
public Order() {}
public Order(int id, String orderId, Date createTime) {
this.id = id;
this.orderId = orderId;
this.createTime = createTime;
}
//setter and getter
}
public class SessionUtils {
//获取SessionFactory
public static SessionFactory getSessionFactory(){
StandardServiceRegistry registry = null;
SessionFactory sessionFactory = null;
try{
registry = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();
//不指定文件名默认是找hibernate.cfg.xml文件
//registry = new StandardServiceRegistryBuilder().configure().build();
sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
}catch(Exception ex){
ex.printStackTrace();
StandardServiceRegistryBuilder.destroy(registry);
}
return sessionFactory;
}
//打开并返回一个Session
public static Session openSession(){
return getSessionFactory().openSession();
}
//关闭Session
public static void closeSession(Session session){
if(null != session){
session.close();
}
}
}
5.2 编写测试用例
有关于HQL的查询都会在以下的单元测试类中完成:
public class HibernateTest {
private Session session;
@Before
public void openSession(){
session = SessionUtils.openSession(); //打开会话
}
@After
public void closeSession(){
SessionUtils.closeSession(session);
}
}
A.获取所有的Order对象,得到一个List集合
@Test
public void list(){
String hql = "from Order";
Query query = session.createQuery(hql, Order.class);
List list = query.getResultList();
for(Order o : list){
System.out.println(o.getOrderId() + "::" + o.getCreateTime());
}
}
B.获取Order的分页数据,得到一个List集合
@Test
public void pageList(){
String hql = "from Order";
Query query = session.createQuery(hql, Order.class);
/**
* setFirstResult() 是设置从第几条开始
* setMaxResults() 是设置每页多少条数据
*/
query.setFirstResult(0).setMaxResults(4);
List list = query.getResultList();
for(Order o : list){
System.out.println(o.getOrderId() + "::" + o.getCreateTime());
}
}
C.多条件查询,返回List集合(第一种形式:索引占位符)
@Test
public void multiCretiera(){
String hql = "from Order where orderId like ? and createTime between ? and ?";
Query query = session.createQuery(hql, Order.class);
String beginDateStr = "2016-07-26 00:00:00";
String endDateStr = "2016-07-26 23:59:59";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date beginDate = null;
Date endDate = null;
try {
beginDate = sdf.parse(beginDateStr);
endDate = sdf.parse(endDateStr);
} catch (ParseException e) {
e.printStackTrace();
}
query.setParameter(0, "%D%").setParameter(1, beginDate).setParameter(2, endDate);
List list = query.getResultList();
for(Order o : list){
System.out.println(o.getOrderId() + "::" + o.getCreateTime());
}
}
E.多条件查询,返回List集合(第二种形式:命名占位符)
@Test
public void multiCretiera1(){
String hql = "from Order where orderId like :orderId and createTime between :beginDate and :endDate";
Query query = session.createQuery(hql, Order.class);
String beginDateStr = "2016-07-26 00:00:00";
String endDateStr = "2016-07-26 23:59:59";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date beginDate = null;
Date endDate = null;
try {
beginDate = sdf.parse(beginDateStr);
endDate = sdf.parse(endDateStr);
} catch (ParseException e) {
e.printStackTrace();
}
query.setParameter("orderId", "%D%").setParameter("beginDate", beginDate).setParameter("endDate", endDate);
List list = query.getResultList();
for(Order o : list){
System.out.println(o.getId() + "::" + o.getOrderId());
} }
F.大于条件的查询,使用索引占位符
@Test
public void gt(){
String hql = "from Order where id > ?";
Query query = session.createQuery(hql, Order.class).setParameter(0, 3);
List list = query.getResultList();
for(Order o : list){
System.out.println(o.getId() + "::" + o.getOrderId());
}
}
G.更新操作
@Test
public void update(){
String hql = "update Order set createTime = ? where id = ?";
Transaction tx = session.beginTransaction();
Query> query = session.createQuery(hql).setParameter(0, new Date()).setParameter(1, 1);
int i = query.executeUpdate();
System.out.println(i);
tx.commit();
session.close();
}
H.删除操作
@Test
public void delete(){
//in 后面的括号可加可不加
String hql = "delete from Order where id in (:idList)";
Transaction tx = session.beginTransaction();
List list = new ArrayList();
list.add(1);
list.add(2);
Query> query = session.createQuery(hql).setParameter("idList", list);
int i = query.executeUpdate();
System.out.println(i);
tx.commit();
session.close();
}
I.获取某一列的值
@Test
public void singleValue(){
String hql = "select orderId from Order";
Query query = session.createQuery(hql, String.class);
List list = query.getResultList();
for(String o : list){
System.out.println(o);
}
}
J.使用对象的方式封装结果数据
@Test
public void getOrdersList(){
String hql = "select new com.demo.hibernate.one2many.Order(o.id, o.orderId, o.createTime) from Order as o";
Query query = session.createQuery(hql, Order.class);
List list = query.getResultList();
for(Order o : list){
System.out.println(o.getId() + ";;" + o.getOrderId() + ";;");
}
}
K.获取关联对象的结果集
@Test
public void getCustomer(){
String hql = "select o.customer from Order o where o.customer.id = 6";
Query query = session.createQuery(hql, Customer.class);
List list = query.getResultList();
for(Customer o : list){
System.out.println(o.getId() + ";;");
}
}
L.多列数据的查询
@Test
public void getObjectArray(){
//String hql = "select o.orderId, o.createTime from Order o";
//String hql = "select o.customer.name, o.customer.phoneNum, o.orderId, o.createTime from Order o";
//String hql = "select o.orderId, o.createTime from Order o join o.customer c";
String hql = "select c.name, c.phoneNum, o.orderId, o.createTime from Order o join o.customer c";
Query
@Test
public void descQuery(){
String hql = "from Order order by id desc";
Query query = session.createQuery(hql, Order.class);
List list = query.getResultList();
for(Order o : list){
System.out.println(o.getId() + "::" + o.getOrderId());
}
}
O.右连接
@Test
public void rightJoin(){
String hql = "select o.customer from Order o right join o.customer";
Query query = session.createQuery(hql, Customer.class);
List list = query.getResultList();
for(Customer c : list){
System.out.println(c.getId());
}
}
用简单的话来定义tcpdump,就是:dump the traffic on a network,根据使用者的定义对网络上的数据包进行截获的包分析工具。 tcpdump可以将网络中传送的数据包的“头”完全截获下来提供分析。它支 持针对网络层、协议、主机、网络或端口的过滤,并提供and、or、not等逻辑语句来帮助你去掉无用的信息。
实用命令实例
默认启动
tcpdump
普通情况下,直
MO= Mobile originate,上行,即用户上发给SP的信息。MT= Mobile Terminate,下行,即SP端下发给用户的信息;
上行:mo提交短信到短信中心下行:mt短信中心向特定的用户转发短信,你的短信是这样的,你所提交的短信,投递的地址是短信中心。短信中心收到你的短信后,存储转发,转发的时候就会根据你填写的接收方号码寻找路由,下发。在彩信领域是一样的道理。下行业务:由SP
import java.util.Arrays;
import java.util.Random;
public class MinKElement {
/**
* 5.最小的K个元素
* I would like to use MaxHeap.
* using QuickSort is also OK
*/
public static void
添加没有默认值:alter table Test add BazaarType char(1)
有默认值的添加列:alter table Test add BazaarType char(1) default(0)
删除没有默认值的列:alter table Test drop COLUMN BazaarType
删除有默认值的列:先删除约束(默认值)alter table Test DRO
Spring Boot 1.2.4已于6.4日发布,repo.spring.io and Maven Central可以下载(推荐使用maven或者gradle构建下载)。
这是一个维护版本,包含了一些修复small number of fixes,建议所有的用户升级。
Spring Boot 1.3的第一个里程碑版本将在几天后发布,包含许多