简易的CRM系统案例之Struts2+Hibernate3+JSP+MySQL版本

改造上一版本的DAO层

简易的CRM系统案例之Struts2+JSP+MySQL版本

src文件下hibernate.cfg.xml

<!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节点代表一个数据库 -->
    <session-factory>

        <!-- 1. 数据库连接配置 -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql:///infos</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>
        <!--
            数据库方法配置, hibernate在运行的时候,会根据不同的方言生成符合当前数据库语法的sql
         -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>


        <!-- 2. 其他相关配置 -->
        <!-- 2.1 显示hibernate在运行时候执行的sql语句 -->
        <property name="hibernate.show_sql">true</property>
        <!-- 2.2 格式化sql -->
        <property name="hibernate.format_sql">true</property>
        <!-- 2.3 自动建表  -->
        <property name="hibernate.hbm2ddl.auto">update</property>

        <!-- 3. 加载所有映射     -->
        <mapping resource="com/loaderman/crm/entity/Account.hbm.xml"/>
        <mapping resource="com/loaderman/crm/entity/User.hbm.xml"/>
        <mapping resource="com/loaderman/crm/entity/Policy.hbm.xml"/>

    </session-factory>
</hibernate-configuration>

User.hbm.xml

<?xml version="1.0"?>
<!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.loaderman.crm.entity">

    <class name="User" table="t_user">
        <!-- 主键 ,映射-->
        <id name="id" column="id">
            <generator class="native"/>
        </id>
        <!-- 非主键,映射 -->
        <property name="name" column="name"></property>
        <property name="sex" column="sex"></property>
        <property name="age" column="age"></property>
        <property name="telephone" column="telephone"></property>
        <property name="idCard" column="idCard"></property>
        <property name="address" column="address"></property>
        <property name="weixin" column="weixin"></property>
        <property name="qq" column="qq"></property>
        <property name="email" column="email"></property>
        <property name="job" column="job"></property>
        <property name="area" column="area"></property>
        <property name="grade" column="grade"></property>
        <property name="remark" column="remark"></property>


    </class>

</hibernate-mapping>
package com.loaderman.crm.dao.impl;

import com.loaderman.crm.dao.BaseDao;
import com.loaderman.crm.dao.UserDao;
import com.loaderman.crm.entity.User;
import com.loaderman.crm.util.HibernateUtils;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import java.util.List;


public class UserDaoimp extends BaseDao implements UserDao {

    @Override
    //获取所有客户信息
    public List<User> getAllUser() {
        Session session = null;
        Transaction tx = null;
        try {
            session = HibernateUtils.getSession();
            tx = session.beginTransaction();
            Query q = session.createQuery(" from User");
            return q.list();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            tx.commit();
            session.close();
        }

    }

    @Override
    public User getUserMoreInfo(User user) {

        Session session = null;
        Transaction tx = null;
        try {
            session = HibernateUtils.getSession();
            tx = session.beginTransaction();
            return (User) session.get(User.class, user.getId());
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            tx.commit();
            session.close();
        }


    }

    @Override
    public List<User> getUserByName(String name) {

        Session session = null;
        Transaction tx = null;
        try {
            session = HibernateUtils.getSession();
            tx = session.beginTransaction();
            Query q = session.createQuery("from User where name=?");
            // 注意:参数索引从0开始
            q.setParameter(0, name);
            // 执行查询
            return q.list();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            tx.commit();
            session.close();
        }
    }

    @Override
    //添加学生
    public int addUser(User user) {
        Session session = null;
        Transaction tx = null;
        try {
            session = HibernateUtils.getSession();
            tx = session.beginTransaction();
            session.save(user);
            return 1;
        } catch (Exception e) {
            throw new RuntimeException(e);

        } finally {
            tx.commit();
            session.close();
        }
    }

    @Override
    //删除
    public int delUser(User user) {
        Session session = null;
        Transaction tx = null;
        try {
            session = HibernateUtils.getSession();
            tx = session.beginTransaction();
            // 先根据id查询对象,再判断删除
            Object obj = session.get(User.class, user.getId());
            if (obj != null) {
                session.delete(obj);
            }
            return 1;
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            tx.commit();
            session.close();
        }


    }

    @Override
    public int modifyUser(User user) {
        Session session = null;
        Transaction tx = null;
        try {
            session = HibernateUtils.getSession();
            tx = session.beginTransaction();
            session.update(user);
            return 1;
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            tx.commit();
            session.close();
        }


    }


    //查找指定的客户存在不存在
    public boolean findUser(User user) {

        Session session = HibernateUtils.getSession();
        Transaction tx = session.beginTransaction();
        Criteria criteria = session.createCriteria(User.class);
        // 构建条件
        criteria.add(Restrictions.eq("name", user.getName()));
        criteria.add(Restrictions.eq("telephone", user.getTelephone()));
        List list = criteria.list();
        System.out.println("查询用户"+list.size());

        tx.commit();
        session.close();
        if (list.size()>0){
            return true;
        }else {
            System.out.println("没有查询到");
            return false;
        }

    }


}

点击下载源码

posted on 2019-01-22 13:52  LoaderMan  阅读(322)  评论(0编辑  收藏  举报

导航