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);
}
}