1. 在IntelliJ中新建maven项目

给出一个建好的示例

2. 在pom.xml中配置依赖

  1. 包括:
  2. spring-context
  3. spring-orm
  4. hibernate-core
  5. mysql
  6. commons-dbcp

3. resources右键new一个Xml Configuration File--Spring Config配置文件:spring-config.xml(或者applicationContext.xml)

    配置dataSource、sessionFactory及transactionManager。

    可能会提示一个Application Context的什么配置,按提示操作即可;或者在IntelliJ工具的Project Structure下的Facets中进行spring的配置。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. 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/context http://www.springframework.org/schema/context/spring-context.xsd">
  6.  
  7. <!--自动扫描指定包及其子包下的所有Bean类-->
  8. <context:component-scan base-package="com.test.app.dao.impl"/>
  9.  
  10. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  11. <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  12. <property name="url" value="jdbc:mysql://localhost:3306/javaee"/>
  13. <property name="username" value="root"/>
  14. <property name="password" value="root"/>
  15. </bean>
  16.  
  17. <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
  18. <property name="dataSource" ref="dataSource"/>
  19. <property name="annotatedClasses">
  20. <list>
  21. <value>com.test.app.domain.User</value>
  22. </list>
  23. </property>
  24. <property name="hibernateProperties">
  25. <value>
  26. hibernate.dialect=org.hibernate.dialect.MySQLDialect
  27. hibernate.hbm2ddl.auto=update
  28. hibernate.show_sql=true
  29. <!--hibernate.format_sql=true-->
  30. </value>
  31. </property>
  32. </bean>
  33.  
  34. <!--配置hibernate事务管理器-->
  35. <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
  36. <property name="sessionFactory" ref="sessionFactory"/>
  37. </bean>
  38. <!--根据Annotation来生成事务代理-->
  39. <tx:annotation-driven transaction-manager="transactionManager"/>
  40. </beans>

4. 新建User实体类,并进行Annotation注解

  1. @Entity //把当前bean设置成实体对象
  2. @Table(name = "user") //设置数据库的表名,默认为类名首字母小写
  3. public class User {
  4. @Id
  5. @GeneratedValue(strategy=GenerationType.IDENTITY) //id生成策略:自增
  6. @Column(name = "user_id")
  7. private int id;
  8. @Column
  9. private String name;
  10. @Column
  11. private int age;
  12. //省略setter和getter方法
  13. }

5. 新建UserDao接口及其实现类UserDaoImpl,并进行Annotation注解

  1. public interface UserDao {
  2. Integer insert(User user);
  3. void delete(User user);
  4. void update(User user);
  5. User find(int id);
  6. }

UserDaoImpl实现类代码如下:

  1. //指定该类作为Spring Bean,实例名为userDao。默认为Bean类的首字母小写
  2. @Repository("userDao") //标注一个DAO组建类
  3. @Transactional
  4. public class UserDaoImpl implements UserDao {
  5. @Autowired //自动装配 或者用@Resource(name = "sessionFactory")配置依赖
  6. private SessionFactory sessionFactory;
  7.  
  8. @Override
  9. public Integer insert(User user) {
  10. return (Integer) sessionFactory.getCurrentSession().save(user);
  11. }
  12.  
  13. @Override
  14. public void delete(User user) {
  15. sessionFactory.getCurrentSession().delete(user);
  16. }
  17.  
  18. @Override
  19. public void update(User user) {
  20. sessionFactory.getCurrentSession().update(user);
  21. }
  22.  
  23. @Override
  24. public User find(int id) {
  25. return (User) sessionFactory.getCurrentSession().get(User.class, id);
  26. }
  27. }

使用@Repository注解需要在Spring的配置文件中指定搜索路径,如下:

  1. <context:component-scan base-package="com.test.app.dao.impl"/>

使用@Transactional注解需要在Spring配置文件中增加如下配置片段

  1. <!--配置hibernate事务管理器-->
  2. <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
  3. <property name="sessionFactory" ref="sessionFactory"/>
  4. </bean>
  5. <!--根据Annotation来生成事务代理-->
  6. <tx:annotation-driven transaction-manager="transactionManager"/>

6. 新建测试类MainTest

  1. public class MainTest {
  2. public static void main(String[] args) {
  3. ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml");
  4. UserDao userDao = (UserDao) applicationContext.getBean("userDao");
  5.  
  6. User user = new User();
  7. user.setName("zhangsan");
  8. user.setAge(23);
  9.  
  10. userDao.insert(user);
  11. }
  12. }

在此,推荐使用junit测试类进行测试,下面进行简要介绍

1. 添加依赖

在pom.xml中添加junit和spring-test依赖,如下:

  1. <dependency>
  2. <groupId>junit</groupId>
  3. <artifactId>junit</artifactId>
  4. <version>4.12</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework</groupId>
  8. <artifactId>spring-test</artifactId>
  9. <version>4.2.3.RELEASE</version>
  10. </dependency>

2. 创建测试类

