hibernateAPI详解
Configuration
创建
加载主配置
创建sessionFactory
SessionFactory
session
获得事务
增
查
改
删
Transaction封装事务的操作
打开事务
提交事务
回滚事务
CRM练习:保存客户
CRM:customer relation manager 客户关系管理系统
1、创建web项目
2、导包
hibernate包、数据库驱动包、标签库包、BeanUtils包
3、引入静态页面
4、搭建hibernate框架
5、思路分析
6、开发
7、测试
package com.web; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.beanutils.BeanUtils; import com.domain.Customer; import com.service.CustomerService; public class AddCustomerServlet extends HttpServlet { private CustomerService customerService = new CustomerService(); public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 1、获取数据 request.setCharacterEncoding("UTF-8"); Map<String, String[]> map = request.getParameterMap(); // 2、创建对象 Customer customer = new Customer(); // 3、封装数据 try { BeanUtils.populate(customer, map); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } // 4、调用service中的保存方法 customerService.save(customer); // 5、重定向 response.sendRedirect(request.getContextPath()+"/ListCustomerServlet"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
package com.service; import com.dao.CustomerDao; import com.domain.Customer; public class CustomerService { private CustomerDao customerDao = new CustomerDao(); public void save(Customer customer) { customerDao.save(customer); } }
package com.dao; import org.hibernate.Session; import org.hibernate.Transaction; import com.domain.Customer; import com.util.HibernateUtil; public class CustomerDao { public void save(Customer customer) { Session session = HibernateUtil.openSession(); Transaction tx = session.beginTransaction(); session.save(customer); tx.commit(); session.close(); } }
package com.domain; import java.util.HashSet; import java.util.Set; public class Customer { private Long cust_id; // private String cust_id; private String cust_name; private Long cust_user_id; private Long cust_create_id; private String cust_source; private String cust_industry; private String cust_level; private String cust_linkman; private String cust_phone; private String cust_mobile; //表达与联系人表一对多的关系 private Set<LinkMan> linkmens =new HashSet<LinkMan>(); public Set<LinkMan> getLinkmens() { return linkmens; } public void setLinkmens(Set<LinkMan> linkmens) { this.linkmens = linkmens; } public Long getCust_id() { return cust_id; } public void setCust_id(Long cust_id) { this.cust_id = cust_id; } public String getCust_name() { return cust_name; } // public String getCust_id() { // return cust_id; // } // public void setCust_id(String cust_id) { // this.cust_id = cust_id; // } public void setCust_name(String cust_name) { this.cust_name = cust_name; } public Long getCust_user_id() { return cust_user_id; } public void setCust_user_id(Long cust_user_id) { this.cust_user_id = cust_user_id; } public Long getCust_create_id() { return cust_create_id; } public void setCust_create_id(Long cust_create_id) { this.cust_create_id = cust_create_id; } public String getCust_source() { return cust_source; } public void setCust_source(String cust_source) { this.cust_source = cust_source; } public String getCust_industry() { return cust_industry; } public void setCust_industry(String cust_industry) { this.cust_industry = cust_industry; } public String getCust_level() { return cust_level; } public void setCust_level(String cust_level) { this.cust_level = cust_level; } public String getCust_linkman() { return cust_linkman; } public void setCust_linkman(String cust_linkman) { this.cust_linkman = cust_linkman; } public String getCust_phone() { return cust_phone; } public void setCust_phone(String cust_phone) { this.cust_phone = cust_phone; } public String getCust_mobile() { return cust_mobile; } public void setCust_mobile(String cust_mobile) { this.cust_mobile = cust_mobile; } @Override public String toString() { return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + ", cust_user_id=" + cust_user_id + ", cust_create_id=" + cust_create_id + ", cust_source=" + cust_source + ", cust_industry=" + cust_industry + ", cust_level=" + cust_level + ", cust_linkman=" + cust_linkman + ", cust_phone=" + cust_phone + ", cust_mobile=" + cust_mobile + "]"; } }
<?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 package="com.domain"> <!-- class中的name写实体类名,table写数据库中对应的表名 --> <class name="Customer" table="cst_customer"> <!--id标签代表主键 --> <id name="cust_id" column="cust_id"> <!-- 代表主键的生成模式 --> <generator class="native"></generator> <!-- identity适用于Mysql主键生成策略 <generator class="identity"></generator> --> </id> <property name="cust_name" column="cust_name" ></property> <property name="cust_user_id" column="cust_user_id" ></property> <property name="cust_create_id" column="cust_create_id" ></property> <property name="cust_source" column="cust_source" ></property> <property name="cust_industry" column="cust_industry" ></property> <property name="cust_level" column="cust_level" ></property> <property name="cust_linkman" column="cust_linkman" ></property> <property name="cust_phone" column="cust_phone" ></property> <property name="cust_mobile" column="cust_mobile" ></property> <!-- 配置一对多的关系 --> <set name="linkmens"> <key column="lkm_cust_id"></key> <one-to-many class="LinkMan"/> </set> </class> </hibernate-mapping>
package com.util; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtil { private static SessionFactory sf; static{ //加载hibernate主配置文件 Configuration conf=new Configuration().configure(); sf=conf.buildSessionFactory(); } //获得全新的session public static Session openSession(){ return sf.openSession(); } //获取一个与当前线程绑定的session public static Session getCurrentSession(){ return sf.getCurrentSession(); } }
<?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> <!-- #hibernate.dialect org.hibernate.dialect.MySQLDialect #hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect #hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect #hibernate.connection.driver_class com.mysql.jdbc.Driver #hibernate.connection.url jdbc:mysql:///test #hibernate.connection.username gavin #hibernate.connection.password--> <!--必须配置的5条 --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql:///crm?useUnicode=true&characterEncoding=UTF8</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">123456</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!--可选配置 #hibernate.show_sql true 自动打印sql在控制台 #hibernate.format_sql true 规范格式化打印在控制台的sql--> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property> <!-- 配置hibernate操作数据库隔离级别 --> <!-- #hibernate.connection.isolation 4 --> <property name="hibernate.connection.isolation">4</property> <!-- 获得与当前线程绑定session对象时必须要配置的 --> <property name="hibernate.current_session_context_class">thread</property> <!-- 如果没有表就会自动生成表,如果有就会自动更新表 #hibernate.hbm2ddl.auto update --> <property name="hibernate.hbm2ddl.auto">update</property> <mapping resource="com/domain/Customer.hbm.xml"/> <mapping resource="com/domain/LinkMan.hbm.xml"/> <mapping resource="com/domain/User.hbm.xml"/> <mapping resource="com/domain/Role.hbm.xml"/> </session-factory> </hibernate-configuration>
原文地址:https://www.cnblogs.com/Java-125/p/9138319.html
时间: 2024-10-10 00:04:44