主要对上一篇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. 解析CentOS 7中系统文件与目录管理

    Linux目录结构 Linux目录结构是树形的目录结构 根目录 所有分区.目录.文件等的位置起点 整个树形目录结构中,使用独立的一个"/"表示 常见的子目录 目录 目录名称 目录 ...

  2. java相关网址汇总(myself)

    jar包下载网址 https://www.mvnjar.com/ 或者 https://mvnrepository.com/ 或者 http://www.java2s.com/Open-Source/ ...

  3. CSP模拟赛 Matrix(DP)

    题面 求出满足以下条件的 n*m 的 01 矩阵个数: (1)第 i 行第 1~li 列恰好有 1 个 1. (2)第 i 行第 ri~m 列恰好有 1 个 1. (3)每列至多有 1 个 1. n, ...

  4. 006_软件安装之_Proteus 8.6 SP2 Professional

    安装好像已经是直接破解的版本了 链接:https://pan.baidu.com/s/1NQDFJeJwmzoMIPZWhWNtFA提取码:ql8g 复制这段内容后打开百度网盘手机App,操作更方便哦

  5. sql server set赋值和select 赋值的区别以及使用方法

    sqlserver存储过程中SELECT 与 SET 对变量赋值的区别   (备注:虽然变量赋值使用方法已经不是问题,但是,了解一下select和set赋值的区别,还是提高了不少认识.应该有很多人并不 ...

  6. 学到了林海峰,武沛齐讲的Day20 装饰器

    import time def timmer(func): #func=test 装饰器架构 def wrapper(): start_time=time.time() func() #就是在运行te ...

  7. 小白学习.NET的初期经验

    对于.NET,刚开始确实很迷茫,确实,对于程序员这种职业我不是很了解,我以前是学数控的 ,对于我心目中的程序而言,程序就是我常用的那些代码,毕竟做了四五年的数控,同样都是程序,给我的了解就是都是代码: ...

  8. iwap问题

    1. 2. . . ========================================================================================== ...

  9. Kmeans聚类(lena图)

    lena512.raw 下载地址:https://files.cnblogs.com/files/jzcbest1016/lena512_20171219131444306.rar .raw文件可以用 ...

  10. dashucoding记录2019.6.7

    购买阿里云ECS主机 购买域名 申请备案 环境配置 安装wordpress 域名解析 在"产品与服务"中选择云服务器ECS 购买完域名之后建议去实名认证 域名购买链接:http:/ ...