Hibernate学习笔记(第三天)

Hibernate_day03

1.    今日任务

·Hibernate一对多|多对一关系表达及操作

·Hibernate多对多关系表达及操作

·案例练习1 – 使用hibernate完成CRM联系人的保存

·案例联系2 – 使用hibernate完成CRM用户分配角色的操作

2.    相关知识点

2.1 Hibernate一对多|多对一关系表达

表中的表达


对象中的表达

 

 

实体类代码:

public class Customer  {
    private Long cust_id;
    private String cust_name;
    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 linkMens = newHashSet<LinkMan>();
   
}


 

public class LinkMan {
    private Long lkm_id;
    private String lkm_name;
    private String lkm_gender;
    private String lkm_phone;
    private String lkm_mobile;
   
    private Customer customer;
 
}
 


配置文件

Customer.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD3.0//EN"
   "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.itheima.domain">
    <!--
        name属性为类名因为上面的 hibernate-mapping已经指定类的包名位置所以这里只要写类名即可如果上面没有指定需要写全类名com.itheima.domain.Customer
        table属性为数据库里表的名字
    -->
    <class name="Customer" table="cst_customer">
        <!-- 映射主键id字段 -->
        <id name="cust_id" column="cust_id">
            <generator class="native"></generator>
        </id>
        <!-- name属性为实体类的字段名 column为数据库里表的字段名一般为了规范都是设置成一样的 -->
        <property name="cust_name" column="cust_name"/>
        <property name="cust_source" column="cust_source"/>
        <property name="cust_industry" column="cust_industry"/>
        <property name="cust_level" column="cust_level"/>
        <property name="cust_linkman" column="cust_linkman"/>
        <property name="cust_phone" column="cust_phone"/>
        <property name="cust_mobile" column="cust_mobile"/>
        <!-- 配置一对多关系 -->
        <set name="linkMens" >
            <key column="lkm_cust_id"></key>
            <one-to-many class="LinkMan" />
        </set>
    </class>
</hibernate-mapping>


LinkMan.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD3.0//EN"
   "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.itheima.domain">
    <!--
        name属性为类名因为上面的 hibernate-mapping已经指定类的包名位置所以这里只要写类名即可如果上面没有指定需要写全类名com.itheima.domain.Customer
        table属性为数据库里表的名字
    -->
    <class name="LinkMan" table="cst_linkman">
        <!-- 映射主键id字段 -->
        <id name="lkm_id" column="lkm_id">
            <generator class="native"></generator>
        </id>
        <!-- name属性为实体类的字段名 column为数据库里表的字段名一般为了规范都是设置成一样的 -->
        <property name="lkm_name" column="lkm_name"/>
        <property name="lkm_gender" column="lkm_gender"/>
        <property name="lkm_phone" column="lkm_phone"/>
        <property name="lkm_mobile" column="lkm_mobile"/>
        <!-- 配置多对一 -->
        <many-to-one name="customer"  column="lkm_cust_id"class="Customer"></many-to-one>
    </class>
</hibernate-mapping>
 


2.2 Hibernate一对多|多对一操作

 

   /**
     * 测试保存
     */
    @Test
    public void f1(){
        //获取session
        Session session =HibernateUtils.getCurrentSession();
        //开启事务
        Transaction tx = session.beginTransaction();
        try {
            Customer c = newCustomer();
            c.setCust_name("王总");
           
            LinkMan lm1 = new LinkMan();
            lm1.setLkm_name("李经理");
            lm1.setCustomer(c);
            LinkMan lm2 = new LinkMan();
            lm2.setLkm_name("张经理");
            lm2.setCustomer(c);
            //添加联系人
            c.getLinkMens().add(lm1);
            c.getLinkMens().add(lm2);
            //保存
            session.save(c);
            session.save(lm1);
            session.save(lm2);
            //提交事务
            tx.commit();
        } catch (Exceptione) {
            // TODO: handleexception
            tx.rollback();
            throw e;
        }
    }
 

2.3  Hibernate一对多|多对一进阶操作

2.3.1       级联操作

配置文件:

<!--
    配置一对多关系
    级联操作:cascade
         save-update:级联保存更新
         delete:级联删除(谨慎使用)
         all:save-update+delete
    级联操作目的:减少代码量
    -->
