简易的CRM系统案例之易的CRM系统案例之JSP+MySQL+SSH框架版本
主要对上一篇hibernate与Spring进行整合改进
简易的CRM系统案例之Struts2+Hibernate3+JSP+MySQL版本
bean-base.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 所有配置的公共部门 --> <!-- 1) 连接池实例 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql:///infos"></property> <property name="user" value="root"></property> <property name="password" value="root"></property> <property name="initialPoolSize" value="3"></property> <property name="maxPoolSize" value="6"></property> </bean> <!-- 2) SessionFactory实例创建 --> <!-- 所有的配置都由spring维护(项目中不需要hibernate.cfg.xml啦) --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- a. 连接池 --> <property name="dataSource" ref="dataSource"></property> <!-- b. hibernate常用配置: 方言、显示sql、自动建表等 --> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <!-- c. 映射配置 --> <property name="mappingLocations"> <list> <value>classpath:com/loaderman/crm/entity/*.hbm.xml</value> </list> </property> </bean> <!-- 3) 事务配置 --> <!-- # 事务管理器 --> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- # 事务增强 --> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="*" read-only="false"/> </tx:attributes> </tx:advice> <!-- # AOP配置 --> <aop:config> <aop:pointcut expression="execution(* com.loaderman.crm.dao.*.*(..))" id="pt"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/> </aop:config> </beans>
bean-dao.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <bean id="accountDao" class="com.loaderman.crm.dao.impl.AccountDaoimp" scope="singleton" lazy-init="false"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="policyDao" class="com.loaderman.crm.dao.impl.PolicyDaoimp" scope="singleton" lazy-init="false"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="userDao" class="com.loaderman.crm.dao.impl.UserDaoimp" scope="singleton" lazy-init="false"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <aop:aspectj-autoproxy proxy-target-class="true"/> </beans>
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 org.hibernate.*; import org.hibernate.criterion.Restrictions; import java.util.List; public class UserDaoimp extends BaseDao implements UserDao { // Spring与Hibernate整合: IOC容器注入 private SessionFactory sessionFactory; public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } @Override //获取所有客户信息 public List<User> getAllUser() { System.out.println("getAllUser"); try { System.out.println("sessionFactory.getCurrentSession()"+sessionFactory.getCurrentSession()); Query q = sessionFactory.getCurrentSession().createQuery(" from User"); return q.list(); } catch (Exception e) { throw new RuntimeException(e); } finally { } } @Override public User getUserMoreInfo(User user) { // Session session = null; // Transaction tx = null; try { // session = HibernateUtils.getSession(); // tx = session.beginTransaction(); return (User) sessionFactory.getCurrentSession().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 = sessionFactory.getCurrentSession().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 = sessionFactory.getCurrentSession().beginTransaction(); sessionFactory.getCurrentSession().save(user); return 1; } catch (Exception e) { throw new RuntimeException(e); } finally { // tx.commit(); // session.close(); } } @Override //删除 public int delUser(User user) { try { // 先根据id查询对象,再判断删除 Object obj = sessionFactory.getCurrentSession().get(User.class, user.getId()); if (obj != null) { sessionFactory.getCurrentSession().delete(obj); } return 1; } catch (Exception e) { throw new RuntimeException(e); } finally { } } @Override public int modifyUser(User user) { try { sessionFactory.getCurrentSession().update(user); return 1; } catch (Exception e) { throw new RuntimeException(e); } finally { } } //查找指定的客户存在不存在 public boolean findUser(User user) { Criteria criteria = sessionFactory.getCurrentSession().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()); if (list.size()>0){ return true; }else { System.out.println("没有查询到"); return false; } } }
点击源码下载
最后,关注【码上加油站】微信公众号后,有疑惑有问题想加油的小伙伴可以码上加入社群,让我们一起码上加油吧!!!