maven工程依赖pom.xml:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.liuyun</groupId>
<artifactId>ssh</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>ssh Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<spring-version>4.2.4.RELEASE</spring-version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.5</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.11.2</version>
</dependency>
<!-- 数据库驱动和数据源 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.0.7.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>5.0.7.Final</version>
</dependency>
<!-- serlvet和jsp编译需要 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>ssh</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
spring的容器配置beans.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: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/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
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:db.properties" />
<!-- 扫描service,respositroy组件 -->
<context:component-scan base-package="com.liuyun"></context:component-scan>
<!-- 数据源 -->
<!-- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean> -->
<!-- sessionFactory (Spring接管) -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- <property name="dataSource" ref="dataSource" /> -->
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!--a. 事务管理类 -->
<bean id="trans" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
<!-- <property name="dataSource" ref="dataSource"></property> -->
</bean>
<!-- b. 定义事务规则 -->
<tx:advice id="txAdvice" transaction-manager="trans">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- c. 定义事务入口(那些类的哪些方法参与事务) -->
<aop:config>
<aop:pointcut expression="execution(* com.liuyun.service.impl.*.*(..))" id="pt" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt" />
</aop:config>
</beans>
Hibernate配置文件hibernate.cfig.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 数据源 -->
<property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<!-- mysql数据库配置 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">32147</property>
<!-- 个性化配置 -->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">false</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 把session和线程绑定,从而实现一个线程只有一个Session -->
<!-- <property name="hibernate.current_session_context_class">thread</property> -->
<!-- 映射文件 -->
<mapping class="com.liuyun.domain.User" />
<!-- <mapping resource="com/liuyun/domain/User.hbm.xml" /> -->
</session-factory>
</hibernate-configuration>
db.properties:
jdbc.user=root
jdbc.password=32147
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/hibernate?useSSL=false
jdbc.driverClass=com.mysql.jdbc.Driver
实体类user.java:
package com.liuyun.domain;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity()
@Table(name = "user")
public class User implements Serializable {
private static final long serialVersionUID = 7734464849457206326L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "name")
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + "]";
}
}
实体与数据库的映射文件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.liuyun.domain">
<class name="User" table="user">
<id name="id" column="id">
<generator class="native" />
</id>
<property name="name" column="name" />
</class>
</hibernate-mapping>
持久层UserDaoImpl:
package com.liuyun.dao.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;
import com.liuyun.dao.IUserDao;
import com.liuyun.domain.User;
@Repository("userDao")
public class UserDaoImpl implements IUserDao {
@Autowired
private HibernateTemplate hibernateTemplate;
@Override
public int save(User user) {
Long count = (Long) hibernateTemplate.save(user);
return count.intValue();
}
@Override
public void update(User user) {
hibernateTemplate.update(user);
}
@Override
public void deleteById(Long id) {
User user = new User();
user.setId(id);
hibernateTemplate.delete(user);
}
@SuppressWarnings("unchecked")
@Override
public List<User> findAll() {
List<User> users = (List<User>) hibernateTemplate.find("from User");
return users;
}
@Override
public User findById(Long id) {
User user = hibernateTemplate.get(User.class, id);
return user;
}
@SuppressWarnings("unchecked")
@Override
public User findByName(String name) {
List<User> users = (List<User>) hibernateTemplate.find("from User where name=?", name);
if (users == null || users.isEmpty()) {
return null;
} else {
return users.get(0);
}
}
}
业务层UserServiceImpl :
package com.liuyun.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.liuyun.dao.IUserDao;
import com.liuyun.domain.User;
import com.liuyun.service.IUserService;
@Service("userService")
public class UserServiceImpl implements IUserService {
@Autowired
IUserDao userDao;
@Override
public int save(User user) {
return userDao.save(user);
}
@Override
public void update(Long id, User user) {
user.setId(id);
userDao.update(user);
}
@Override
public void deleteById(Long id) {
userDao.deleteById(id);
}
@Override
public List<User> findAll() {
return userDao.findAll();
}
@Override
public User findById(Long id) {
return userDao.findById(id);
}
@Override
public User findByName(String name) {
return userDao.findByName(name);
}
}