<set name="linkMens" cascade="save-update">
    <key column="lkm_cust_id"></key>
    <one-to-many class="LinkMan" />
</set>
代码:
/**
 * 测试级联操作
 */
@Test
public void f2(){
    //获取session
    Session session = HibernateUtils.getCurrentSession();
    //开启事务
    Transaction tx = session.beginTransaction();
    try {
        Customer c = newCustomer();
        c.setCust_name("张总");
       
        LinkMan lm1 = new LinkMan();
        lm1.setLkm_name("小李");
        lm1.setCustomer(c);
        LinkMan lm2 = new LinkMan();
        lm2.setLkm_name("小王");
        lm2.setCustomer(c);
        //添加联系人
        c.getLinkMens().add(lm1);
        c.getLinkMens().add(lm2);
        //保存
        session.save(c);
        //提交事务
        tx.commit();
    } catch (Exceptione) {
        // TODO: handleexception
        tx.rollback();
        throw e;
    }
}


2.3.2关系维护



在保存的时候.双向关联所以产生了多余的sql语句。可在配置文件中使用insert语句设置是否维护关系。

配置文件:

<!--
    配置一对多关系
    级联操作:cascade
         save-update:级联保存更新
         delete:级联删除
         all:save-update+delete
    级联操作目的:减少代码量
    inverse属性:配置关系是否维护
        true:customer不维护关系
        false(默认值):customer维护关系
        原则:无论怎么放弃,总有一方要维护关系
        一对多关系中:一的一方放弃,也只能一的一方放弃,多的一方不能放弃
    -->
<set name="linkMens" cascade="save-update"inverse="true">
    <key column="lkm_cust_id"></key>
    <one-to-many class="LinkMan" />
</set>


测试结果:

2.4  Hibernate多对多关系表达

表中


对象中


ORM元数据:

 

User实体:

public class User {
    private Long user_id;
    private String user_name;
    private String user_password;
    private Character user_state;
   
    private Set<Role> roles= new HashSet<Role>();
 
    public User() {
        super();
        // TODOAuto-generated constructor stub
    }
 
    public User(Long user_id, String user_name, String user_password, Characteruser_state,Set<Role> roles) {
        super();
        this.user_id = user_id;
        this.user_name = user_name;
        this.user_password = user_password;
        this.user_state = user_state;
        this.roles = roles;
    }
 
    public Long getUser_id() {
        return user_id;
    }
 
    public voidsetUser_id(Long user_id) {
        this.user_id = user_id;
    }
 
    public String getUser_name(){
        return user_name;
    }
 
    public voidsetUser_name(String user_name) {
        this.user_name = user_name;
    }
 
    public StringgetUser_password() {
        return user_password;
    }
 
    public voidsetUser_password(String user_password) {
        this.user_password = user_password;
    }
 
    public CharactergetUser_state() {
        return user_state;
    }
 
    public voidsetUser_state(Character user_state) {
        this.user_state = user_state;
    }
 
    public Set<Role>getRoles() {
        returnroles;
    }
 
    public voidsetRoles(Set<Role> roles) {
        this.roles = roles;
    }
 
    @Override
    public String toString() {
        return "User[user_id=" + user_id + ", user_name=" + user_name + ",user_password=" + user_password
                + ",user_state=" + user_state + ",roles=" + roles + "]";
    }
   
   
}


Role实体:

public class Role {
    private Long role_id;
    private String role_name;
    private String role_memo;
   
    private Set<User> users = newHashSet<User>();
 
    public Role() {
        super();
        // TODOAuto-generated constructor stub
    }
 
    public Role(Long role_id, String role_name, String role_memo, Set<User> users) {
        super();
        this.role_id = role_id;
        this.role_name = role_name;
        this.role_memo = role_memo;
        this.users = users;
    }
 
    public Long getRole_id() {
        return role_id;
    }
 
    public voidsetRole_id(Long role_id) {
        this.role_id = role_id;
    }
 
    public String getRole_name(){
        return role_name;
    }
 
    public voidsetRole_name(String role_name) {
        this.role_name = role_name;
    }
 
    public String getRole_memo(){
        return role_memo;
    }
 
