springjpa连接postgre
来源:互联网 发布:linux下串口调试 编辑:程序博客网 时间:2024/06/05 04:38
1 xml配置文件
首先,在src/main/resources下,建立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:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--属性文件读入-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver"/>
<property name="url" value="jdbc:postgresql://localhost:5432/dataBase1"/>
<property name="username" value="postgres"/>
<property name="password" value="1234567"/>
</bean>
<context:component-scan base-package="cn.dev.sync.core"/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="spring"></property>
<property name="dataSource" ref="dataSource"></property>
<property name="packagesToScan" value="cn.dev.sync.core.domain"></property>
<property name="persistenceProviderClass" value="org.hibernate.jpa.HibernatePersistenceProvider"></property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<!--设置为true会自动生成DDL,并且在数据库中执行-->
<property name="generateDdl" value="false"/>
<property name="database" value="POSTGRESQL"/>
<property name="showSql" value="true"/>
</bean>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<jpa:repositories base-package="cn.dev.sync.core.util"
query-lookup-strategy = "use-declared-query"
entity-manager-factory-ref="entityManagerFactory"
transaction-manager-ref="transactionManager"
/>
</beans>
2 model类
其中:cn.dev.sync.core.domain包中存放了,具体的Modle类型。
如用户:User
package cn.dev.sync.core.domain;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "v_user") //liferay实体中User用户
public class User {
public User() {
super();
}
@Id //一定有有主键
@Column(name="uid")
private String uid;
@Column(name="email")
private String email;
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
3 测试类
package cn.dev.sync.core.test;
import java.util.List;
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 cn.dev.sync.core.domain.User;
import cn.dev.sync.core.service.UserService;
@RunWith(SpringJUnit4ClassRunner.class) //使用junit4进行测试
@ContextConfiguration ({"/applicationContextLiferay.xml"})
public class LiferayTest {
@Autowired
private UserService userService ;
@org.junit.Test
public void search(){
List<User> userList = userService.searchAll();
for(int i=0;i<100;i++){
System.out.println(userList.get(i).getUid()+"-----");
}
System.out.println(userList.size()+"---------userList size-----------");
}
}
4 其他文件
UserService和UserDao
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.dev.sync.core.domain.User;
import cn.dev.sync.core.util.UserDao;
@Service
public class UserService{
@Autowired
private UserDao userDao;
public List<User> searchAll() {
return (List<User>)userDao.findAll();
}
}
UserDao文件:
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.PagingAndSortingRepository;
import cn.dev.sync.core.domain.User;
public interface UserDao extends PagingAndSortingRepository<User, Integer>,JpaSpecificationExecutor<User>
{
}
- springjpa连接postgre
- C#连接Postgre SQL
- postgre连接符
- ado、odbc连接Postgre SQL
- postgre 配置与连接代码
- ado、odbc连接Postgre SQL
- ado、odbc连接Postgre SQL
- postgre数据库远程连接配置
- arcgis连接postgre数据库问题
- 使用C语言连接postgre数据库
- perl,dbi连接postgre和mysql
- postgre按照某列前几位连接并显示
- Windows下对postgre开启远程连接权限
- maven+springjpa+hibernate
- 八.springJPA入门
- FATAL: no pg_hba.conf entry for host "::1" odoo10连接postgre navicate连接问题
- postgre constraints
- postgre索引
- iOS-视频 FFmpeg的使用
- V8配置语法
- 微信公众平台开发历程(四)
- 取消控件中Text默认大写的方法
- Hive既分区又分桶
- springjpa连接postgre
- 写给初学者的Python与pip安装教程
- idea搭建maven项目
- java开发环境的搭建(基础配置二 ——Eclipse)
- 关于内购的那些坑
- WUST OJ 1349 TLE(简单DP)
- HDU
- QT5.5.0版本添加icon图标步骤
- Qt 设置某项Windows服务的启动方式