环境搭建
源码地址:gitee:https://gitee.com/ytfs-dtx/JPA
导入依赖
<properties> <spring.version>5.2.5.RELEASEspring.version> <hibernate.version>5.4.10.Finalhibernate.version> <slf4j.version>1.7.30slf4j.version> <log4j.version>2.12.1log4j.version> <druid.version>1.1.21druid.version> <mysql.version>5.1.6mysql.version> properties> <dependencies> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-aopartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.aspectjgroupId> <artifactId>aspectjweaverartifactId> <version>1.9.5version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-contextartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-context-supportartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-testartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-ormartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-beansartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-coreartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.hibernategroupId> <artifactId>hibernate-entitymanagerartifactId> <version>${hibernate.version}version> dependency> <dependency> <groupId>org.hibernategroupId> <artifactId>hibernate-coreartifactId> <version>${hibernate.version}version> dependency> <dependency> <groupId>org.hibernate.validatorgroupId> <artifactId>hibernate-validatorartifactId> <version>6.1.2.Finalversion> <exclusions> <exclusion> <artifactId>classmateartifactId> <groupId>com.fasterxmlgroupId> exclusion> exclusions> dependency> <dependency> <groupId>mysqlgroupId> <artifactId>mysql-connector-javaartifactId> <version>${mysql.version}version> dependency> <dependency> <groupId>com.alibabagroupId> <artifactId>druidartifactId> <version>${druid.version}version> dependency> <dependency> <groupId>org.springframework.datagroupId> <artifactId>spring-data-jpaartifactId> <version>2.2.6.RELEASEversion> <exclusions> <exclusion> <artifactId>slf4j-apiartifactId> <groupId>org.slf4jgroupId> exclusion> exclusions> dependency> <dependency> <groupId>javax.elgroupId> <artifactId>javax.el-apiartifactId> <version>3.0.0version> dependency> <dependency> <groupId>org.glassfishgroupId> <artifactId>javax.elartifactId> <version>3.0.0version> dependency> <dependency> <groupId>org.slf4jgroupId> <artifactId>slf4j-apiartifactId> <version>${slf4j.version}version> dependency> <dependency> <groupId>org.apache.logging.log4jgroupId> <artifactId>log4j-apiartifactId> <version>${log4j.version}version> dependency> <dependency> <groupId>junitgroupId> <artifactId>junitartifactId> <version>4.12version> dependency> dependencies>
创建配置文件
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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"> <bean id="entityManagerFactoryBean" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="packagesToScan" value="xyz.ytfs.entity"/> <property name="persistenceProvider"> <bean class="org.hibernate.jpa.HibernatePersistenceProvider"/> property> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="database" value="MYSQL"/> <property name="generateDdl" value="false"/> <property name="showSql" value="true"/> <property name="databasePlatform" value="org.hibernate.dialect.MySQL55Dialect"/> bean> property> <property name="jpaDialect"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/> property> <property name="jpaProperties"> <props> <prop key="hibernate.hbm2ddl.auto">updateprop> props> property> bean> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactoryBean"/> bean> <jpa:repositories base-package="xyz.ytfs.dao" transaction-manager-ref="transactionManager" entity-manager-factory-ref="entityManagerFactoryBean"/> <context:property-placeholder location="classpath:jdbcConfig.properties"/> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> bean> beans>
创建实体类
注解说明
@ManyToMany
作用:用于映射多对多关系
属性:
cascade:配置级联操作。
fetch:配置是否采用延迟加载。
targetEntity:配置目标的实体类。映射多对多的时候不用写。
@JoinTable
作用:针对中间表的配置
属性:
nam:配置中间表的名称
joinColumns:中间表的外键字段关联当前实体类所对应表的主键字段
inverseJoinColumn:中间表的外键字段关联对方表的主键字段
@JoinColumn
作用:用于定义主键字段和外键字段的对应关系。
属性:
name:指定外键字段的名称
referencedColumnName:指定引用主表的主键字段名称
unique:是否唯一。默认值不唯一
nullable:是否允许为空。默认值允许。
insertable:是否允许插入。默认值允许。
updatable:是否允许更新。默认值允许。
columnDefinition:列的定义信息。
User
package xyz.ytfs.entity; import javax.persistence.*; import java.util.HashSet; import java.util.Set; /** * @author by ytfs * @Classname User * @Description TODO(用户类) * @Date 2020/5/9 23:47 */ @Entity @Table(name = "sys_user") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "user_id") private Long userId; @Column(name = "user_name") private String userName; /** * 配置用户到角色的多对多关系 * 配置多对多的映射关系 * 1.声明表关系的配置 * * @ManyToMany(targetEntity = Role.class) //多对多 * targetEntity:代表对方的实体类字节码 * 2.配置中间表(包含两个外键) * @JoinTable name : 中间表的名称 * joinColumns:配置当前对象在中间表的外键 * @JoinColumn的数组 name:外键名 * referencedColumnName:参照的主表的主键名 * inverseJoinColumns:配置对方对象在中间表的外键 */ @ManyToMany(targetEntity = Role.class, cascade = CascadeType.ALL) @JoinTable(name = "sys_user_role", //joinColumns,当前对象在中间表中的外键 joinColumns = {@JoinColumn(name = "sys_user_id", referencedColumnName = "user_id")}, //inverseJoinColumns,对方对象在中间表的外键 inverseJoinColumns = {@JoinColumn(name = "sys_role_id", referencedColumnName = "role_id")}) private Setroles = new HashSet<>(); public Long getUserId() { return userId; } public void setUserId(Long userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public Set getRoles() { return roles; } public void setRoles(Set roles) { this.roles = roles; } @Override public String toString() { return "User{" + "userId=" + userId + ", userName='" + userName + '\'' + ", roles=" + roles + '}'; } }
Role
package xyz.ytfs.entity; import javax.persistence.*; import java.util.HashSet; import java.util.Set; /** * @author by ytfs * @Classname Role * @Description TODO(用户对应的类型) * @Date 2020/5/9 23:47 */ @Entity @Table(name = "sys_role") public class Role { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "role_id") private Long roleId; @Column(name = "role_name") private String roleName; /** * 配置用户到角色的多对多关系 * 配置多对多的映射关系 * 1.声明表关系的配置 * @ManyToMany(targetEntity = User.class) //多对多 * targetEntity:代表对方的实体类字节码 * 2.配置中间表(包含两个外键) * @JoinTable * name : 中间表的名称 * joinColumns:配置当前对象在中间表的外键 * @JoinColumn的数组 * name:外键名 * referencedColumnName:参照的主表的主键名 * inverseJoinColumns:配置对方对象在中间表的外键 */ /* @ManyToMany(targetEntity = User.class) @JoinTable(name = "sys_user_role", //joinColumns,当前对象在中间表中的外键 joinColumns = {@JoinColumn(name = "sys_role_id", referencedColumnName = "role_id")}, //inverseJoinColumns,对方对象在中间表的外键 inverseJoinColumns = {@JoinColumn(name = "sys_user_id", referencedColumnName = "user_id")})*/ @ManyToMany(mappedBy = "roles") private Setusers = new HashSet<>(); public Set getUsers() { return users; } public void setUsers(Set users) { this.users = users; } public Long getRoleId() { return roleId; } public void setRoleId(Long roleId) { this.roleId = roleId; } public String getRoleName() { return roleName; } public void setRoleName(String roleName) { this.roleName = roleName; } @Override public String toString() { return "Role{" + "roleId=" + roleId + ", roleName='" + roleName + '\'' + ", users=" + users + '}'; } }
创建数据访问层的接口
IUserDao
package xyz.ytfs.dao; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import xyz.ytfs.entity.User; /** * @author by ytfs * @Classname UserDao * @Description TODO(用户的数据访问层接口) * @Date 2020/5/10 0:05 */ public interface IUserDao extends JpaRepository, JpaSpecificationExecutor { }
IRoleDao
package xyz.ytfs.dao; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import xyz.ytfs.entity.Role; /** * @author by ytfs * @Classname IRoleDao * @Description TODO(用户对应的职位数据访问层) * @Date 2020/5/10 0:07 */ public interface IRoleDao extends JpaRepository, JpaSpecificationExecutor { }
测试类
package xyz.ytfs.test; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.annotation.Rollback; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import xyz.ytfs.dao.IRoleDao; import xyz.ytfs.dao.IUserDao; import xyz.ytfs.entity.Role; import xyz.ytfs.entity.User; import javax.transaction.Transactional; import java.util.HashSet; /** * @author by ytfs * @Classname JpaManyToManyTest * @Description TODO(多对多的测试类) * @Date 2020/5/10 0:08 */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContext.xml") public class JpaManyToManyTest { @Autowired private IRoleDao roleDao; @Autowired private IUserDao userDao; /** * 保存一个用户,保存一个角色 ** 多对多放弃维护权:被动的一方(这里就是被选择的一方Role)放弃
*/ @Test @Transactional @Rollback(false) public void testSave() { User user = new User(); user.setUserName("雨听风说"); Role role = new Role(); role.setRoleName("程序猿"); /* 表达多对多的关系,双方都表达的时候必须有一方放弃维护中间表,不然会抛出异常 */ role.getUsers().add(user); user.getRoles().add(role); this.userDao.save(user); this.roleDao.save(role); } //测试级联添加(保存一个用户的同时保存用户的关联角色) @Test @Transactional @Rollback(false) public void testCasacdeSave() { User user = new User(); user.setUserName("雨听风说"); Role role = new Role(); role.setRoleName("程序猿"); /* 表达多对多的关系 */ role.getUsers().add(user); user.getRoles().add(role); //级联操作,在保存用户的同时保存用户的类型 this.userDao.save(user); } /** * 级联删除,删除id为1的客户 */ @Test @Transactional @Rollback(false) public void testDeleteCasaced() { //查询id为1的用户 User user = this.userDao.getOne(1L); //删除id为1的用户,级联删除用户对应的类型 this.userDao.delete(user); } }