主要对上一篇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;
} } }

点击源码下载


简易的CRM系统案例之易的CRM系统案例之JSP+MySQL+SSH框架版本的更多相关文章

  1. 简易的CRM系统案例之SpringMVC+JSP+MySQL+hibernate框架版本

    继续对上一版本进行改版,变成SpringMVC框架 简易的CRM系统案例之易的CRM系统案例之JSP+MySQL+SSH框架版本 src/spring.xml <?xml version=&qu ...

  2. 简易的CRM系统案例之SpringMVC+JSP+MySQL+myBatis框架版本

    主要对上一版DAO框架的替换hibernate变成myBatis 简易的CRM系统案例之SpringMVC+JSP+MySQL+hibernate框架版本 src/mybatis.xml <?x ...

  3. 简易的CRM系统案例之Struts2&Spring整合+Hibernate3+JSP+MySQL版本

    主要对上一篇Struts2&Spring整合的改造 简易的CRM系统案例之Struts2+Hibernate3+JSP+MySQL版本 src/bean.xml <beans xmlns ...

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

    改造上一版本的DAO层 简易的CRM系统案例之Struts2+JSP+MySQL版本 src文件下hibernate.cfg.xml <!DOCTYPE hibernate-configurat ...

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

    对简易的CRM系统案例之Servlet+Jsp+MySQL版本改进 Servlet优化为Struts2 学习 <?xml version="1.0" encoding=&qu ...

  6. 简易的CRM系统案例之Servlet+Jsp+MySQL版本

    数据库配置 datebase.properties driver=com.mysql.jdbc.Driver url=jdbc:mysql://127.0.0.1:3306/infos usernam ...

  7. Android实训案例(九)——答题系统的思绪,自己设计一个题库的体验,一个思路清晰的答题软件制作过程

    Android实训案例(九)--答题系统的思绪,自己设计一个题库的体验,一个思路清晰的答题软件制作过程 项目也是偷师的,决心研究一下数据库.所以写的还是很详细的,各位看官,耐着性子看完,实现结果不重要 ...

  8. Dynamics CRM ADFS及IFD部署后延长系统注销时间

    Dynamics CRM 部署IFD后,一段时间后登陆状态会失效,系统会提示让你重新登陆,可以通过延长失效时间来规避 在 powershell中执行如下指令 Set-ADFSRelyingPartyT ...

  9. SAS学习笔记之《SAS编程与数据挖掘商业案例》(1)系统简介和编程基础

    SAS学习笔记之<SAS编程与数据挖掘商业案例>(1)系统简介和编程基础 1. SAS系统简介 1.1 SAS是先编译后执行的语言,data步标志着编译的开始. 数据指针:当前内存缓存区, ...

随机推荐

  1. WIN10试用

    技巧 Win10技巧3.智能化窗口排列 排列窗口时后面的内容被挡住无疑让人倍感郁闷,Win10很好地解决了这个问题.当我们通过拖拽法将一个窗口分屏显示时(目前仅限1/2比例),操作系统就会利用屏幕剩余 ...

  2. Deep Module(深模块)

    Deep Module(深模块) 目录 1,模块化设计 2,接口里有什么 3,抽象 4,深模块 5,浅模块 6,Classitis 7,例子 8,结论 正文 类是不是越小越好?最近在读John Ous ...

  3. python函数式编程-匿名函数

    >>> map(lambda x: x * x, [, , , , , , , , ]) [, , , , , , , , ] 关键字lambda表示匿名函数,冒号前面的x表示函数参 ...

  4. JDBC_通过DriverManager获得数据库连接

    package day_18; import org.junit.Test; import java.io.InputStream; import java.sql.*; import java.sq ...

  5. SignalR 初体验

    目录 一.前言 二.服务端 2.1.站点服务端 2.2.宿主服务或客户端 2.3.持久连接和集线器 三.客户端 3.1.使用代理客户端 3.2.不使用代理客户端 一.前言 微软官方给的说明:ASP.N ...

  6. 17 webpack中babel的配置——静态属性与实例属性

    // class关键字,是ES6中提供的新语法,是用来实现ES6中面向对象编程的方式 class Person{ // 使用static关键字,可以定义静态属性 // 所谓的静态属性,就是可以直接通过 ...

  7. HDU 2454 Degree Sequence of Graph G——可简单图化&&Heavel定理

    题意 给你一个度序列,问能否构成一个简单图. 分析 对于可图化,只要满足度数之和是偶数,即满足握手定理. 对于可简单图化,就是Heavel定理了. Heavel定理:把度序列排成不增序,即 $deg[ ...

  8. 【Winfrom-无边框窗体】Winform如何拖动无边框窗体?

    去掉边框 this.FormBorderStyle = FormBorderStyle.None; 方法一: Point mouseOff;//鼠标移动位置变量 bool leftFlag;//标签是 ...

  9. 接口实现后台GZIP压缩,pako.js 前端解压

    import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException; ...

  10. 洛谷 P3627 [APIO2009]抢掠计划 题解

    Analysis 建图+强连通分量+SPFA求最长路 但要保证最后到达的点中包含酒馆 虽然思路并不难想,但要求的代码能力很高. #include<iostream> #include< ...