    public void setRole_memo(Stringrole_memo) {
        this.role_memo = role_memo;
    }
 
    public Set<User> getUsers(){
        return users;
    }
 
    public voidsetUsers(Set<User>users) {
        this.users = users;
    }
 
    @Override
    public String toString() {
        return "Role[role_id=" + role_id + ", role_name=" + role_name + ",role_memo=" + role_memo + ",users=" + users
                + "]";
    }
}
 


User.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD3.0//EN"
   "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.itheima.domain">
    <!--
        name属性为类名因为上面的 hibernate-mapping已经指定类的包名位置所以这里只要写类名即可如果上面没有指定需要写全类名com.itheima.domain.Customer
        table属性为数据库里表的名字
    -->
    <class name="User" table="sys_user">
        <!-- 映射主键id字段 -->
        <id name="user_id" column="user_id">
            <generator class="native"></generator>
        </id>
        <!-- name属性为实体类的字段名 column为数据库里表的字段名一般为了规范都是设置成一样的 -->
        <property name="user_name" column="user_name"/>
        <property name="user_password" column="user_password"/>
        <property name="user_state" column="user_state"/>
       
        <!-- 配置多对多关系 -->
        <set name="roles" table="sys_user_role">
            <key column="user_id"></key>
            <many-to-many class="Role" column="role_id"></many-to-many>
        </set>
    </class>
</hibernate-mapping>
 


Role.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD3.0//EN"
   "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.itheima.domain">
    <!--
        name属性为类名因为上面的 hibernate-mapping已经指定类的包名位置所以这里只要写类名即可如果上面没有指定需要写全类名com.itheima.domain.Customer
        table属性为数据库里表的名字
    -->
    <class name="Role" table="sys_role">
        <!-- 映射主键id字段 -->
        <id name="role_id" column="role_id">
            <generator class="native"></generator>
        </id>
        <!-- name属性为实体类的字段名 column为数据库里表的字段名一般为了规范都是设置成一样的 -->
        <property name="role_name" column="role_name"/>
        <property name="role_memo" column="role_memo"/>
        <!-- 配置多对多 -->
        <set name="users" table="sys_user_role">
            <key column="role_id"></key>
            <many-to-many class="User" column="user_id"></many-to-many>
        </set>
    </class>
</hibernate-mapping>


 

2.5  Hibernate多对多操作

测试保存

@Test
public void f1(){
    Session session =HibernateUtils.getCurrentSession();
    Transaction tx = session.beginTransaction();
    //执行代码
    User u1 = new User();
    u1.setUser_name("小王");
    User u2 = new User();
    u2.setUser_name("小林");
   
    Role r1 = new Role();
    r1.setRole_name("经理");
    Role r2= new Role();
    r2.setRole_name("财务");
   
    u1.getRoles().add(r1);
    u1.getRoles().add(r2);
   
    u2.getRoles().add(r1);
    u2.getRoles().add(r2);
   
    r1.getUsers().add(u1);
    r1.getUsers().add(u2);
   
    r2.getUsers().add(u1);
    r2.getUsers().add(u2);
    //保存
    session.save(u1);
    session.save(u2);
    session.save(r1);
    session.save(r2);
   
    tx.commit();
}


测试结果:


代码看似没问题,实际上运行爆出了异常,原因是双方都维护了关系,导致两边都执行update语句所以爆出异常

2.6 Hibernate多对多操作进阶

2.6.1关系维护

为了解决上述问题,将来在开发中,如果遇到多对多关系.一定要选择一方放弃维护关系。一般谁来放弃要看业务方向. 例如录入员工时,需要为员工指定所属角色. 那么业务方向就是由员工维护角色. 角色不需要维护与员工关系.角色放弃维护

修改Role.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD3.0//EN"
   "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.itheima.domain">
    <!--
        name属性为类名因为上面的 hibernate-mapping已经指定类的包名位置所以这里只要写类名即可如果上面没有指定需要写全类名com.itheima.domain.Customer
        table属性为数据库里表的名字
    -->
    <class name="Role" table="sys_role">
        <!-- 映射主键id字段 -->
        <id name="role_id" column="role_id">
            <generator class="native"></generator>
        </id>
        <!-- name属性为实体类的字段名 column为数据库里表的字段名一般为了规范都是设置成一样的 -->
        <property name="role_name" column="role_name"/>
        <property name="role_memo" column="role_memo"/>
        <!-- 配置多对多
