使用spring data jpa简化dao层编写

一、添加maven依赖:

...

			org.springframework.data
			spring-data-jpa
			
				
					org.slf4j
					jcl-over-slf4j
				
			
		
...


二、增加配置文件(applicationcontext.xml)



	
	

	

    
        
	
	
		
			
				none
				true
			
		
		
			
		
	
	
	
	
	
		 
	
	
	


三、编写域模型

package com.ldy.entities;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name="lsuser")
public class User implements Serializable {
	private String userID;
	private String roleID;
	private String userName;
	private String password;
	private String email;
	private String ceilPhone;
	private Date createTime;
	private String createUser;
	private Date modifyTime;
	private String modifyUser;
	@Id
	//@GeneratedValue(strategy = GenerationType.IDENTITY)
	@GeneratedValue(generator="custom_uuid")
	@GenericGenerator(name="custom_uuid",strategy="uuid2")
	public String getUserID() {
		return userID;
	}
	public void setUserID(String userID) {
		this.userID = userID;
	}
	public String getRoleID() {
		return roleID;
	}
	public void setRoleID(String roleID) {
		this.roleID = roleID;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getCeilPhone() {
		return ceilPhone;
	}
	public void setCeilPhone(String ceilPhone) {
		this.ceilPhone = ceilPhone;
	}
	public Date getCreateTime() {
		return createTime;
	}
	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}
	public String getCreateUser() {
		return createUser;
	}
	public void setCreateUser(String createUser) {
		this.createUser = createUser;
	}
	public Date getModifyTime() {
		return modifyTime;
	}
	public void setModifyTime(Date modifyTime) {
		this.modifyTime = modifyTime;
	}
	public String getModifyUser() {
		return modifyUser;
	}
	public void setModifyUser(String modifyUser) {
		this.modifyUser = modifyUser;
	}
	
}


四、声明仓库接口

package com.ldy.repositories;


import org.springframework.data.repository.CrudRepository;


import com.ldy.entities.User;
public interface UserRepositories extends CrudRepository{


}


五、编写测试

package com.ldy.springdata.runtime;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.inject.Inject;

import org.apache.commons.dbcp.BasicDataSource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.ldy.entities.User;
import com.ldy.repositories.UserRepositories;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/config/applicationContext.xml")
public class DataSourceTest {
	@Inject
    private ApplicationContext applicationContext;
	@Inject UserRepositories userRepo;
	@Test
	public void testUserRepositoies(){
		User user = new User();
		user.setCeilPhone("12222323421");
		user.setEmail("[email protected]");
		System.out.println("userRepo.save(user):"+userRepo.save(user));
	}
}




你可能感兴趣的:(spring框架)