Spring4.2.3+Hibernate4.3.11整合( IntelliJ maven项目)(使用Annotation注解)(Junit测试类)
1. 在IntelliJ中新建maven项目
给出一个建好的示例
2. 在pom.xml中配置依赖
- 包括:
- spring-context
- spring-orm
- hibernate-core
- mysql
- 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的配置。
- <?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:context="http://www.springframework.org/schema/context"
- 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">
- <!--自动扫描指定包及其子包下的所有Bean类-->
- <context:component-scan base-package="com.test.app.dao.impl"/>
- <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="annotatedClasses">
- <list>
- <value>com.test.app.domain.User</value>
- </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>
- <!--配置hibernate事务管理器-->
- <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
- <property name="sessionFactory" ref="sessionFactory"/>
- </bean>
- <!--根据Annotation来生成事务代理-->
- <tx:annotation-driven transaction-manager="transactionManager"/>
- </beans>
4. 新建User实体类,并进行Annotation注解
- @Entity //把当前bean设置成实体对象
- @Table(name = "user") //设置数据库的表名,默认为类名首字母小写
- public class User {
- @Id
- @GeneratedValue(strategy=GenerationType.IDENTITY) //id生成策略:自增
- @Column(name = "user_id")
- private int id;
- @Column
- private String name;
- @Column
- private int age;
- //省略setter和getter方法
- }
5. 新建UserDao接口及其实现类UserDaoImpl,并进行Annotation注解
- public interface UserDao {
- Integer insert(User user);
- void delete(User user);
- void update(User user);
- User find(int id);
- }
UserDaoImpl实现类代码如下:
- //指定该类作为Spring Bean,实例名为userDao。默认为Bean类的首字母小写
- @Repository("userDao") //标注一个DAO组建类
- @Transactional
- public class UserDaoImpl implements UserDao {
- @Autowired //自动装配 或者用@Resource(name = "sessionFactory")配置依赖
- private 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);
- }
- }
使用@Repository注解需要在Spring的配置文件中指定搜索路径,如下:
- <context:component-scan base-package="com.test.app.dao.impl"/>
使用@Transactional注解需要在Spring配置文件中增加如下配置片段
- <!--配置hibernate事务管理器-->
- <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
- <property name="sessionFactory" ref="sessionFactory"/>
- </bean>
- <!--根据Annotation来生成事务代理-->
- <tx:annotation-driven transaction-manager="transactionManager"/>
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("zhangsan");
- user.setAge(23);
- userDao.insert(user);
- }
- }
在此,推荐使用junit测试类进行测试,下面进行简要介绍
1. 添加依赖
在pom.xml中添加junit和spring-test依赖,如下:
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.12</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-test</artifactId>
- <version>4.2.3.RELEASE</version>
- </dependency>
2. 创建测试类
UserDaoImpl——Alt+Enter——Create Test,将自动新建一个UserDapImplTest测试类
3. 配置测试类
在类名上方添加如下内容,用于配置Spring配置文件的位置
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations = "classpath:spring-config.xml")
4. 执行测试类
类名:右键——Run,执行所有方法
方法名:右键——Run,执行当前方法
5. 示例代码
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations = "classpath:spring-config.xml")
- public class UserDaoImplTest {
- @Resource
- private UserDao userDao;
- @Test
- public void testInsert() throws Exception {
- //省略...
- }
- @Test
- public void testDelete() throws Exception {
- //省略...
- }
- @Test
- public void testUpdate() throws Exception {
- User user = userDao.find(2);
- user.setAge(20);
- userDao.update(user);
- }
- @Test
- public void testFind() throws Exception {
- User user = userDao.find(2);
- System.out.println(user.getName());
- }
- }
Spring4.2.3+Hibernate4.3.11整合( IntelliJ maven项目)(使用Annotation注解)(Junit测试类)的更多相关文章
- Spring4.2.3+Hibernate4.3.11整合( IntelliJ maven项目)
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 ...
- 高并发秒杀系统--junit测试类与SpringIoc容器的整合
1.原理是在Junit启动时加载SpringIoC容器 2.SpringIoC容器要根据Spring的配置文件加载 [示例代码] package org.azcode.dao; import org. ...
- struts2.3.24 + spring4.1.6 + hibernate4.3.11+ mysql5.5.25开发环境搭建及相关说明
一.目标 1.搭建传统的ssh开发环境,并成功运行(插入.查询) 2.了解c3p0连接池相关配置 3.了解验证hibernate的二级缓存,并验证 4.了解spring事物配置,并验证 5.了解spr ...
随机推荐
- EasyUI DataGrid可编辑单元格
效果如图: 首先在需要可编辑的列上添加一个editor属性,列定义为numberbox编辑类型 <th field="SCORES" editor="{type:' ...
- 开启GitHub模式,now!
(原文地址为:http://www.karottc.com/blog/2014/06/15/current-doing/) 最近看到了一篇文章,该文章的作者将自己连续177天在github上commi ...
- CentOS7.1 安装Liberty之环境准备(1)
一.基础平台 1.一台装有VMware的windows系统(可联网) 2.CentOS 7.1 64bit镜像 二.最小化安装两台CentOS 7.1 的虚拟机controller.compute1, ...
- C语言中文网
网址:http://c.biancheng.net/cpp/ 涵盖如下:
- 【微信小程序】支付过程详解
一.介绍 今天跟大家分享微信小程序集成支付. 二.分析 1.小程序支付API 地址:https://mp.weixin.qq.com/debug/wxadoc/dev/api/api-pay.html ...
- CFontDialog学习
void CMfcFontDlgDlg::OnBtnFont() { // Show the font dialog with all the default settings. CFontDialo ...
- 【LeetCode】Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point t ...
- Linux各文件颜色的含义
Linux系统中文件有多种颜色,不同颜色文件代表不同类型的文件,具体如下: 蓝色:目录 绿色:可执行文件 红色:压缩文件 浅蓝色:链接文件 白色:普通文件 黄色:设备文件
- python 之 多线程
一.多线程(具体可参照博文多进程---->http://www.cnblogs.com/work115/p/5621789.html) 1.函数式实现多线程 2.类实现多线程 3.多线程之线程锁 ...
- HDU 3695 / POJ 3987 Computer Virus on Planet Pandora
Computer Virus on Planet Pandora Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1353 ...