1,spring配置:
<!-- 启用注解 -->
<context:annotation-config></context:annotation-config>
<!-- 注解扫描包 -->
<context:component-scan base-package="hyman.study" ></context:component-scan>
<!-- aop类 -->
<bean id="hymanInterceptor" class="hyman.study.interceptor.HymanInterceptor">
</bean>
<!-- aop配置 -->
<aop:config>
<aop:pointcut expression="execution(public * hyman.study..*(..))" id="myPointcut"/>
<aop:aspect id="myAspect1" ref="hymanInterceptor">
<aop:around method="around" pointcut-ref="myPointcut"/>
<aop:before method="before" pointcut-ref="myPointcut"/>
<aop:after method="after" pointcut="execution(public * hyman.study..*(..))"/>
</aop:aspect>
</aop:config>
<!-- 配置properties配置文件读取bean -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:database/jdbc.properties</value>
</list>
</property>
</bean>
<!-- 配置dataSource -->
<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.cache.use_second_level_cache">false</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>
</property>
<!-- 配置需要扫描的hibernate实体类所在包 -->
<property name="packagesToScan">
<list>
<value>hyman.study.model</value>
</list>
</property>
</bean>
其中,jdbc.properties 如下:
jdbc.driverClassName=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC
jdbc.username=root
jdbc.password=111111
2,User实体类:
@Entity
@Table(name="t_user")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
@Column(name="username")
private String username;
@Column(name="password")
private String password;
...
}
3,UserDao.java
@Repository("userDao")
public class UserDao {
@Autowired
private SessionFactory sessionFactory;
public void addUser(){
try {
Session session=sessionFactory.openSession();
Transaction tx=session.beginTransaction();
User user=new User();
user.setUsername("I love you");
user.setPassword("1314");
session.save(user);
tx.commit();
...
} catch (Exception e) {
e.printStackTrace();
}
}
}
4,测试
@Test
public void testAddUser(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring/applicationContext.xml");
UserDao userDao=(UserDao) applicationContext.getBean("userDao");
userDao.addUser();
}
5,pom.xml:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.12.Final</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.13</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>
<dependency>
<groupId>com.alexkasko.springjdbc</groupId>
<artifactId>springjdbc-iterable</artifactId>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
</dependencies>
</project>