十二 关联映射操作-一对一的关联关系

需求:用户与角色的一对一的关联关系;

新建项目08-spring-data-jpa-onetoone

pom.xml


	4.0.0
	com.bjsxt
	08-spring-data-jpa-onetoone
	0.0.1-SNAPSHOT
	
		1.8
		4.3.23.RELEASE
		5.1.17.Final
		5.1.47
		0.9.2.1
		4.12
		1.18.6
		5.0.7.Final
		1.7.0.RELEASE
	

	
		
			org.springframework
			spring-core
			${spring.version}
		
		
			org.springframework
			spring-context
			${spring.version}
		
		
			org.springframework
			spring-aspects
			${spring.version}
		
		
			org.springframework
			spring-aop
			${spring.version}
		

		
			org.springframework
			spring-jdbc
			${spring.version}
		

		
			org.springframework
			spring-orm
			${spring.version}
		

		
			org.springframework
			spring-test
			${spring.version}
		

		
			org.hibernate
			hibernate-core
			${hibernate.version}
		

		
			mysql
			mysql-connector-java
			${mysql.version}
		

		
			com.mchange
			c3p0
			${c3p0.version}
		

		
			junit
			junit
			${junit.version}
			test
		

		
			org.projectlombok
			lombok
			${lombok.version}
			provided
		

		
			org.hibernate
			hibernate-entitymanager
			${hibernate-entitymanager.version}
		

		
			org.springframework.data
			spring-data-jpa
			${spring-data-jpa.version}
		

		
		
			log4j
			log4j
			1.2.17
		

	

jdbc.properties

jdbc.url=jdbc:mysql://localhost:3306/spring-data-test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT
#jdbc.url=jdbc:mysql://localhost:3306/spring-data-test 这个保存到数据库中会导致乱码
jdbc.driver.class=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.password=123456

applicationContext.xml




	
	

	
	
		
		
		
		
	

	
	
		
		
			
				
				
				
				
				
				
				
			
		
		
		
		
			
				com.bjsxt.pojo
			
		
	
	
	
	
		
	

	
	

	
	
	
	
	

1  编写实体类

package com.bjsxt.pojo;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import lombok.Data;

@Entity
@Table(name="ts_users")
@Data
public class Users implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Column(name="id")
	private Long id;
	
	@Column(name="user_name")
	private String userName;
	
	@Column(name="age")
	private Integer age;
	
	@OneToOne(cascade=CascadeType.PERSIST)
	// @JoinColumn就是维护一个外键
	@JoinColumn(name = "role_id")
	private Roles roles;

	@Override
	public String toString() {
		return "Users [id=" + id + ", userName=" + userName + ", age=" + age + "]";
	}
}
package com.bjsxt.pojo;

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

import lombok.Data;

/**
 * 
 * @author guozi
 *
 */
@Entity
@Table(name = "ts_roles")
@Data
public class Roles {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "role_id")
	private Long roleId;
	
	@Column(name = "role_name")
	private String roleName;
	
	// mappedBy:users中指定的roles属性
	@OneToOne(mappedBy = "roles")
	private Users users;

	@Override
	public String toString() {
		return "Roles [roleId=" + roleId + ", roleName=" + roleName + "]";
	}
}

2 创建dao接口

package com.bjsxt.dao;

import org.springframework.data.jpa.repository.JpaRepository;

import com.bjsxt.pojo.Users;

/**
 * JpaRepository接口使用
 * JpaRepository是开发中使用最多的接口
 * @author guozi
 *
 */
public interface UsersDao extends JpaRepository {
	
}

3 创建测试类

package com.bjsxt.dao;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.bjsxt.pojo.Roles;
import com.bjsxt.pojo.Users;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class OneToOneTest {
	
	@Autowired
	private UsersDao usersDao;

	/**
	 * 保存
	 * 	Hibernate: create table ts_roles (role_id bigint not null auto_increment, role_name varchar(255), primary key (role_id))
		Hibernate: alter table ts_users add column role_id bigint
		Hibernate: alter table ts_users add constraint FKldb9v9i5o9nlrd4pahgoajvsj foreign key (role_id) references ts_roles (role_id)
		Hibernate: insert into ts_roles (role_name) values (?)
		Hibernate: insert into ts_users (age, role_id, user_name) values (?, ?, ?)
	 */
	@Test
	public void test() {
		// 创建角色
		Roles role = new Roles();
		role.setRoleName("船长");
		
		// 创建用户
		Users user = new Users();
		user.setAge(20);
		user.setUserName("路飞");
		
		// 建立关系
		user.setRoles(role);
		role.setUsers(user);
		
		// 保存数据
		this.usersDao.save(user);
	}

	/**
	 * 查询
	 * 	Hibernate: select users0_.id as id1_1_0_, users0_.age as age2_1_0_, users0_.role_id as role_id4_1_0_, users0_.user_name as user_nam3_1_0_, roles1_.role_id as role_id1_0_1_, roles1_.role_name as role_nam2_0_1_ from ts_users users0_ left outer join ts_roles roles1_ on users0_.role_id=roles1_.role_id where users0_.id=?
		Users [id=14, userName=路飞, age=20]
		Roles [roleId=1, roleName=船长]
	 */
	@Test
	public void test2 () {
		Users users = this.usersDao.findOne(14l);
		System.out.println(users);
		System.out.println(users.getRoles());
	}
}

 

 

你可能感兴趣的:(Spring,Data)