Spring4.2.3+Hibernate4.3.11整合( IntelliJ maven项目)
1. 在IntelliJ中新建maven项目
给出一个建好的示例
2. 在pom.xml中配置依赖
包括:
spring-context
spring-orm
hibernate-core
mysql
commons-dbcp
aspectjweaver
代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.xin</groupId>
<artifactId>spring-test</artifactId>
<version>1.0-SNAPSHOT</version> <dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.11.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>
</dependencies>
</project>
3. resources右键new一个Xml Configuration File--Spring Config配置文件:spring-config.xml(或者applicationContext.xml)
配置dataSource、sessionFactory及事务机制。
可能会提示一个Application Context的什么配置,按提示操作即可;或者在IntelliJ工具的Project Structure下的Facets中进行spring的配置。
spring-config.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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/javaee"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>com.test.app.domain/User.hbm.xml</value><!--步骤4中的配置-->
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=true
<!--hibernate.format_sql=true-->
</value>
</property>
</bean> <!--步骤5中的配置-->
<bean id="userDao" class="com.test.app.dao.impl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean> <!--事务机制-->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 定义事务规则 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/> <!--所有以'get'开头的方法都是read-only的-->
<tx:method name="*"/><!-其他方式使用默认的事务设置-->
</tx:attributes>
</tx:advice>
<!-- 定义事务入口 -->
<aop:config>
<aop:pointcut id="txPointcut" expression="execution(* com.test.app.dao.impl.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" />
</aop:config>
</beans>
4. 新建User实体类,及配置文件User.hbm.xml(置于resources目录下)
并在spring-config.xml中名为sessionFactory的bean中进行相关配置。
public class User {
private int id;
private String name;
private int age;
//省略setter和getter方法
}
User.hbm.xml代码如下
<?xml version='1.0' encoding='utf-8'?>
<!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.test.app.domain">
<class name="User" table="user">
<id name="id" column="user_id">
<generator class="identity"/><!--id生成策略:自增-->
</id>
<property name="name" column="name"/>
<property name="age" column="age"/>
</class>
</hibernate-mapping>
5. 新建UserDao接口及其实现类UserDaoImpl,并在spring-config.xml中配置该bean。
public interface UserDao {
Integer insert(User user);//增
void delete(User user); //删
void update(User user); //改
User find(int id); //查
}
UserDaoImpl实现类代码如下:
public class UserDaoImpl implements UserDao {
SessionFactory sessionFactory; public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
} @Override
public Integer insert(User user) {
return (Integer) sessionFactory.getCurrentSession().save(user);
} @Override
public void delete(User user) {
sessionFactory.getCurrentSession().delete(user);
} @Override
public void update(User user) {
sessionFactory.getCurrentSession().update(user);
} @Override
public User find(int id) {
return (User) sessionFactory.getCurrentSession().get(User.class, id);
}
}
6. 新建测试类MainTest
public class MainTest {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml");
UserDao userDao = (UserDao) applicationContext.getBean("userDao"); User user = new User();
user.setName("xin");
user.setAge(18); userDao.insert(user);
}
}
Spring4.2.3+Hibernate4.3.11整合( IntelliJ maven项目)的更多相关文章
- Spring4.2.3+Hibernate4.3.11整合( IntelliJ maven项目)(使用Annotation注解)(Junit测试类)
1. 在IntelliJ中新建maven项目 给出一个建好的示例 2. 在pom.xml中配置依赖 包括: spring-context spring-orm hibernate-core mysql ...
- (转)Spring4.2.5+Hibernate4.3.11+Struts2.3.24整合开发
http://blog.csdn.net/yerenyuan_pku/article/details/52902851 前面我们已经学会了Spring4.2.5+Hibernate4.3.11+Str ...
- (转)Spring4.2.5+Hibernate4.3.11组合开发
http://blog.csdn.net/yerenyuan_pku/article/details/52887573 搭建和配置Spring与Hibernate整合的环境 今天我们来学习Spring ...
- spring mvc4.1.6 + spring4.1.6 + hibernate4.3.11 + mysql5.5.25 开发环境搭建及相关说明
一.准备工作 开始之前,先参考上一篇: struts2.3.24 + spring4.1.6 + hibernate4.3.11 + mysql5.5.25 开发环境搭建及相关说明 struts2.3 ...
- (转)Spring4.2.5+Hibernate4.3.11+Struts1.3.8集成方案二
http://blog.csdn.net/yerenyuan_pku/article/details/52894958 前面我们已经集成了Spring4.2.5+Hibernate4.3.11+Str ...
- (转)Spring4.2.5+Hibernate4.3.11+Struts1.3.8集成方案一
http://blog.csdn.net/yerenyuan_pku/article/details/52888808 前面我们已经集成了Spring4.2.5+Hibernate4.3.11这两个框 ...
- Spring Data Jpa示例(IntelliJ maven项目)
1. 在IntelliJ中新建maven项目 给出一个建好的示例,(本示例中省略了业务逻辑组件UserService) 2. 在pom.xml中配置依赖 包括: spring-context spri ...
- struts2.3.24 + spring4.1.6 + hibernate4.3.11+ mysql5.5.25开发环境搭建及相关说明
一.目标 1.搭建传统的ssh开发环境,并成功运行(插入.查询) 2.了解c3p0连接池相关配置 3.了解验证hibernate的二级缓存,并验证 4.了解spring事物配置,并验证 5.了解spr ...
- FastDFS整合普通Maven项目(四)
1.下载官方的源代码:https://codeload.github.com/happyfish100/fastdfs-client-java/zip/master 2.采用maven命令编译成jar ...
随机推荐
- 第一百五十三节,封装库--JavaScript,表单验证--备注字数验证
封装库--JavaScript,表单验证--备注字数验证 效果图 html <div id="reg"> <h2 class="tuo"> ...
- List、Map、Set三个接口存储元素时各有什么特点?
List.Map.Set三个接口存储元素时各有什么特点? 解答: 1)List是有序的Collection,使用此接口能够精确的控制每个元素插入的位置.用户能够使用索引(元素在List中的位置,类似于 ...
- 【BZOJ】1661: [Usaco2006 Nov]Big Square 巨大正方形(暴力)
http://www.lydsy.com/JudgeOnline/problem.php?id=1661 暴力大法好... 枚举对角线(注意,一种对角线2种情况就行了,自己想...) 然后可以算出其它 ...
- java网络编程1-查询Internet地址
//经过dns查询后的结果会缓存起来,成功结果永久缓存,失败结果会缓存10s,通过下面的方法设置成功和失败的缓存时间 // 0为不缓存,-1为永不过期,其它单位为s Security.setPrope ...
- SR领域文献资源汇总(链接地址)
DRCN http://www.drcn.org/ The International Workshop on Design of Reliable Communication Networks ...
- 高性能javascript 文件加载阻塞
高性能javascript javascript脚本执行过程中会中断页面加载,直到脚本执行完毕,此操作阻塞了页面加载,造成性能问题. 脚本位置和加载顺序:如果将脚本放在head内,那么再脚本执行完 ...
- [USACO5.5]隐藏口令Hidden Password
题目链接:传送门 题目大意:给你一个长度 N 的字符串,5<=N<=5,000,000,将首尾合并成环,断环成链并满足字典序最小,输出此时首字母在原串中的位置-1: 题目思路:最小表示法 ...
- angularJS中的ng-show、ng-if指令
angularJS中的ng-show.ng-hide.ng-if指令都可以用来控制dom元素的显示或隐藏. 1. ng-show和ng-hide 根据所给表达式的值来显示或隐藏HTML元素.元素会渲染 ...
- Python全栈day26-27(面向对象进阶)
参考 http://www.cnblogs.com/linhaifeng/articles/6204014.html 1,什么是反射 反射的概念是由Smith在1982年首次提出的,主要是指程序可以访 ...
- vbs 修改Administrator帐号密码
Dim WshShell, oExec Set wshShell = CreateObject("WScript.Shell") Set objFSO = CreateObject ...