hibernate入门-基本配置及简单的crud操作
框架来说主要是需要写大量的配置文件,hibernate相比mybatis来说更强大,移植性更好;
1.类和数据库的映射配置:配置文件命名一般——类名.hbm.xml (user.hbm.xml),与实体类放在同一目录下,配置成员变量和数据库字段的映射:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="ni.jun.yang.bean.User" table="t_user"> <id name="userId" column="userId"> <!-- 主键自动增长 --> <generator class="native"></generator> </id> <property name="userName" column="userName"></property> <property name="userPsw" column="userPsw"></property> </class> </hibernate-mapping>
2.主配置文件,放在src目录下,命名一般用hibernate.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate1</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">123</property> <!-- 设置数据库方言 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 显示sql语句 --> <property name="hibernate.show_sql">true</property> <!-- 事务隔离级别: 1.读未提交 1 2.读已提交 2 3.可重复读 4 4.串行化(不可以并发)8 --> <property name="connection.isolation">2</property> <property name="hibernate.format_sql">true</property> <!-- 表自动生成 : update没有就建表,有表没影响保留原来的数据, create每次都会建表,会删除以前的数据, create-drop,服务器关闭删除表--> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 二级缓存配置 --> <property name="hibernate.cache.use_second_level_cache">true</property> <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property> <!-- 连接池 C3P0配置 --> <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property> <property name="hibernate.c3p0.max_size">20</property> <property name="hibernate.c3p0.min_size">5</property> <property name="hibernate.c3p0.timeout">120</property> <property name="automaticTestTable">Test</property> <property name="hibernate.c3p0.max_statements">100</property> <property name="hibernate.c3p0.idle_test_period">120</property> <property name="hibernate.c3p0.acquire_increment">1</property> <property name="c3p0.testConnectionOnCheckout">true</property> <property name="c3p0.idleConnectionTestPeriod">18000</property> <property name="c3p0.maxIdleTime">25000</property> <property name="c3p0.idle_test_period">120</property> <!-- 映射类配置文件 --> <mapping resource="ni/jun/yang/bean/user.hbm.xml"></mapping> <mapping resource="ni/jun/yang/bean/Orders.hbm.xml"></mapping> <mapping resource="ni/jun/yang/bean/Address.hbm.xml"></mapping> <mapping resource="ni/jun/yang/bean/Customer.hbm.xml"></mapping> <class-cache usage="read-write" class="ni.jun.yang.bean.User"/> </session-factory> </hibernate-configuration>
3.工具类读取配置文件,获取SessionFactory对象:
package ni.jun.yang.util; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtil { private static SessionFactory sessionFactory; public HibernateUtil() { } public static SessionFactory getSessionFactory(){ if(sessionFactory == null){ Configuration cfg =new Configuration(); cfg.configure();//读取配置文件 sessionFactory = cfg.buildSessionFactory(); } return sessionFactory; } }
4.session类中提供了一系列的crud操作:
4.1 添加:
public void Add(User user) { Session session = sessionFactory.openSession(); Transaction ts = session.beginTransaction(); //开启事务 session.save(user); ts.commit(); //提交事务 session.close(); }
4.2 删除:
public void delete(User user) { Session session = sessionFactory.openSession(); Transaction ts = session.beginTransaction(); //开启事务 session.delete(user); ts.commit(); //提交事务 session.close(); }
4.3 修改:
public void update(User user) { Session session = sessionFactory.openSession(); Transaction ts = session.beginTransaction();//开启事务 session.update(user); ts.commit(); //提交事务 session.close(); }
4.4 查询:
public User select(int id) { Session session = sessionFactory.openSession(); Transaction ts = session.beginTransaction(); User user = session.get(User.class, id); user.setUserPsw("321"); ts.commit(); session.close(); return user; }
4.5 查询所有,结合hql语句
public List<User> selectAll() { Session session = sessionFactory.openSession(); Transaction ts = session.beginTransaction(); Query<User> q=session.createQuery("from User"); // Query<User> q=session.createQuery("from User where userName =:userName");//占位符 // q.setString("userName", "zhangsan"); //占位符赋值 List<User> lists = q.list(); return lists; }