Spring Data JPA基本增删改查和JPQL查询(含完整代码和视频连接) (2)

在applicationContext.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:lang="http://www.springframework.org/schema/lang" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/lang/spring-lang.xsd http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/context/spring-context.xsd"> <!-- spring 和 spring data jpa 的配置--> <!-- 1.创建entityManagerFactory对象交给spring容器管理--> <bean id ="entityManagerFactoty"> <property ref="dataSource"/> <!-- 配置的扫描的包(实体类所在的包)--> <property value="cn.itcast.domain"/> <!-- jpa的实现厂家 --> <property> <bean/> </property> <!-- jpa的供应适配器 --> <property> <bean class = "org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property value="false" /> <!-- 指定数据库类型 --> <property value="MYSQL" /> <!--数据库方言: 支持的特有语法--> <property value="org.hibernate.dialect.MySQLDialect"/> <property value="true"/> </bean> </property> <!-- jpa的方言 : 高级的特性 --> <property> <bean/> </property> </bean> <!--2.创建数据库连接池--> <bean id = "dataSource"> <property value= "root"/> <property value = "root"/> <property value="jdbc:mysql:///jpa"/> <property value="com.mysql.jdbc.Driver"/> </bean> <!-- 3.整合 spring datajpa --> <jpa:repositories base-package="cn.itcast.dao" transaction-manager-ref="transactionManager" entity-manager-factory-ref="entityManagerFactoty"></jpa:repositories> <!--4.配置事务管理器--> <bean id = "transactionManager" class = "org.springframework.orm.jpa.JpaTransactionManager"> <property ref = "entityManagerFactoty"></property> </bean> <!-- 5。声明式事务 --> <!-- 6. 配置包扫描 --> <context:component-scan base-package="cn.itcast"></context:component-scan> </beans>

3.实体类添加注解

package cn.itcast.domain; import javax.persistence.*; /** * 客户的实体类 * 配置映射关系 * 1.实体类和表的映射关系 * 2.实体类中属性和表中字段的映射关系 * @Entity:声明实体类 * @Table:配置实体类和表的映射关系 * name:配置数据库表的名称 */ @Entity @Table(name = "cst_customer") public class Customer { /** * @Id:声明主键的配置 * @GeneratedValue:配置主键的生成策略 * strategy * GenerationType.IDENTITY:自增,mysql * *底层数据库必须支持自动增长(底层数据库支持的自动增长方式,对id自增) * GenerationType.SEQUENCE:序列,oracle * *底层数据库必须支持序列 * GenerationType.TABLE :jpa提供的一种机制,通过一张数据库表的形式帮助我们完成主键自增 * GenerationType.AUTO :有程序自动的帮助我们选择主键生成策略 * @Column:配置属性和字段的映射关系 * name:数据库表中字段的名称 */ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "cust_id") private long custId;//客户的主键 @Column(name = "cust_name") private String custName;//客户名称 @Column(name = "cust_source") private String custSource;//客户来源 @Column(name = "cust_level") private String custLevel;//客户级别 @Column(name = "cust_industry") private String custIndustry;//客户所属行业 @Column(name = "cust_phone") private String custPhone;//客户的联系方式 @Column(name = "cust_address") private String custAddress;//客户地址 public long getCustId() { return custId; } public void setCustId(long custId) { this.custId = custId; } public String getCustName() { return custName; } public void setCustName(String custName) { this.custName = custName; } public String getCustSource() { return custSource; } public void setCustSource(String custSource) { this.custSource = custSource; } public String getCustLevel() { return custLevel; } public void setCustLevel(String custLevel) { this.custLevel = custLevel; } public String getCustIndustry() { return custIndustry; } public void setCustIndustry(String custIndustry) { this.custIndustry = custIndustry; } public String getCustPhone() { return custPhone; } public void setCustPhone(String custPhone) { this.custPhone = custPhone; } public String getCustAddress() { return custAddress; } public void setCustAddress(String custAddress) { this.custAddress = custAddress; } @Override public String toString() { return "Customer{" + "custId=" + custId + ", custName='" + custName + '\'' + ", custSource='" + custSource + '\'' + ", custLevel='" + custLevel + '\'' + ", custIndustry='" + custIndustry + '\'' + ", custPhone='" + custPhone + '\'' + ", custAddress='" + custAddress + '\'' + '}'; } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zwdpxf.html