Spring(Data-JPA)
博客专区 > 赵-猛 的博客 > 博客详情
Spring(Data-JPA)
赵-猛 发表于6个月前
Spring(Data-JPA)
  • 发表于 6个月前
  • 阅读 1
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 新注册用户 域名抢购1元起>>>   

pom.xml

<dependency>
  <groupId>org.springframework.data</groupId>
  <artifactId>spring-data-commons</artifactId>
  <version>1.13.1.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework.data</groupId>
  <artifactId>spring-data-jpa</artifactId>
  <version>1.11.4.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-entitymanager</artifactId>
  <version>5.1.3.Final</version>
</dependency>
<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-validator</artifactId>
  <version>5.1.3.Final</version>
</dependency>

<dependency>
  <groupId>commons-dbcp</groupId>
  <artifactId>commons-dbcp</artifactId>
  <version>1.4</version>
</dependency>

<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>5.1.25</version>
</dependency>

 

spring-mysql.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:tx="http://www.springframework.org/schema/tx" 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-4.2.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.2.xsd
       http://www.springframework.org/schema/data/repository
       http://www.springframework.org/schema/data/repository/spring-repository-1.11.xsd
	   http://www.springframework.org/schema/tx 
	   http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
	   http://www.springframework.org/schema/data/jpa 
	   http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
	   ">

	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://${mysql.db.host}:${mysql.db.port}/${mysql.db.database}" />
		<property name="username" value="${mysql.db.username}" />
		<property name="password" value="${mysql.db.password}" />

		<property name="maxActive" value="${mysql.db.maxActive}" />
		<property name="maxIdle" value="${mysql.db.maxIdle}" />
		<property name="maxWait" value="${mysql.db.maxWait}" />
		<property name="defaultAutoCommit" value="${mysql.db.defaultAutoCommit}" />
	</bean>
	<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
			</props>
		</property>
	</bean>

	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" />

	<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
		<property name="showSql" value="true" />
		<property name="generateDdl" value="true" />
		<property name="database" value="MYSQL" />
	</bean>

	<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
		<property name="packagesToScan" value="me.zhaomeng.dao.model" />
	</bean>

	<bean id="entityManager" class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
		<property name="entityManagerFactory" ref="entityManagerFactory" />
	</bean>

	<jpa:repositories base-package="me.zhaomeng.dao" />
</beans>

spring.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"
	xsi:schemaLocation="
	   http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.2.xsd">
       
 	<import resource="classpath:spring-mysql.xml"/>
<!--  	<import resource="classpath:spring-scheduler.xml"/> -->
<!--  	<import resource="classpath:spring-cache.xml"/> -->

	<context:annotation-config />
	
	<context:property-placeholder location="classpath:spring.properties" />
	
	<bean class="me.zhaomeng.dao.uploader.DBUploader"/>
</beans>

spring.properties

##mysql database 
mysql.db.host=localhost
mysql.db.port=3306
mysql.db.database=xxxx
mysql.db.username=zhaomeng
mysql.db.password=zhaomeng
mysql.db.maxActive=100
mysql.db.maxIdle=30
mysql.db.maxWait=10
mysql.db.defaultAutoCommit=true

 

Address.java

@Entity
@Table(name = "address")
public class Address {
	
	@Id
	@Column(name = "id")
	public Integer id;
	
	@Column(name = "companyId")
	public Integer companyId;
	
	@Column(name = "userId")
	public Integer userId;
	
	@Column(name = "province")
	public String province;
	
	@Column(name = "city")
	public String city;
	
	@Column(name = "district")
	public String district;
	
	@Column(name = "detailAddress")
	public String detailAddress;
	
	@Column(name = "lat")
	public String lat;
	
	@Column(name = "lng")
	public String lng;
	
	@Column(name = "businessArea")
	public String businessArea;
	
	@Column(name = "createTime")
	public String createTime;
	
	@Column(name = "updateTime")
	public String updateTime;
	
	@Column(name = "useTime")
	public String useTime;
	
	@Column(name = "provinceCode")
	public String provinceCode;
	
	@Column(name = "cityCode")
	public String cityCode;
	
	@Column(name = "districtCode")
	public String districtCode;
	
	@Column(name = "businessAreaCode")
	public String businessAreaCode;
	
	@Column(name = "isDefault")
	public Boolean isDefault;
	
	@Column(name = "isEnable")
	public Boolean isEnable;
	
	@Column(name = "isDel")
	public Boolean isDel;
	
	@Column(name = "isValid")
	public Boolean isValid;

	@Override
	public String toString() {
		return "Address [id=" + id + ", companyId=" + companyId + ", userId=" + userId + ", province=" + province + ", city=" + city + ", district="
				+ district + ", detailAddress=" + detailAddress + ", lat=" + lat + ", lng=" + lng + ", businessArea=" + businessArea + ", createTime="
				+ createTime + ", updateTime=" + updateTime + ", useTime=" + useTime + ", provinceCode=" + provinceCode + ", cityCode=" + cityCode
				+ ", districtCode=" + districtCode + ", businessAreaCode=" + businessAreaCode + ", isDefault=" + isDefault + ", isEnable=" + isEnable
				+ ", isDel=" + isDel + ", isValid=" + isValid + "]";
	}
}

AddressDao.java

public interface AddressDAO extends JpaRepository<Address, Integer> {

}

XXXXXX.java

@Component
public class DBUploader {

	@Autowired
	private AddressDAO addressDao;

	@Autowired
	private CompanyDAO companyDao;
	
	public void loadDataToDB() throws IOException {
		FileHelper.consumeItems("data/www.lagou.com/lagou.txt", s -> {
			try {
				Company com = ModelHelper.fromJsonString(s);
				companyDao.save(com);
				
				addressDao.save(com.addressList);
			}
			catch (IOException e) {
				System.err.println(e);
			}
		});
	}
}

如果使用springboot,配置會非常簡單

共有 人打赏支持
粉丝 5
博文 78
码字总数 382762
×
赵-猛
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: