SSJ整合(Spring Struts2 JPA )

父项目 pom.xml导入项目依赖


  4.0.0
  cn.itcast
  commom_parent
  0.0.1-SNAPSHOT
  pom

  
	
		4.2.4.RELEASE
		2.3.24
		5.0.7.Final
		1.6.6
		1.4.1.RELEASE
		5.2.0
		1.2.2
		1.10.4.RELEASE
		2.6.2
		3.11
		0.9.1.2
		3.0.1
		2.5
		4.11
	
	
	
			commons-collections
			commons-collections
			3.2
		
		
			org.codehaus.groovy
			groovy-all
			2.2.0
		
		
			net.sf.jasperreports
			jasperreports
			5.2.0
			
				
					com.lowagie
					itext
				
			
		
		
			com.lowagie
			itext
			2.1.7
		
		
		
			org.apache.xbean
			xbean-spring
			4.5
		
		
			org.quartz-scheduler
			quartz
			2.2.1
		
		
			org.quartz-scheduler
			quartz-jobs
			2.2.1
		
		
			net.sf.ehcache
			ehcache-core
			2.6.11
		
		
		
			javax.mail
			mail
			1.4
		
		
			com.belerweb
			pinyin4j
			2.5.0
		
		
		
			net.sf.json-lib
			json-lib
			2.4
			jdk15
		
		
		
			org.apache.shiro
			shiro-all
			${shiro.version}
		

		
		
			org.springframework
			spring-context
			${spring.version}
		
		
			org.springframework
			spring-context-support
			${spring.version}
		
		
			org.springframework
			spring-web
			${spring.version}
		
		
			org.springframework
			spring-orm
			${spring.version}
		
		
			org.springframework
			spring-jms
			${spring.version}
		
		
			org.springframework
			spring-test
			${spring.version}
		
		
			org.springframework
			spring-aspects
			${spring.version}
		
		
		
			org.springframework.data
			spring-data-jpa
			${springdatajpa.version}
		
		
		
			org.apache.activemq
			activemq-all
			${activemq.version}
		
		
		
			org.apache.struts
			struts2-core
			${struts2.version}
		
		
			org.apache.struts
			struts2-spring-plugin
			${struts2.version}
		
		
			org.apache.struts
			struts2-convention-plugin
			${struts2.version}
		
		
		
			org.hibernate
			hibernate-core
			${hibernate.version}
		
		
			org.hibernate
			hibernate-entitymanager
			${hibernate.version}
		

		
		
			c3p0
			c3p0
			${c3p0.version}
		

		
		
			org.slf4j
			slf4j-log4j12
			${slf4j.version}
		

		
		
			commons-lang
			commons-lang
			2.6
		

		
		
			redis.clients
			jedis
			${jedis.version}
		

		
			org.springframework.data
			spring-data-redis
			${springdataredis.version}
		

		
		
		
			com.oracle
			ojdbc14
			10.2.0.1.0
		

		
		
			org.apache.poi
			poi
			${poi.version}
		
		
			org.apache.poi
			poi-ooxml
			${poi.version}
		
		
			org.apache.poi
			poi-ooxml-schemas
			${poi.version}
		

		
		
			javax.servlet
			servlet-api
			${servlet.version}
			provided
		

		
		
			junit
			junit
			${junit.version}
			test
		

		
		
			org.apache.cxf
			cxf-rt-frontend-jaxws
			${cxf.version}
		

		
			org.apache.cxf
			cxf-rt-transports-http
			${cxf.version}
		

		
			org.apache.cxf
			cxf-rt-frontend-jaxrs
			${cxf.version}
		

		
			org.apache.cxf
			cxf-rt-rs-client
			${cxf.version}
		

		
			org.apache.cxf
			cxf-rt-rs-extension-providers
			${cxf.version}
		

		
			org.codehaus.jettison
			jettison
			1.3.7
		

		
		
			com.alibaba
			fastjson
			1.1.37
		

		
			com.colobu
			fastjson-jaxrs-json-provider
			0.3.1
		

	

	
		
		
			
				
					org.apache.maven.plugins
					maven-compiler-plugin
					3.2
					
						1.7
						1.7
						utf8
						true
					
				
			
		
	
  
  


web.xml配置spring,struts2



  bos_management_web
  
    index.html
    index.htm
    index.jsp
    default.html
    default.htm
    default.jsp
  
  
   
  
  	contextConfigLocation
  	classpath:applicationContext.xml
  
  
  
  	org.springframework.web.context.ContextLoaderListener
  
  
  
  	struts2
  	org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  
  
  	struts2
  	/*
  

配置主配置文件 applicationContext.xml  




	
	
		
		
		
		
	
	
	
	
		
		
		
		
			
		
		
			
				
				
				
				
				
			
		
		
			
		
	
	
	
	
		
	
	
	
	
	
	
	
	
	
 	 
	

Spring整合JPA


	
		
		
		
		
			
		
		
			
				
				
				
				
				
			
		
		
			
		
	
	


配置各层依赖关系 web层依赖service层,各层依此类推

  
  
  	
  	cn.huitong.bos
    bos_management_service
    0.0.1-SNAPSHOT
  	
  

JPA基本注解开发

@Entity  标明是实体类

@Table(name = "T_SUB_ARCHIVE")    映射表名称

@Id 主键标识
@GeneratedValue    自动生成主键的值
@Column(name = "C_ID")    设置映射数据表中列的信息

private Integer id; // 主键

@Column(name = "C_OPERATING_TIME")

@Temporal(TemporalType.TIMESTAMP)   控制数据表保存日期哪个部分

@OneToMany     (mappedby 相当于 inverse)@ManyToOne  结合@JoinColumn添加一列外键

@@ManyToMany   一方写mappedby放弃外键维护,另一方@JoinTable维护中间表



整合 Spring Data JPA


 	 

持久层框架  Spring Data JPA

Spring Data JPA基于Hibernate开发的一个JPA框架 ,极大简化Dao层代码

Spring Data                Spring规范,统一持久层框架

Spring Data JPA         Spring Data 整合JPA

Spring Data,     Spring Data JPA    父子关系


SSJ整合(Spring Struts2 JPA )_第1张图片

Spring Data JPA创建查询的方法:

方法名解析查询 findBy、readBy、getBy。。。

Query查询:可以绑定JPQL或SQL语句


JPA常用接口:

JpaRepository:扩展增删改查、批量操作

JpaSpecificationExecutor:用来做复杂查询的


配置好环境 ,JPA持久化Dao层

/*
 * T代表操作的泛型类
 * ID 代表实体类主键类型
 */
public interface StandardDao extends JpaRepository {

	
}

测试

方法名解析查询

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class StandardTest {
	
	@Resource    //注入StandardDao
	private StandardDao standardDao;
	
	
	
	//方法名解析查询findlByName
	@Test
	public void test2(){
		List list = standardDao.findByName("10~20公斤");
		for(Standard s:list){
			System.out.println(s.getName());
		}
	}
}

Query查询

//Query注解查询          
		@Test
		public void test4(){
			List list = standardDao.getByNameLike("%20%");
			for(Standard s:list){
				System.out.println(s.getName());
			}
		}

Dao持久层

public interface StandardDao extends JpaRepository {

	
	List findByName(String string);

	List findByNameLike(String string);
	
	@Query("from Standard where name like ?1")//1代表参数1赋值给name  复杂查询 JPQL  相当于HQL
	List getByNameLike(String string);
}

注解开发流程

Action

//struts2注解
@ParentPackage("struts-default")
@Namespace("/")
@Actions
//spring注解
@Controller
@Scope("prototype")
public class StandardAction extends ActionSupport implements ModelDriven {

	private Standard model=new Standard();
	@Override
	public Standard getModel() {
		// TODO Auto-generated method stub
		return model;
	}
	
@Autowired
private IStandardService standardService;

//添加保存

@Action(value="standardAction_add",results={@Result(name="add",location="/pages/base/standard.jsp")})
public String add(){
	standardService.add(model);
	return "add";
	
}
Service层
@Service
public class StandardServiceImpl implements IStandardService {
	
	@Autowired
	private StandardDao standardDao;

	@Override
	public void add(Standard model) {
		// TODO Auto-generated method stub
		standardDao.save(model);
	}

Dao层

public interface StandardDao extends JpaRepository {

}



































































你可能感兴趣的:(SSJ整合(Spring Struts2 JPA ))