, spring. , , , spring ...
. ApplicationContext , , .. , , spring XML- .
applicationContext.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:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:annotation-driven/>
<context:annotation-config/>
<context:component-scan base-package="com.farah"/>
<mvc:resources mapping="/resources/**" location="/resources/"/>
<import resource="spring-data.xml"/>
</beans>
, Spring-Data-JPA, spring, , .
spring -data.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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context.xsd">
<tx:annotation-driven transaction-manager="transactionManager"/>
<context:component-scan base-package="com.farah.repository.impl"/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
<property name="jpaProperties">
<map>
<entry key="hibernate.hbm2ddl.auto" value="create-drop"/>
<entry key="hibernate.show_sql" value="true"/>
</map>
</property>
<property name="packagesToScan" value="com.farah.domain"/>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/ub" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
</beans>
, spring Data JPA , . JpaRepository, CRUD. , , .
AccountRepository
public interface AccountRepository extends JpaRepository<Account, Long> {
}
JpaRepository
.
@Entity
@Table( name = "T_ACCOUNT" )
public class Account implements Serializable {
@Id
@GeneratedValue( strategy = GenerationType.AUTO ) private Long id;
@Column( name = "name" ) private String name;
...
}
@Inject .
ServiceFacade
AccountService
@Transactional
@Service
public class AccountService {
@Autowired
private AccountRepository repository;
public Boolean create( Account Account ) {
...
}
public Boolean update( Account Account ) {
...
}
public Boolean delete( Account Account ) {
...
}
}