UserDaoImpl——Alt+Enter——Create Test,将自动新建一个UserDapImplTest测试类

3. 配置测试类

在类名上方添加如下内容,用于配置Spring配置文件的位置

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @ContextConfiguration(locations = "classpath:spring-config.xml")

4. 执行测试类

类名:右键——Run,执行所有方法

方法名:右键——Run,执行当前方法

5. 示例代码

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @ContextConfiguration(locations = "classpath:spring-config.xml")
  3. public class UserDaoImplTest {
  4. @Resource
  5. private UserDao userDao;
  6.  
  7. @Test
  8. public void testInsert() throws Exception {
  9. //省略...
  10. }
  11.  
  12. @Test
  13. public void testDelete() throws Exception {
  14. //省略...
  15. }
  16.  
  17. @Test
  18. public void testUpdate() throws Exception {
  19. User user = userDao.find(2);
  20. user.setAge(20);
  21. userDao.update(user);
  22. }
  23.  
  24. @Test
  25. public void testFind() throws Exception {
  26. User user = userDao.find(2);
  27. System.out.println(user.getName());
  28. }
  29. }

Spring4.2.3+Hibernate4.3.11整合( IntelliJ maven项目)(使用Annotation注解)(Junit测试类)的更多相关文章

  1. Spring4.2.3+Hibernate4.3.11整合( IntelliJ maven项目)

    1. 在IntelliJ中新建maven项目 给出一个建好的示例 2. 在pom.xml中配置依赖 包括: spring-context spring-orm hibernate-core mysql ...

  2. (转)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 ...

  3. (转)Spring4.2.5+Hibernate4.3.11组合开发

    http://blog.csdn.net/yerenyuan_pku/article/details/52887573 搭建和配置Spring与Hibernate整合的环境 今天我们来学习Spring ...

  4. 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 ...

  5. (转)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 ...

  6. (转)Spring4.2.5+Hibernate4.3.11+Struts1.3.8集成方案一

    http://blog.csdn.net/yerenyuan_pku/article/details/52888808 前面我们已经集成了Spring4.2.5+Hibernate4.3.11这两个框 ...

  7. Spring Data Jpa示例(IntelliJ maven项目)

    1. 在IntelliJ中新建maven项目 给出一个建好的示例,(本示例中省略了业务逻辑组件UserService) 2. 在pom.xml中配置依赖 包括: spring-context spri ...

  8. 高并发秒杀系统--junit测试类与SpringIoc容器的整合

    1.原理是在Junit启动时加载SpringIoC容器 2.SpringIoC容器要根据Spring的配置文件加载 [示例代码] package org.azcode.dao; import org. ...

  9. struts2.3.24 + spring4.1.6 + hibernate4.3.11+ mysql5.5.25开发环境搭建及相关说明

    一.目标 1.搭建传统的ssh开发环境,并成功运行(插入.查询) 2.了解c3p0连接池相关配置 3.了解验证hibernate的二级缓存,并验证 4.了解spring事物配置,并验证 5.了解spr ...

随机推荐

  1. EasyUI DataGrid可编辑单元格

    效果如图: 首先在需要可编辑的列上添加一个editor属性,列定义为numberbox编辑类型 <th field="SCORES" editor="{type:' ...

  2. 开启GitHub模式,now!

    (原文地址为:http://www.karottc.com/blog/2014/06/15/current-doing/) 最近看到了一篇文章,该文章的作者将自己连续177天在github上commi ...

  3. CentOS7.1 安装Liberty之环境准备(1)

    一.基础平台 1.一台装有VMware的windows系统(可联网) 2.CentOS 7.1 64bit镜像 二.最小化安装两台CentOS 7.1 的虚拟机controller.compute1, ...

  4. C语言中文网

    网址:http://c.biancheng.net/cpp/ 涵盖如下:

  5. 【微信小程序】支付过程详解

    一.介绍 今天跟大家分享微信小程序集成支付. 二.分析 1.小程序支付API 地址:https://mp.weixin.qq.com/debug/wxadoc/dev/api/api-pay.html ...

  6. CFontDialog学习

    void CMfcFontDlgDlg::OnBtnFont() { // Show the font dialog with all the default settings. CFontDialo ...

  7. 【LeetCode】Copy List with Random Pointer

    A linked list is given such that each node contains an additional random pointer which could point t ...

  8. Linux各文件颜色的含义

    Linux系统中文件有多种颜色,不同颜色文件代表不同类型的文件,具体如下: 蓝色:目录 绿色:可执行文件 红色:压缩文件 浅蓝色:链接文件 白色:普通文件 黄色:设备文件

  9. python 之 多线程

    一.多线程(具体可参照博文多进程---->http://www.cnblogs.com/work115/p/5621789.html) 1.函数式实现多线程 2.类实现多线程 3.多线程之线程锁 ...

  10. HDU 3695 / POJ 3987 Computer Virus on Planet Pandora

      Computer Virus on Planet Pandora Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1353 ...