spring jdbc tutorials querydsl
This commit is contained in:
@@ -19,6 +19,7 @@
|
||||
<java.version>1.7</java.version>
|
||||
<spring.version>4.0.6.RELEASE</spring.version>
|
||||
<cglib.version>2.2.2</cglib.version>
|
||||
<querydsl.version>2.9.0</querydsl.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
@@ -62,6 +63,24 @@
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-jdbc-core</artifactId>
|
||||
<version>1.0.0.RELEASE</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.mysema.querydsl</groupId>
|
||||
<artifactId>querydsl-sql</artifactId>
|
||||
<version>${querydsl.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
<version>1.6.1</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>cglib</groupId>
|
||||
<artifactId>cglib-nodep</artifactId>
|
||||
@@ -94,10 +113,53 @@
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>5.1.31</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>1.3.176</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
<build>
|
||||
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>com.mysema.querydsl</groupId>
|
||||
<artifactId>querydsl-maven-plugin</artifactId>
|
||||
<version>${querydsl.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>export</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<jdbcDriver>com.mysql.jdbc.Driver</jdbcDriver>
|
||||
<jdbcUrl>jdbc:mysql://localhost:3306/sjdbc</jdbcUrl>
|
||||
<jdbcUser>sa</jdbcUser>
|
||||
<schemaPattern>PUBLIC</schemaPattern>
|
||||
<packageName>org.spring.jdbc.tutorials.model</packageName>
|
||||
<targetFolder>${project.basedir}/src/generated/java</targetFolder>
|
||||
</configuration>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>5.1.31</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
<version>1.1.2</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
|
||||
</project>
|
||||
|
||||
+236
@@ -2,6 +2,7 @@ package org.spring.jdbc.tutorials.dao.impl;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
@@ -12,24 +13,46 @@ import org.spring.jdbc.tutorials.CustomerMappingSqlQuery;
|
||||
import org.spring.jdbc.tutorials.CustomerRowMapper;
|
||||
import org.spring.jdbc.tutorials.CustomerSqlUpdate;
|
||||
import org.spring.jdbc.tutorials.dao.CustomerDAO;
|
||||
import org.spring.jdbc.tutorials.model.Address;
|
||||
import org.spring.jdbc.tutorials.model.Customer;
|
||||
import org.spring.jdbc.tutorials.model.QAddress;
|
||||
import org.spring.jdbc.tutorials.model.QCustomer;
|
||||
import org.springframework.data.jdbc.core.OneToManyResultSetExtractor;
|
||||
import org.springframework.data.jdbc.query.QueryDslJdbcTemplate;
|
||||
import org.springframework.data.jdbc.query.SqlDeleteCallback;
|
||||
import org.springframework.data.jdbc.query.SqlInsertWithKeyCallback;
|
||||
import org.springframework.data.jdbc.query.SqlUpdateCallback;
|
||||
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
|
||||
import org.springframework.jdbc.core.BeanPropertyRowMapper;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.PreparedStatementCreator;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
|
||||
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
|
||||
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
|
||||
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
|
||||
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
|
||||
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
|
||||
import org.springframework.jdbc.core.simple.SimpleJdbcCall;
|
||||
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
|
||||
import org.springframework.jdbc.support.GeneratedKeyHolder;
|
||||
import org.springframework.jdbc.support.KeyHolder;
|
||||
|
||||
import com.mysema.query.sql.SQLQuery;
|
||||
import com.mysema.query.sql.dml.SQLDeleteClause;
|
||||
import com.mysema.query.sql.dml.SQLInsertClause;
|
||||
import com.mysema.query.sql.dml.SQLUpdateClause;
|
||||
import com.mysema.query.types.Path;
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public class JdbcCustomerDAO implements CustomerDAO {
|
||||
|
||||
/**
|
||||
* QueryDslJdbcTemplate
|
||||
*/
|
||||
private QueryDslJdbcTemplate queryDslJdbcTemplate;
|
||||
|
||||
|
||||
/**
|
||||
* JdbcTemplate
|
||||
*/
|
||||
@@ -63,6 +86,186 @@ public class JdbcCustomerDAO implements CustomerDAO {
|
||||
*/
|
||||
private CustomerSqlUpdate customerSqlUpdate;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 使用OneToManyResultSetExtractor 一对多查询
|
||||
* @return
|
||||
*/
|
||||
public List<Customer> useOneToManyResultSetExtractor(){
|
||||
String sql = "SELECT customer.cust_id, customer.name, customer.age, address.id," +
|
||||
" address.cust_id, address.street, address.city " +
|
||||
" FROM customer " +
|
||||
" LEFT JOIN address ON customer.cust_id = address.cust_id " +
|
||||
" ORDER BY customer.cust_id" ;
|
||||
|
||||
List<Customer> list = jdbcTemplate.query(sql, new OneToManyResultSetExtractor<Customer,Address,Integer>(new CustomerMapper(),new AddressMapper()){
|
||||
|
||||
protected Integer mapPrimaryKey(ResultSet rs) throws SQLException {
|
||||
return rs.getInt("customer.cust_id");
|
||||
}
|
||||
|
||||
protected Integer mapForeignKey(ResultSet rs) throws SQLException {
|
||||
if(rs.getObject("address.cust_id") == null){
|
||||
return null;
|
||||
}else{
|
||||
return rs.getInt("address.cust_id");
|
||||
}
|
||||
}
|
||||
|
||||
protected void addChild(Customer root, Address child) {
|
||||
root.addAddress(child);
|
||||
}
|
||||
|
||||
});
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 该内部类主要用来处理OneToManyResultSetExtractor
|
||||
* @author welcome
|
||||
*
|
||||
*/
|
||||
class CustomerMapper implements RowMapper<Customer> {
|
||||
|
||||
@Override
|
||||
public Customer mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
Customer customer = new Customer();
|
||||
customer.setCustId(rs.getInt("customer.cust_id"));
|
||||
customer.setName(rs.getString("customer.name"));
|
||||
customer.setAge(rs.getInt("customer.age"));
|
||||
return customer;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 该内部类主要用来处理OneToManyResultSetExtractor
|
||||
* @author welcome
|
||||
*
|
||||
*/
|
||||
class AddressMapper implements RowMapper<Address> {
|
||||
|
||||
@Override
|
||||
public Address mapRow(ResultSet rs, int rowNum) throws SQLException {
|
||||
Address address = new Address();
|
||||
address.setId(rs.getInt("address.id"));
|
||||
address.setStreet(rs.getString("address.street"));
|
||||
address.setCity(rs.getString("address.city"));
|
||||
return address;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 使用QueryDslJdbcTemplate查询
|
||||
* @return
|
||||
*/
|
||||
public List<Customer> useQueryDslQueryAll(){
|
||||
|
||||
final QCustomer qCustomer = new QCustomer("customer");
|
||||
final QAddress qAddress = new QAddress("address");
|
||||
|
||||
SQLQuery sqlQuery = queryDslJdbcTemplate.newSqlQuery().from(qCustomer).leftJoin(qCustomer._addressCustomerRef,qAddress);
|
||||
|
||||
List<Customer> list = queryDslJdbcTemplate.queryForObject(sqlQuery, new OneToManyResultSetExtractor<Customer,Address,Integer>(new CustomerMapper(),new AddressMapper()) {
|
||||
|
||||
@Override
|
||||
protected Integer mapPrimaryKey(ResultSet rs) throws SQLException {
|
||||
return rs.getInt(qCustomer.custId.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Integer mapForeignKey(ResultSet rs) throws SQLException {
|
||||
String columnname = qAddress.addressCustomerRef.getLocalColumns().get(0).toString();
|
||||
if(rs.getObject(columnname) == null){
|
||||
return null;
|
||||
}else{
|
||||
return rs.getInt(columnname);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void addChild(Customer root, Address child) {
|
||||
root.addAddress(child);
|
||||
}
|
||||
|
||||
|
||||
}, new Path[]{qCustomer.custId,qCustomer.name,qCustomer.age,qAddress.id,qAddress.custId,qAddress.street,qAddress.city});
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 使用QueryDsl新增
|
||||
*/
|
||||
public int useQueryDslInsert(final Customer customer){
|
||||
|
||||
final QCustomer qCustomer = new QCustomer("customer");
|
||||
|
||||
int returnVal = queryDslJdbcTemplate.insertWithKey(qCustomer, new SqlInsertWithKeyCallback<Integer>() {
|
||||
|
||||
@Override
|
||||
public Integer doInSqlInsertWithKeyClause(SQLInsertClause insert)
|
||||
throws SQLException {
|
||||
return insert.columns(qCustomer.name,qCustomer.age).values(customer.getName(),customer.getAge()).executeWithKey(qCustomer.custId);
|
||||
}
|
||||
});
|
||||
|
||||
return returnVal;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 使用QueryDsl修改
|
||||
*/
|
||||
public long useQueryDslUpdate(final Customer customer){
|
||||
|
||||
final QCustomer qCustomer = new QCustomer("customer");
|
||||
|
||||
long returnVal = queryDslJdbcTemplate.update(qCustomer, new SqlUpdateCallback() {
|
||||
|
||||
@Override
|
||||
public long doInSqlUpdateClause(SQLUpdateClause update) {
|
||||
return update.
|
||||
where(qCustomer.custId.eq(customer.getCustId())).
|
||||
set(qCustomer.name, customer.getName()).
|
||||
set(qCustomer.age, customer.getAge()).
|
||||
execute();
|
||||
}
|
||||
});
|
||||
|
||||
return returnVal;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 使用QueryDsl删除
|
||||
*/
|
||||
public long useQueryDslDelete(final Customer customer){
|
||||
|
||||
final QCustomer qCustomer = new QCustomer("customer");
|
||||
|
||||
long returnVal = queryDslJdbcTemplate.delete(qCustomer, new SqlDeleteCallback() {
|
||||
|
||||
@Override
|
||||
public long doInSqlDeleteClause(SQLDeleteClause delete) {
|
||||
return delete.where(qCustomer.custId.eq(customer.getCustId())).execute();
|
||||
}
|
||||
});
|
||||
|
||||
return returnVal;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 调用存储过程
|
||||
* DELIMITER //
|
||||
@@ -110,6 +313,31 @@ public class JdbcCustomerDAO implements CustomerDAO {
|
||||
return name;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Returning ResultSet/REF Cursor from a SimpleJdbcCall
|
||||
*
|
||||
* DELIMITER //
|
||||
* CREATE PROCEDURE read_all_customers()
|
||||
* BEGIN
|
||||
* SELECT a.CUST_ID, a.NAME, a.AGE FROM customer a;
|
||||
* END//
|
||||
*
|
||||
* 使用simpleJdbcCall调用read_all_customers存储过程返回一个ResultSet Cursor
|
||||
*
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<Customer> getResultSetCursorUseSimpleJdbcCall(){
|
||||
Map<String, Object> map = simpleJdbcCall.withProcedureName("read_all_customers").
|
||||
returningResultSet("customers", ParameterizedBeanPropertyRowMapper.
|
||||
newInstance(Customer.class)).
|
||||
execute(new HashMap<String, Object>(0));
|
||||
return (List<Customer>) map.get("customers");
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 使用MappingSqlQuery方式查询
|
||||
* @return
|
||||
@@ -493,6 +721,14 @@ public class JdbcCustomerDAO implements CustomerDAO {
|
||||
this.simpleJdbcInsert = simpleJdbcInsert;
|
||||
}
|
||||
|
||||
public QueryDslJdbcTemplate getQueryDslJdbcTemplate() {
|
||||
return queryDslJdbcTemplate;
|
||||
}
|
||||
|
||||
public void setQueryDslJdbcTemplate(QueryDslJdbcTemplate queryDslJdbcTemplate) {
|
||||
this.queryDslJdbcTemplate = queryDslJdbcTemplate;
|
||||
}
|
||||
|
||||
public JdbcTemplate getJdbcTemplate() {
|
||||
return jdbcTemplate;
|
||||
}
|
||||
|
||||
@@ -1,11 +1,16 @@
|
||||
package org.spring.jdbc.tutorials.model;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class Customer {
|
||||
|
||||
private int custId;
|
||||
private String name;
|
||||
private int age;
|
||||
|
||||
private Set<Address> addresses = new HashSet<Address>();
|
||||
|
||||
public Customer() {
|
||||
}
|
||||
|
||||
@@ -40,11 +45,25 @@ public class Customer {
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public Set<Address> getAddresses() {
|
||||
return addresses;
|
||||
}
|
||||
|
||||
public void setAddresses(Set<Address> addresses) {
|
||||
this.addresses = addresses;
|
||||
}
|
||||
|
||||
public void addAddress(Address address) {
|
||||
this.addresses.add(address);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Customer [custId=" + custId + ", name=" + name + ", age=" + age
|
||||
+ "]";
|
||||
+ ", addresses=" + addresses + "]";
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -22,9 +22,13 @@
|
||||
http://www.springframework.org/schema/task
|
||||
http://www.springframework.org/schema/task/spring-task-3.2.xsd">
|
||||
|
||||
<jdbc:embedded-database id="dataSource">
|
||||
<jdbc:initialize-database data-source="dataSource" ignore-failures="DROPS" enabled="false">
|
||||
<jdbc:script location="classpath:jdbc.sql"/>
|
||||
</jdbc:embedded-database>
|
||||
</jdbc:initialize-database>
|
||||
|
||||
<bean id="queryDslJdbcTemplate" class="org.springframework.data.jdbc.query.QueryDslJdbcTemplate">
|
||||
<constructor-arg name="dataSource" ref="dataSource"></constructor-arg>
|
||||
</bean>
|
||||
|
||||
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
|
||||
<constructor-arg name="dataSource" ref="dataSource"/>
|
||||
@@ -63,6 +67,7 @@
|
||||
<bean id="JdbcCustomerDAO" class="org.spring.jdbc.tutorials.dao.impl.JdbcCustomerDAO">
|
||||
<property name="jdbcTemplate" ref="jdbcTemplate" />
|
||||
<property name="simpleJdbcCall" ref="simpleJdbcCall" />
|
||||
<property name="queryDslJdbcTemplate" ref="queryDslJdbcTemplate"/>
|
||||
<property name="namedParameterJdbcTemplate" ref="namedParameterJdbcTemplate"/>
|
||||
<property name="simpleJdbcInsert" ref="simpleJdbcInsert"/>
|
||||
<property name="customerMappingSqlQuery" ref="customerMappingSqlQuery"/>
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
CREATE TABLE Employee (
|
||||
DROP TABLE employee;
|
||||
|
||||
CREATE TABLE employee (
|
||||
ID INT(10) NOT NULL AUTO_INCREMENT,
|
||||
NAME VARCHAR(100) NOT NULL,
|
||||
AGE INT(10) NOT NULL,
|
||||
PRIMARY KEY (ID)
|
||||
);
|
||||
|
||||
DROP TABLE customer;
|
||||
|
||||
CREATE TABLE customer (
|
||||
CUST_ID INT(10) NOT NULL AUTO_INCREMENT,
|
||||
NAME VARCHAR(100) NOT NULL,
|
||||
@@ -12,6 +16,29 @@ CREATE TABLE customer (
|
||||
PRIMARY KEY (CUST_ID)
|
||||
);
|
||||
|
||||
DROP TABLE address;
|
||||
CREATE TABLE address (
|
||||
id INT(10) NOT NULL AUTO_INCREMENT,
|
||||
cust_id INT(10),
|
||||
street VARCHAR(255),
|
||||
city VARCHAR(255),
|
||||
PRIMARY KEY(id),
|
||||
CONSTRAINT address_customer_ref
|
||||
FOREIGN KEY (cust_id) REFERENCES customer (cust_id)
|
||||
);
|
||||
|
||||
DROP TABLE contact;
|
||||
|
||||
CREATE TABLE contact (
|
||||
contact_id int(11) NOT NULL AUTO_INCREMENT,
|
||||
name varchar(45) NOT NULL,
|
||||
email varchar(45) NOT NULL,
|
||||
address varchar(45) NOT NULL,
|
||||
telephone varchar(45) NOT NULL,
|
||||
PRIMARY KEY (contact_id)
|
||||
);
|
||||
|
||||
|
||||
-- 存储过程
|
||||
|
||||
DELIMITER //
|
||||
@@ -25,6 +52,13 @@ BEGIN
|
||||
FROM customer WHERE CUST_ID = in_id;
|
||||
END//
|
||||
|
||||
-- 存储过程
|
||||
|
||||
DELIMITER //
|
||||
CREATE PROCEDURE read_all_customers()
|
||||
BEGIN
|
||||
SELECT a.CUST_ID, a.NAME, a.AGE FROM customer a;
|
||||
END//
|
||||
|
||||
-- 函数
|
||||
|
||||
@@ -37,12 +71,3 @@ BEGIN
|
||||
RETURN out_name;
|
||||
END//
|
||||
|
||||
|
||||
CREATE TABLE contact (
|
||||
contact_id int(11) NOT NULL AUTO_INCREMENT,
|
||||
name varchar(45) NOT NULL,
|
||||
email varchar(45) NOT NULL,
|
||||
address varchar(45) NOT NULL,
|
||||
telephone varchar(45) NOT NULL,
|
||||
PRIMARY KEY (contact_id)
|
||||
);
|
||||
+33
@@ -199,6 +199,39 @@ public class JdbcCustomerDAOTest {
|
||||
System.out.println(jdbcCustomerDAO.useSqlUpdate(1,"useSqlUpdate"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getResultSetCursorUseSimpleJdbcCall(){
|
||||
System.out.println(jdbcCustomerDAO.getResultSetCursorUseSimpleJdbcCall());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void useOneToManyResultSetExtractor(){
|
||||
System.out.println(jdbcCustomerDAO.useOneToManyResultSetExtractor());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void useQueryDslQueryAll(){
|
||||
System.out.println(jdbcCustomerDAO.useQueryDslQueryAll());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void useQueryDslInsert(){
|
||||
Customer customer = new Customer("useQueryDslInsert",23);
|
||||
System.out.println(jdbcCustomerDAO.useQueryDslInsert(customer));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void useQueryDslUpdate(){
|
||||
Customer customer = new Customer(1,"useQueryDslUpdate",23);
|
||||
System.out.println(jdbcCustomerDAO.useQueryDslUpdate(customer));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void useQueryDslDelete(){
|
||||
Customer customer = new Customer(10,"useQueryDslInsert",23);
|
||||
System.out.println(jdbcCustomerDAO.useQueryDslDelete(customer));
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user