使用inverse属性
            true: 放弃维护外键关系
            false(默认值):维护关系 -->
        <set name="users" table="sys_user_role"inverse="true">
            <key column="role_id"></key>
            <many-to-many class="User" column="user_id"></many-to-many>
        </set>
    </class>
</hibernate-mapping>


测试代码:

@Test
public void f1(){
    Session session =HibernateUtils.openSession();
    Transaction tx = session.beginTransaction();
    //执行代码
    User u1 = new User();
    u1.setUser_name("小王");
    User u2 = new User();
    u2.setUser_name("小林");
   
    Role r1 = new Role();
    r1.setRole_name("经理");
    Role r2= new Role();
    r2.setRole_name("财务");
   
    u1.getRoles().add(r1);
    u1.getRoles().add(r2);
    u2.getRoles().add(r1);
    u2.getRoles().add(r2);
    //保存
    session.save(u1);
    session.save(u2);
    session.save(r1);
    session.save(r2);
   
    tx.commit();
    session.close();
}


2.6.2 级联操作

User.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD3.0//EN"
   "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.itheima.domain">
    <!--
        name属性为类名因为上面的 hibernate-mapping已经指定类的包名位置所以这里只要写类名即可如果上面没有指定需要写全类名com.itheima.domain.Customer
        table属性为数据库里表的名字
    -->
    <class name="User" table="sys_user">
        <!-- 映射主键id字段 -->
        <id name="user_id" column="user_id">
            <generator class="native"></generator>
        </id>
        <!-- name属性为实体类的字段名 column为数据库里表的字段名一般为了规范都是设置成一样的 -->
        <property name="user_name" column="user_name"/>
        <property name="user_password" column="user_password"/>
        <property name="user_state" column="user_state"/>
       
        <!-- 配置多对多关系 -->
        <set name="roles" table="sys_user_role"cascade="save-update">
            <key column="user_id"></key>
            <many-to-many class="Role" column="role_id"></many-to-many>
        </set>
    </class>
</hibernate-mapping>


测试代码:

@Test
public void f1(){
    Session session =HibernateUtils.openSession();
    Transaction tx = session.beginTransaction();
    //执行代码
    User u1 = new User();
    u1.setUser_name("小王");
    User u2 = new User();
    u2.setUser_name("小林");
   
    Role r1 = new Role();
    r1.setRole_name("经理");
    Role r2= new Role();
    r2.setRole_name("财务");
   
    u1.getRoles().add(r1);
    u1.getRoles().add(r2);
    u2.getRoles().add(r1);
    u2.getRoles().add(r2);
    //保存
    session.save(u1);
    session.save(u2);
   
    tx.commit();
    session.close();
}


 

3.    CRM案例-添加联系人

 

 

 

实体类:

    

public class Customer  {
    private Long cust_id;
    private String cust_name;
    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 = newHashSet<LinkMan>();
 
    public Customer() {
        super();
        // TODOAuto-generated constructor stub
    }
public Customer(Long cust_id, String cust_name, String cust_source, String cust_industry, String cust_level,
            String cust_linkman, String cust_phone, String cust_mobile,Set<LinkMan> linkMens) {
        super();
        this.cust_id = cust_id;
        this.cust_name = cust_name;
        this.cust_source = cust_source;
        this.cust_industry = cust_industry;
        this.cust_level = cust_level;
        this.cust_linkman = cust_linkman;
        this.cust_phone = cust_phone;
        this.cust_mobile = cust_mobile;
        this.linkMens = linkMens;
    }
 
    public Long getCust_id() {
        return cust_id;
    }
 
    public voidsetCust_id(Long cust_id) {
        this.cust_id = cust_id;
    }
 
    public String getCust_name(){
        return cust_name;
    }
 
    public voidsetCust_name(String cust_name) {
        this.cust_name = cust_name;
    }
 
    public String getCust_source(){
        return cust_source;
    }
 
    public voidsetCust_source(String cust_source) {
        this.cust_source = cust_source;
    }
 
    public StringgetCust_industry() {
        return cust_industry;
    }
 
    public voidsetCust_industry(String cust_industry) {
        this.cust_industry = cust_industry;
    }
 
    public StringgetCust_level() {
        return cust_level;
    }
 
    public voidsetCust_level(String cust_level) {
        this.cust_level = cust_level;
    }
 
    public StringgetCust_linkman() {
        return cust_linkman;
    }
 
    public void setCust_linkman(Stringcust_linkman) {
        this.cust_linkman = cust_linkman;
    }
 
    public StringgetCust_phone() {
        return cust_phone;
    }
 
    public voidsetCust_phone(String cust_phone) {
        this.cust_phone = cust_phone;
    }
 
    public StringgetCust_mobile() {
        return cust_mobile;
    }
 
    public voidsetCust_mobile(String cust_mobile) {
        this.cust_mobile = cust_mobile;
    }
 
    public Set<LinkMan> getLinkMens(){
        return linkMens;
    }
 
    public voidsetLinkMens(Set<LinkMan> linkMens) {
        this.linkMens = linkMens;
    }
 
    @Override
    public String toString() {
        return "Customer[cust_id=" + cust_id + ", cust_name=" + cust_name + ",cust_source=" + cust_source
                + ",cust_industry=" + cust_industry + ",cust_level=" + cust_level + ",cust_linkman=" + cust_linkman
                + ", cust_phone=" + cust_phone + ",cust_mobile=" + cust_mobile + ",linkMens=" + linkMens + "]";
    }
}

 

 

 

public class LinkMan {
    private Long lkm_id;
    private String lkm_name;
    private String lkm_gender;
    private String lkm_phone;
    private String lkm_mobile;
   
    private Customer customer;
 
    private Long cust_id; //用于接受form表单的参数
   
   
    public Long getCust_id() {
        return cust_id;
    }
 
    public voidsetCust_id(Long cust_id) {
        this.cust_id = cust_id;
    }
 
    public Long getLkm_id() {
        return lkm_id;
    }
 
    public voidsetLkm_id(Long lkm_id) {
        this.lkm_id = lkm_id;
    }
 
    public String getLkm_name(){
        return lkm_name;
    }
 
    public voidsetLkm_name(String lkm_name) {
        this.lkm_name = lkm_name;
    }
 
    public StringgetLkm_gender() {
        return lkm_gender;
    }
 
    public voidsetLkm_gender(String lkm_gender) {
        this.lkm_gender = lkm_gender;
    }
 
    public String getLkm_phone(){
        return lkm_phone;
    }
 
    public voidsetLkm_phone(String lkm_phone) {
        this.lkm_phone = lkm_phone;
    }
 
    public StringgetLkm_mobile() {
        return lkm_mobile;
    }
 
    public voidsetLkm_mobile(String lkm_mobile) {
        this.lkm_mobile = lkm_mobile;
    }
 
    public CustomergetCustomer() {
        return customer;
    }
 
    public voidsetCustomer(Customer customer) {
        this.customer = customer;
    }
 
    public LinkMan() {
        super();
        // TODO Auto-generatedconstructor stub
    }
 
    public LinkMan(Long lkm_id, String lkm_name, String lkm_gender, String lkm_phone, String lkm_mobile,
            Customer customer) {
        super();
        this.lkm_id = lkm_id;
        this.lkm_name = lkm_name;
        this.lkm_gender = lkm_gender;
        this.lkm_phone = lkm_phone;
        this.lkm_mobile = lkm_mobile;
        this.customer = customer;
    }
 
    @Override
    public String toString() {
        return "LinkMan[lkm_id=" + lkm_id + ", lkm_name=" + lkm_name + ",lkm_gender=" + lkm_gender + ",lkm_phone="
                + lkm_phone + ",lkm_mobile=" + lkm_mobile + ",customer=" + customer + "]";
    }
}
 


配置文件

Customer.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD3.0//EN"
   "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.itheima.domain">
    <!--
        name属性为类名因为上面的 hibernate-mapping已经指定类的包名位置所以这里只要写类名即可如果上面没有指定需要写全类名com.itheima.domain.Customer
        table属性为数据库里表的名字
    -->
    <class name="Customer" table="cst_customer">
        <!-- 映射主键id字段 -->
        <id name="cust_id" column="cust_id">
            <generator class="native"></generator>
        </id>
        <!-- name属性为实体类的字段名 column为数据库里表的字段名一般为了规范都是设置成一样的 -->
        <property name="cust_name" column="cust_name"/>
        <property name="cust_source" column="cust_source"/>
        <property name="cust_industry" column="cust_industry"/>
        <property name="cust_level" column="cust_level"/>
        <property name="cust_linkman" column="cust_linkman"/>
        <property name="cust_phone" column="cust_phone"/>
        <property name="cust_mobile" column="cust_mobile"/>
        <set name="linkMens" inverse="true"cascade="save-update">
            <key column="lkm_cust_id"></key>
            <one-to-many class="LinkMan"/>
        </set>
    </class>
</hibernate-mapping>


 

LinkMan.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD3.0//EN"
   "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.itheima.domain">
    <!--
        name属性为类名因为上面的 hibernate-mapping已经指定类的包名位置所以这里只要写类名即可如果上面没有指定需要写全类名 com.itheima.domain.Customer
        table属性为数据库里表的名字
    -->
    <class name="LinkMan" table="cst_linkman">
        <!-- 映射主键id字段 -->
        <id name="lkm_id" column="lkm_id">
            <generator class="native"></generator>
        </id>
        <!-- name属性为实体类的字段名 column为数据库里表的字段名一般为了规范都是设置成一样的 -->
        <property name="lkm_name" column="lkm_name"/>
        <property name="lkm_gender" column="lkm_gender"/>
        <property name="lkm_phone" column="lkm_phone"/>
        <property name="lkm_mobile" column="lkm_mobile"/>
        <!-- 配置多对一 -->
        <many-to-one name="customer"  column="lkm_cust_id"class="Customer"></many-to-one>
    </class>
</hibernate-mapping>
 
 


LinkManServlet:

public classLinkManServlet extends BaseServlet {
         privatestatic finallong serialVersionUID= 1L;
 
         LinkManService lms= new LinkManServiceImpl();
    publicString add(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
    LinkManlinkMan = newLinkMan();
    try {
             //从表单获取link对象并封装
                            BeanUtils.populate(linkMan, request.getParameterMap());
                           
                            //调用业务层添加联系人
                            lms.add(linkMan);
                           
                            //重定向到联系人列表页面
                            response.sendRedirect(request.getContextPath()+"/linkMan?method=list¤tPage=1");
                   } catch(Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                   }
    return null;
         }
 
}


BaseServlet:

public class BaseServlet extendsHttpServlet {
    private static final long serialVersionUID = 1L;
 
    protected voidservice(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {
        try {
            //获取方法名
            String m = request.getParameter("method");
            if (m == null) {
                m = "index";
            }
            //获取类本身
            Class clazz =getClass();
            //获取方法对象
            Method method = clazz.getMethod(m,HttpServletRequest.class,HttpServletResponse.class);
            //调用方法
            String url = (String)method.invoke(this, request,response);
            if (url!=null) {
                request.getRequestDispatcher(url).forward(request, response);
            }
           
        } catch (Exceptione) {
           
        }
    }
    public Stringindex(HttpServletRequest request, HttpServletResponse response){
        return null;
    }
 
}


 

 

Service层

public classLinkManServiceImpl implements LinkManService {
    CustomerDao customerDao = newCustomerDaoImpl();
    LinkManDao linkManDao = newLinkManDaoImpl();
    @Override
    public voidadd(LinkMan linkMan) throws Exception{
        // TODOAuto-generated method stub
        //根据id查询客户
        Customer c = customerDao.getCustomer(linkMan.getCust_id());
        //设置客户
        linkMan.setCustomer(c);
        //调用dao层添加联系人
        linkManDao.add(linkMan);
    }
}


Dao层

public classLinkManDaoImpl implements LinkManDao {
 
    @Override
    public voidadd(LinkMan linkMan) throws Exception{
        // TODOAuto-generated method stub
        Session session =HibernateUtils.openSession();
        Transaction tx = session.beginTransaction();
       
        session.save(linkMan);
        tx.commit();
    }
 
}


 测试结果:


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值