1、application.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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
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-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="com.hibernate "></context:component-scan>
<!-- 配置数据源 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置C3p0 -->
<bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
</bean>
<!-- 配置hibernate 的 session Factory 实例 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="datasource"></property>
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<property name="mappingLocations" value="classpath:com/hibernate/entities/*.hbm.xml"></property>
</bean>
<!-- 配置spring申明式事物 -->
<!-- 1、配置事物管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置事物属性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!-- 配置事物切点 -->
<aop:config>
<aop:pointcut expression="execution(* com.hibernate.service.*.*(..))" id="txPointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
</beans>
2、db.properties
jdbc.user=root
jdbc.password=123456
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://106.14.176.63:3306/yypao?useUnicode=true&characterEncoding=GBK
jdbc.initPoolSize=5
jdbc.maxPoolSize=10
3、hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">up</property>
</session-factory>
</hibernate-configuration>
**************************************************************************************************************************
java代码:
-----DAO------
~BookDao:
package com.hibernate.dao;
import com.hibernate.entities.Test;
public interface BookDao {
Test find(int id);
}
~BookDaoImpl:
package com.hibernate.dao;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.hibernate.entities.Test;
@Repository
public class BookDaoImpl implements BookDao{
@Autowired
private SessionFactory sessionFactory;
private Session getSession(){
return sessionFactory.getCurrentSession();
}
@Override
public Test find(int id) {
// TODO Auto-generated method stub
String hqlString = "select id,name,age,job from Test where id = ?";
Query query = getSession().createQuery(hqlString).setInteger(0, id);
List<Test> list = query.list();
System.out.println(list.get(0).getJob());
return null;
}
}
-------实体-------:
package com.hibernate.entities;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Test {
@Id
@GeneratedValue
private Integer id;
private String name;
private Integer age;
private String job;
public Integer getId() {
return id;
}
public String getName() {
return name;
}
public Integer getAge() {
return age;
}
public String getJob() {
return job;
}
public void setId(Integer id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
public void setJob(String job) {
this.job = job;
}
// @Override
// public String toString() {
// return "Test [id=" + id + ", name=" + name + ", age=" + age + ", job="
// + job + "]";
// }
}
-----hibernate表映射文件---------
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2017-10-10 22:43:38 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.hibernate.entities.Test" table="TEST">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<property name="age" type="java.lang.Integer">
<column name="AGE" />
</property>
<property name="job" type="java.lang.String">
<column name="JOB" />
</property>
</class>
</hibernate-mapping>
-------service-----
~BookService
package com.hibernate.service;
import com.hibernate.entities.Test;
public interface BookService {
Test geTest(int id);
}
~BookServiceImpl:
package com.hibernate.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hibernate.dao.BookDao;
import com.hibernate.entities.Test;
@Service
public class BookServiceImpl implements BookService{
@Autowired
private BookDao bookDao;
@Override
public Test geTest(int id) {
// TODO Auto-generated method stub
return (Test)this.bookDao.find(id);
}
}
---------测试-----
package com.hibernate.test;
import static org.junit.Assert.*;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.hibernate.service.BookService;
import com.mchange.v2.c3p0.DataSources;
public class JdbcTest {
private ApplicationContext ctx = null;
private BookService bookService = null;
{
ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
bookService = ctx.getBean(BookService.class);
}
/*@Test
public void testDataSource() throws SQLException {
DataSource dataSource = ctx.getBean(DataSource.class);
System.out.println(dataSource.getConnection());
}*/
@Test
public void getTestList(){
bookService.geTest(1);
}
}
---------日志打印结果------