配置pom.xml
<dependencies>
<!--Spring的Web的支持-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<!--Spring的MVC的支持-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<!--Spring的JDBC包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<!--Spring对于ORM的支持包(Spring和JPA的集成就在这个包中)-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<!--hibernate的核心包-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.8.Final</version>
</dependency>
<!--hibernate对于jpa的支持包-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.8.Final</version>
</dependency>
<!--mysql的驱动包(连数据库必需要加它)-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<!--引入dbcp连接池-->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>
<!--Spring的测试包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<!--要使用的切面包-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency>
<!--SpringMVC返回JSON的支持包-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.5</version>
</dependency>
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
搭建项目结构
Src
----- main
-------------java
--------------controller 页面的跳转
--------------dao 接口
-----------------impl 实现接口
注意:使用@PersistenceContext 注入EntityManager
--------------domain 属性 get/set toString
--------------service 接口 注意事务的配置
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)只读
---------------impl 实现
-------------resources 配置文件
-------------webapp 前台
------test
-------------java 测试
配置Jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///ssj?characterEncoding=UTF-8
jdbc.username=root
jdbc.password=root
配置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: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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!--Spring进行包的扫描-->
<context:component-scan base-package="cn.itsource.wy.ssj.dao"/>
<context:component-scan base-package="cn.itsource.wy.ssj.service"/>
<!--引入jdbc.properties 注意:必须写classpath-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--配置DataSource-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--配置包的扫描(Scan),认识JPA的注解 -->
<property name="packagesToScan" value="cn.itsource.wy.ssj.domain" />
<!--配置适配器 功能太多,必须指定有那个实现-->
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<!--方言(确定数据库)-->
<property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
<!--建表策略 -->
<property name="generateDdl" value="true" />
<!--支持SQL显示-->
<property name="showSql" value="true" />
</bean>
</property>
</bean>
<!--配置Spring对于JPA的事务支持-->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<!--支持事务的注解-->
<tx:annotation-driven transaction-manager="transactionManager" />
配置applicationContext-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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
">
<!--扫描Controller-->
<context:component-scan base-package="cn.itsource.wy.ssj.controller" />
<!--SpringMVC对注解支持-->
<mvc:default-servlet-handler />
<!--静态资源放行-->
<mvc:annotation-driven />
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="/WEB-INF/views/" />
<!--后缀-->
<property name="suffix" value=".jsp" />
</bean>
<!--文件上传-->
<!--<bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver">-->
<!--<property name="maxUploadSize">-->
<!--<value>${1024*1024*10}</value>-->
<!--</property>-->
<!--</bean>-->
</beans>
Domain----->Product
@Entity
public class Product {
@Id
@GeneratedValue
private Long id;
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 "Product{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
Dao----->IProductDao
public interface IProductDao {
void save(Product product);
void update(Product product);
void delete(Long id);
Product findOne(Long id);
List<Product> findAll();
}
Impl----->Iproducctimpl
@Repository
public class ProductDaoImpl implements IProductDao{
@PersistenceContext
private EntityManager entityManager;
@Override
public void save(Product product) {
entityManager.persist(product);
}
@Override
public void update(Product product) {
entityManager.merge(product);
}
@Override
public void delete(Long id) {
Product product=findOne(id);
if (product!=null){
entityManager.remove(product);
}
}
@Override
public Product findOne(Long id) {
return entityManager.find(Product.class, id);
}
@Override
public List<Product> findAll() {
String a="select o from Product o";
return entityManager.createQuery(a).getResultList();
}
}
Service----->IProductService
public interface IProductService {
void save(Product product);
void update(Product product);
void delete(Long id);
Product findOne(Long id);
List<Product> findAll();
}
Impl----->ProductServiceImpl
@Service
@Transactional
public class ProductServiceImpl implements IProductService {
@Autowired
private IProductDao productDao;
@Override
public void save(Product product) {
productDao.save(product);}
@Override
public void update(Product product) {
productDao.update(product);}
@Override
public void delete(Long id) {
productDao.delete(id);}
@Override
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public Product findOne(Long id) {
return productDao.findOne(id); }
@Override
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public List<Product> findAll() {
return productDao.findAll();
}
}
Controller------>ProductController
@Controller
@RequestMapping("/product")
public class ProductController {
@Autowired
private IProductService productService;
@RequestMapping("/index")
public String index(){
return "product";
}
@RequestMapping("/list")
@ResponseBody
public List<Product> list(){
return productService.findAll();
}
}