一。一个添加用户的小例子
使用了spring的annotation自动装配技术将数据源装配到ServiceImpl中。
需要引入:commons-pool.jar commons-dbcp.jar 还有mysql的驱动包mysql-connector-java-5.1.22-bin.jar
service.java
package bean;
public interface Service {
public void service();
public void save(User user);
}
ServiceImpl.java
package bean;
import java.lang.reflect.Proxy;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ServiceTest {
@Test
public void proxy() {
BeanFactory factory = new ClassPathXmlApplicationContext(
"applicationContext.xml");
Service s = (Service) factory.getBean("serviceImpl");
s.save(null);
}
}
Interceptor.java
package interceptor;
import org.springframework.stereotype.Component;
@Component
public class Interceptor {
public void before() {
System.out.println("服务即将开始");
}
}
applicationContext.xml
<?xml version="1.0" encoding="GBK"?>
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<context:component-scan base-package="bean" /> <!-- bean包里面标记@component的类都会自动生成同名bean -->
<bean name="interceptor" class="interceptor.Interceptor" />
<!-- 定义数据库 -->
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!-- results in a setDriverClassName(String) call -->
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/spring"/>
<property name="username" value="root"/>
<property name="password" value="1234"/>
</bean>
</beans>
ServiceTest.java
package bean;
import java.lang.reflect.Proxy;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ServiceTest {
@Test
public void proxy() {
BeanFactory factory = new ClassPathXmlApplicationContext(
"applicationContext.xml");
Service s = (Service) factory.getBean("serviceImpl");
s.save(null);
}
}
二。加入hibernate
以一个保存用户的save过程为例
ServiceImpl
package bean;
import java.sql.Connection;
import java.sql.SQLException;
import javax.annotation.Resource;
import javax.jms.Session;
import javax.sql.DataSource;
import model.User;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;
@Component
public class ServiceImpl implements Service {
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
@Resource(name="sessionFactory")
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public void save(User user) {
try {
org.hibernate.Session s= sessionFactory.openSession();
s.beginTransaction();
s.save(user);
s.getTransaction().commit();
s.close();
System.out.println("user saved!");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 业务方法
public void service() {
// TODO Auto-generated method stub
System.out.println("i'm servicing");
}
}
User
package model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.springframework.stereotype.Component;
@Entity//指明这是实体类
@Table(name="user")//对应数据库里的user表
public class User {
private int id;
private String name = null;
private String password = null;
@Id//id 自动生成
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
applicationContext.xml
<?xml version="1.0" encoding="GBK"?>
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<context:component-scan base-package="bean" />
<!-- 定义数据源 -->
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<!-- results in a setDriverClassName(String) call -->
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/spring" />
<property name="username" value="root" />
<property name="password" value="1234" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<!-- 注意 这里注入实体类 -->
<property name="annotatedClasses">
<list>
<value>model.User</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop><!-- 方言 -->
<prop key="hibernate.show_sql">true</prop><!-- 是否显示sql语句 -->
</props>
</property>
</bean>
</beans>
技巧:
<property name="annotatedClasses">
<list>
<value>model.User</value>
</list>
</property>
这里我们每个实体类都得手动加进去,可以使用AnnotationSessionFactoryBean的setpackagesToScan方法,改成:
<property name="packagesToScan">
<list>
<value>model</value>
</list>
</property>
这样的话就可以自动扫描model包下的所有实体类了,以后添加新的实体类也不用修改xml
ServiceTest
package test;
import static org.junit.Assert.*;
import model.User;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import bean.Service;
public class ServiceTest {
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void testSave() {
BeanFactory factory = new ClassPathXmlApplicationContext(
"applicationContext.xml");
Service s = (Service) factory.getBean("serviceImpl");
User user = new User();
user.setId(1);
user.setName("hibernate");
user.setPassword("hi");
s.save(user);
}
}