study-SpringDataJpa
这里总结的是在配置SpringDataJpa时的环境配置
SpringDataJpa已经告一段落了,这里显示的基本配置,如果想要详细的内容下面有Github的连接
环境的主要几个部署-1.pox.xml-2.application.xml
1.pox.xml的环境配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.7</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>SpringDateProtect</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringDateProtect</name>
<description>SpringDateProtect</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.application.xml环境配置,这里要注意一下-->
22(行),42(行),56(行),60(行)这4个点,这些要换成你自己的配置,
22,42,56,60--将代码复制进去后,就知道什么意思了
60(行)的地方,写扫描cn.itcast下面的“注释”所以主要的包我写的都是cn.itcast
22(行)这个地方,我写的是cn.itcast.domain:这里我放的是实体类,domain是cn.itcast下面的包
42(行)这个地方我写的是:我自己连接Mysql的信息,你们要改成自己的
56(行)这个地方,我写的是 cn.itcast.dao:这个是,写连接的dao是cn.itcast下面的包
application.xml代码块
<?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:context="http://www.springframework.org/schema/context"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
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/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<!-- <!– 配置读取properties文件的工具类 –>-->
<!-- <context:property-placeholder location="classpath:jdbc.properties"/>-->
<!-- 配置c3p0数据库连接池 -->
<bean id="EntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 以后会扫描这个包下的实体类(关注点-和数据库创建的实体类,放在这个里面,cn.itcast.domain这个是我的包,你要写自己的--)-->
<property name="packagesToScan" value="cn.itcast.domain"/>
<!-- jpa实现厂家-->
<property name="persistenceProvider">
<bean class="org.hibernate.jpa.HibernatePersistenceProvider"/>
</property>
<!-- jpa供应商配置器-->
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<!-- 是否自己创建数据库-->
<property name="generateDdl" value="false"/>
<!-- 指定数据库类型-->
<property name="database" value="MYSQL"/>
<!-- 使用的数据库的方言-语法-->
<property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"/>
<!-- 是否显示sql语句-->
<property name="showSql" value="true"/>
</bean>
</property>
<!-- jpa的方言-->
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
</property>
</bean>
<!--创建数据库连接池-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="root"></property>
<property name="password" value="XXXXXX"></property>
<property name="jdbcUrl" value="jdbc:mysql:///SpringJpa"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
</bean>
<!--整合jpaData(注意)-->
<jpa:repositories base-package="cn.itcast.dao" transaction-manager-ref="transactionManager"
entity-manager-factory-ref="EntityManagerFactory"></jpa:repositories>
<!-- 配置事务管理器-->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="EntityManagerFactory" ref="EntityManagerFactory"></property>
</bean>
<!-- 配置springIOC的注解扫描(这个要注意) -->
<context:component-scan base-package="cn.itcast"></context:component-scan>
</beans>
细节区的代码块:
cn.itcast.domain里面的内容--目录的Client
package cn.itcast.domain;
//要想直接调出-get-set-toString--(command+N)
import javax.persistence.*;
@Entity
@Table(name = "client")
public class Client {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "name")
private String name;
@Column(name = "job")
private String job;
@Column(name = "email")
private String email;
@Column(name = "number")
private int number;
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getJob() {
return this.job;
}
public void setJob(String job) {
this.job = job;
}
public String getEmail() {
return this.email = email;
}
public void setEmail(String email) {
this.email = email;
}
public int getNumber() {
return this.number = number;
}
public void setNumber(int number) {
this.number = number;
}
@Override
public String toString() {
return "domain{" +
"id=" + id +
", name='" + name + '\'' +
", job='" + job + '\'' +
", email='" + email + '\'' +
", number=" + number +
'}';
}
}
cn.itcast.dao里面的内容--目录的clientdao
package cn.itcast.dao; import cn.itcast.domain.Client;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
/* SpringDataJpa--接口
JpaRepository<T,Id>
T=操作类实体类类范型,T=主题的类名class后面的 ID=实体类主键属性的类范型 ID主键的类型
JpaSpecificationExecutor<T>
T=操作类实体类类范型,T=主题的类名class后面的(和上面的T一样)
*/
public interface clientDao extends JpaRepository<Client, Integer>, JpaSpecificationExecutor<Client> { }
Test
我这里总结的是,基础配置,必须要配置1.pom.xml-2.application.xml-3.domain(放实体类)-
4.dao(连接实体类)
剩下的——增删改查,在github中
2022-05-16
已更新内容--在GitHub中 https://github.com/2004chenxixi/SpringDataJpa-learn