public class HibernateUtils {
public static final ThreadLocal threadLocal= new ThreadLocal();
public static SessionFactory sessionFactory;
public void init(){
System.out.println("...... 初始化 ......" );
}
/**
* 获取当前的Session
* @return
*/
public static Session currentSession(){
Session session = (Session) threadLocal.get();
if(ObjectUtils.isEmpty(session)){
session = sessionFactory.getCurrentSession();
threadLocal.set(session);
}
return session;
}
/**
* 释放Session
*/
public static void closeSession(){
Session session = (Session) threadLocal.get();
if(ObjectUtils.isEmpty(session)){
session.close();
}
threadLocal.set(null);
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void setSessionFactory(SessionFactory sessionFactory) {
HibernateUtils.sessionFactory = sessionFactory;
}
public static ThreadLocal getThreadlocal() {
return threadLocal;
}
}
创建Person实体类
@Entity
@Table(name = "db_person")
public class Person {
@Id
@GeneratedValue
@Column(name = "PK_ID")
private int pkId;
@Column(name = "NAME")
private String name;
@Column(name = "AGE")
private int age;
@Column(name = "ADDRESS")
private String address;
public int getPkId() {
return pkId;
}
public void setPkId(int pkId) {
this.pkId = pkId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Person [pkId=" + pkId + ", name=" + name + ", age=" + age + ", address=" + address + "]";
}
}
创建PersonDao Dao层
@Repository
public class PersonDao extends BaseDao {
@Override
public void insert(Person entity) {
HibernateUtils.currentSession().save(entity);
}
@Override
public Person get(int pkId) {
return (Person) HibernateUtils.currentSession().get(Person.class, pkId);
}
@Override
public List getLists() {
String hql = "from Person";
Query query = HibernateUtils.currentSession().createQuery(hql);
return query.list();
}
@Override
public void Delete(int pkId) {
Person person = get(pkId);
HibernateUtils.currentSession().delete(person);
}
@Override
public void update(Person entity) {
HibernateUtils.currentSession().update(entity);
}
}
创建PersonService Service层
@Service
public class PersonService {
@Autowired
PersonDao personDao;
@Transactional
public void insert(Person person) {
personDao.insert(person);
}
@Transactional
public void delete(int pkId) throws Exception {
personDao.Delete(pkId);
}
@Transactional
public void update(Person person) {
personDao.update(person);
}
@Transactional
public void get(int pkId) {
System.out.println("======打印数据======" + personDao.get(pkId));
}
@Transactional
public void getLists() {
List list = personDao.getLists();
for (Person person : list) {
System.out.println("====== 打印数据 =====" + person);
}
}
}
单元测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring/application.xml"})
public class OneTest {
@Autowired
PersonService personService;
@Test
public void insert() {
Person person = new Person();
person.setName("王五");
person.setAge(23);
person.setAddress("中华人民共和国");
person.setPkId(26);
personService.insert(person);
}
@Test
public void delete() throws Exception {
personService.delete(25);
}
@Test
public void update() {
Person person = new Person();
person.setPkId(15);
personService.update(person);
}
@Test
public void get() {
personService.get(22);
}
@Test
public void getLists() {
personService.getLists();
}
}
本篇文章重点说明什么是函数柯里化,这个语法现象的背后动机是什么,有什么样的应用场景,以及与部分应用函数(Partial Applied Function)之间的联系 1. 什么是柯里化函数
A way to write functions with multiple parameter lists. For instance
def f(x: Int)(y: Int) is a
ApplicationContext能读取多个Bean定义文件,方法是:
ApplicationContext appContext = new ClassPathXmlApplicationContext(
new String[]{“bean-config1.xml”,“bean-config2.xml”,“bean-config3.xml”,“bean-config4.xml
#!/bin/bash
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information re
参考了
http://zhedahht.blog.163.com/blog/static/25411174201142733927831/
但是用java来实现有一个问题。
由于Java无法像C那样“传递参数的地址,函数返回时能得到参数的值”,唯有新建一个辅助类:AuxClass
import ljn.help.*;
public class BalancedBTree {
BeanUtils.copyProperties VS PropertyUtils.copyProperties
作为两个bean属性copy的工具类,他们被广泛使用,同时也很容易误用,给人造成困然;比如:昨天发现同事在使用BeanUtils.copyProperties copy有integer类型属性的bean时,没有考虑到会将null转换为0,而后面的业