hibernate.cfg.xml文件
<!-- 約束在5.0.7.Final.jar內(nèi)configuration-3.0.dtd -->
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- hibernate 配置 -->
<hibernate-configuration>
<!-- 必須配置:連接數(shù)據(jù)庫四大參數(shù)與方言 -->
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///hibernate</property>
<property name="hibernate.connection.username">wshc</property>
<property name="hibernate.connection.password">123456</property>
<!-- 方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 可以選配 -->
<!-- 顯示sql -->
<property name="hibernate.show_sql">true</property>
<!-- 改變sql格式 -->
<property name="hibernate.format_sql">true</property>
<!-- 對(duì)數(shù)據(jù)庫的操作模式 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 映射 -->
<mapping resource="com/ithc/bean/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
student.hbm.xml表的映射配置
<!-- 映射 -->
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!-- name:全類名 table:表名 -->
<class name="com.ithc.bean.Student" table="student">
<!-- 配置主鍵 class標(biāo)簽李只能有一個(gè)主鍵 column:配置數(shù)據(jù)庫的字段名 name:實(shí)體類的屬性名 -->
<id column="id" name="id">
<generator class="native"/>
</id>
<property name="name" column="name" unique="true"/>
<property name="email" column="email"/>
</class>
</hibernate-mapping>
action持續(xù)層的調(diào)用
@WebServlet(value="/studentAction")
public class StudentAction extends HttpServlet{
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String name = new String(req.getParameter("name").getBytes("iso-8859-1"),"utf-8");
String email = new String(req.getParameter("email").getBytes("iso-8859-1"),"utf-8");
StudentService studentService = new StudentServiceImpl();
System.out.println(name + email);
studentService.insert(name,email);
}
}
hibernate的session封裝
public class GetSession {
private static SessionFactory sessionFactory;
static{
Configuration configure = new Configuration().configure();
sessionFactory = configure.buildSessionFactory();
}
public static Session getSession(){
return sessionFactory.openSession();
}
}
dao層調(diào)用數(shù)據(jù)層步驟
public class StudentDaoImpl implements StudentDao{
@Override
public void insert(String name, String email) {
Session session = GetSession.getSession();
Transaction transaction = session.beginTransaction();
Student student = new Student();
student.setName (name);
student.setEmail(email);
session.save(student);
transaction.commit();
session.close();
}
}
增牵敷、刪、改篙程、查(操作增摄闸、刪贬蛙、改時(shí)要先查詢一下)
@Test
public void test1(){
//獲取session
Session session = GetSession.getSession();
//
Transaction transaction = session.beginTransaction();
//查詢
Student student = session.get(Student.class, 5);
student.setName("笑笑");
//修改
session.update(student);
System.out.println(student);
//刪除
session.delete(student);
System.out.println(student);
//增加
Student student2 = new Student();
student2.setName("小輝");
student2.setEmail("xiaohui@163.com");
System.out.println(student2);
session.save(student2);
//提交事務(wù)
transaction.commit();
//釋放資源
session.close();
}
}