标签:
CREATE TABLE "STUDENT"
(
"STUNO" VARCHAR2(20),
"STUNAME" VARCHAR2(20),
"STUPASS" VARCHAR2(20),
"STUSEX" VARCHAR2(2),
"MOBILE" VARCHAR2(20),
"EMAIL" VARCHAR2(20),
"ADDRESS" VARCHAR2(50),
"STUAGE" NUMBER
)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 数据库URL --> <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property> <!-- 数据库用户名 --> <property name="connection.username">zhengcheng</property> <!-- 数据库密码 --> <property name="connection.password">123123</property> <!-- JDBC驱动 --> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <!-- 每个数据库都有对应的Dialect以匹配其平台特性 --> <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> <!-- 指定当前session范围和上下文 --> <property name="current_session_context_class">thread</property> <!-- 指定运行期生成的SQL输出到日志以供调试 --> <property name="show_sql">true</property> <!-- 是否格式化sql --> <property name="format_sql">true</property> <!-- 映射文件 --> <mapping resource="Student.hbm.xml" /> </session-factory> </hibernate-configuration>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="hibDemo1.entity.Student" table="STUDENT"> <id name="stuNo" type="java.lang.String" column="STUNO"> <!--generator的class类型 assigned:主键的状态 assigned表示程序生成 sequence:Oracle中的序列 identity:Sql中的自动编号 increment:先查询最大的编号再增1 uuid:生成32位长的字符串 native:根据数据库自动生成 --> <generator class="assigned" /> </id> <!-- 直接使用property属性设置 --> <property name="stuName" type="java.lang.String" column="STUNAME" length="50" not-null="true" /> <!-- 使用column设置 --> <property name="stuPass" type="java.lang.String" column="STUPASS"> <column name="STUPASS" length="50" not-null="true"></column> </property> <property name="stuSex" type="java.lang.String" column="STUSEX" /> <property name="stuAge" type="java.lang.Integer" column="STUAGE" /> <property name="Mobile" type="java.lang.String" column="MOBILE" /> <property name="Email" type="java.lang.String" column="EMAIL" /> <property name="Address" type="java.lang.String" column="ADDRESS" /> </class> </hibernate-mapping>
package hibDemo1.dao; import hibDemo1.entity.Student; import java.util.List; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; public class StudentService { private static Configuration conf; private static SessionFactory sf; private static Transaction tx; static { try { conf = new Configuration().configure(); sf = conf.buildSessionFactory(); } catch (HibernateException e) { e.printStackTrace(); } } public static Session getSession() { return sf.openSession(); //过去老的方法,不需要使用事务 //return sf.getCurrentSession(); // 新的方法,需要和事务一起使用,可以保证每个用户创建的session独立,需要在配置文件中配置 //<property name="current_session_context_class">thread</property> } /** * 获取所有学生列表 * * @return */ public List<Student> GetAllStudent() { List<Student> list = null; Session session = getSession(); if (session != null) { try { String hql = "from Student"; Query query = session.createQuery(hql); list = query.list(); } catch (HibernateException e) { e.printStackTrace(); } finally { session.close(); } } return list; } /** * 获取单个学生信息 * * @param stuNo * @return */ public Student GetStudentBystuNo(String stuNo) { Student stu = null; Session session = getSession(); if (session != null) { try { // get如果没有查询到数据,则返回null // stu = (Student) session.get(Student.class, stuNo); // load如果没有查询到数据,则抛出异常 stu = (Student) session.load(Student.class, stuNo); } catch (HibernateException e) { e.printStackTrace(); } finally { session.close(); } } return stu; } /** * 添加一个学生 * * @param stu * @author Administrator */ public boolean AddStudent(Student stu) { boolean b = false; Session session = getSession(); if (session != null) { try { // 开启一个事务 tx = session.beginTransaction(); // 保存 session.save(stu); // 提交事务 tx.commit(); return true; } catch (HibernateException e) { e.printStackTrace(); tx.rollback(); } finally { session.close(); } } return b; } /** * 更新一个学生 * * @param stu * @author Administrator */ public boolean UpdateStudent(String stuNo, String newName) { boolean b = false; Session session = getSession(); if (session != null) { try { // 开启一个事务 tx = session.beginTransaction(); // 获取一个学生对象 Student stu = (Student) session.load(Student.class, stuNo); // 更新某个属性 stu.setStuName(newName); // 提交事务 tx.commit(); return true; } catch (HibernateException e) { e.printStackTrace(); tx.rollback(); } finally { session.close(); } } return b; } /** * 更新一个学生 * * @param stu * @author Administrator */ public boolean DeleteStudent(String stuNo) { boolean b = false; Session session = getSession(); if (session != null) { try { // 开启一个事务 tx = session.beginTransaction(); // 获取一个学生对象 Student stu = (Student) session.load(Student.class, stuNo); // 删除操作 session.delete(stu); // 提交事务 tx.commit(); return true; } catch (HibernateException e) { e.printStackTrace(); tx.rollback(); } finally { session.close(); } } return b; } }
test.java
package hibDemo.test; import hibDemo1.dao.StudentService; import hibDemo1.entity.Student; import java.util.List; import java.util.Scanner; public class test { public static void main(String[] args) { // 添加学生信息 // AddStudent(); // 显示所有学生信息 ShowAll(); // 显示单个学生信息 // ShowOne(); //更新学生信息 //Update(); //删除学生信息 //Delete(); ShowAll(); } public static void ShowAll() { StudentService service = new StudentService(); List<Student> list = service.GetAllStudent(); for (Student student : list) { System.out.println(student.getStuNo() + " " + student.getStuName()); } } public static void ShowOne() { String no = "A004"; StudentService service = new StudentService(); Student student = service.GetStudentBystuNo(no); if (student != null) { System.out.println(student.getStuNo() + " " + student.getStuName()); } else { System.out.println("no data"); } } public static void AddStudent() { Scanner input = new Scanner(System.in); Student stu = new Student(); System.out.print("请输入学生编号:(A001)"); stu.setStuNo(input.next()); System.out.print("请输入学生姓名:(A001)"); stu.setStuName(input.next()); stu.setStuPass("888888"); System.out.print("请输入学生年龄:(0-100)"); stu.setStuAge(input.nextInt()); System.out.print("请输入学生手机号:(A001)"); stu.setMobile(input.next()); System.out.print("请输入学生邮箱:(A001)"); stu.setEmail(input.next()); System.out.print("请输入学生地址:(A001)"); stu.setAddress(input.next()); StudentService service = new StudentService(); service.AddStudent(stu); } public static void Update() { Scanner input = new Scanner(System.in); System.out.print("输入要修改的学号:"); String stuNo = input.next(); System.out.print("输入要修改的姓名:"); String newName = input.next(); StudentService service = new StudentService(); service.UpdateStudent(stuNo, newName); } public static void Delete() { Scanner input = new Scanner(System.in); System.out.print("输入要修改的学号:"); String stuNo = input.next(); StudentService service = new StudentService(); service.DeleteStudent(stuNo); } }
标签:
原文地址:http://www.cnblogs.com/zhengcheng/p/5014246.html