1 什么是三大框架
1.1ssh:
struts spring hibernate
Struts2 Spring Hibernate
1.2ssm(最流行的框架):
SpringMVC Spring MyBatis
1.3ssj(Spring springMvc jpa):
Struts2 Spring JPA
SpringMVC Spring JPA(今天学习)
SpringMVC Spring spring Data JPA(spring的全家桶)
2 Spring集成JPA
Spring4 + SpringMVC+ jpa/hibernate4
建议:先完成Spring与jpa的集成
Pom.xml里的配置
<dependencies>
<!-- spring和springmvc整合包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<!-- springjdbc的包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<!-- Spring和ORM(JPA)整合的包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.8.Final</version>
</dependency>
<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>
<!-- 连接池-->
<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>
<!-- Springaop-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency>
<!-- 处理json的包-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
2.1写一个domain对象,配置JPA映射
@Entity
@Table(name = "t_product")
public class Product {
@Id
@GeneratedValue
private Long id;
private String name;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "dir_id")
//忽略延迟加载额外生成的handler这个属性
@JsonIgnoreProperties({"hibernateLazyInitializer","handler"})
private ProductDir dir;
public ProductDir getDir() {
return dir;
}
public void setDir(ProductDir dir) {
this.dir = dir;
}
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 + '\'' +
'}';
}
}
Spring的配置文件
Bean对象注入的顺序
jdbc.properties->dataSource->entityManagerFactory->dao->service->junit->action
jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///ssj
jdbc.username=root
jdbc.password=123456
配置连接池dataSource
<!-- 配置连接池dataSource bean生命周期方法 销毁方法close 用来连接之后 还给链接池-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<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>
配置entityManagerFactory
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<!-- 配置属性 setDataSource-->
<property name="dataSource" ref="dataSource"></property>
<!-- 扫描实体类的配置 entity-->
<property name="packagesToScan" value="cn.itsource.ssj.domain"></property>
<property name="jpaVendorAdapter" >
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<!-- 是否显示sql-->
<property name="showSql" value="true"></property>
<!-- 是否创建表-->
<property name="generateDdl" value="true"></property>
<!--数据库方言-->
<property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"></property>
</bean>
</property>
3 Spring集成SpringMVC
<?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.ssj.web.controller" />
<!--静态资源放行-->
<mvc:default-servlet-handler />
<!-- 扫描@RequestMapping-->
<mvc:annotation-driven />
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--监听器 Listener javaweb 可以监听四大作用生命周期 和 作用域属性操作
就可以读取的配置文件 contextConfigLocation 为key value applicationContext.xml
-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 核心控制器配置-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContnext-mvc.xml</param-value>
</init-param>
<!-- 优先加载servlet-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 配置过滤器 解决懒加载延迟关闭问题-->
<filter>
<filter-name>openEntityManagerInViewFilter</filter-name>
<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openEntityManagerInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--编码过滤器配置-->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
声明式事务管理(注解版本)
在spring的配置文件添加一点事务配置,并且在service层类上面添加一些注解,就可以实现事务管理
ProductServiceImpl
/**
* 事务:编程式事务 :通过程序写代码控制
* 声明式事务: 注解就可以支持 --现在要用
*
* 事务:一般是放到service层 dao层只专注于数据库操作
* 一个类里面 有事务方法多 还是没有事务方法多 (查询居多)
*/
@Service
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)//事务传播机制 如果下面的方法 有事务 ,就使用事务运行 如果没有事务 就用非事务运行
public class ProductServiceImpl implements IProductService {
@Autowired
private IProductDao productDao;
@Transactional //这事务就新创建事务
public void save(Product product) {
productDao.save(product);
}
@Transactional//事务方式运行
public void update(Product product) {
productDao.update(product);
}
@Transactional//事务方式运行
public void detele(Long id) {
productDao.detele(id);
}
public Product queryOne(Long id) {
return productDao.queryOne(id);
}
public List<Product> queryAll() {
return productDao.queryAll();
}
}
ProductController
@Controller
@RequestMapping("/product")
public class ProductController {
@Autowired
private IProductService productService;
//跳转页面
@RequestMapping("/index")
public String index(){
return "product";//返回到product.jsp
}
//返回json格式的数据
@RequestMapping("/list")
@ResponseBody//如果能控制返回json 只处理产品
public List<Product> list(){
System.out.println("-----------------------------------");
System.out.println(productService.queryAll());
return productService.queryAll();
}
}
4配置好,启动tomcat
product.jsp
<%--
Created by IntelliJ IDEA.
User: JAVA
Date: 2019/9/25
Time: 14:13
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<link rel="stylesheet" type="text/css" href="/easyui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="/easyui/themes/icon.css">
<script type="text/javascript" src="/easyui/jquery.min.js"></script>
<script type="text/javascript" src="/easyui/jquery.easyui.min.js"></script>
<script type="text/javascript" src="/easyui/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript">
//格式化列的方法
function objFormat(obj){
if(obj != null){
return obj.name;
}
}
</script>
</head>
<body>
<table id="productGrid" class="easyui-datagrid" style="width:400px;height:250px"
data-options="url:'/product/list',fitColumns:true,singleSelect:true,fit:true,toolbar:'#tt'">
<thead>
<tr>
<th data-options="field:'id',width:100">编码</th>
<th data-options="field:'name',width:100">名称</th>
<th data-options="field:'dir',width:100,formatter:objFormat">产品分类</th>
<%-- <th data-options="field:'productDir',width:100,align:'right',formatter:objFormat">产品</th>--%>
</tr>
</thead>
<div id="tt">
<a href="javascript:;" class="easyui-linkbutton" data-method="save">添加</a>
<a href="javascript:;" class="easyui-linkbutton" data-method="update" >修改</a>
<a href="javascript:;" class="easyui-linkbutton" data-method="delete">删除</a>
</div>
</table>
</body>
</html>