本文共 6995 字,大约阅读时间需要 23 分钟。
package shangbo.spring.jdbc.example2;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class App { public static void main(String[] args) throws Exception { // 实例化 Spring IoC 容器 ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); // 从容器中获得 BusinessService 的实例 BusinessService service = context.getBean(BusinessService.class); // 业务逻辑 service.doWork(); }}
package shangbo.spring.jdbc.example2;import javax.sql.DataSource;import org.apache.commons.dbcp.BasicDataSource;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.jdbc.datasource.DataSourceTransactionManager;import org.springframework.transaction.annotation.EnableTransactionManagement;@Configuration@EnableTransactionManagement // 开启事务管理public class AppConfig { @Bean public BasicDataSource dataSource() { BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver"); dataSource.setUrl("jdbc:oracle:thin:@localhost:1521:xe"); dataSource.setUsername("hr"); dataSource.setPassword("123456"); return dataSource; } @Bean public DataSourceTransactionManager txManager(DataSource dataSource) { DataSourceTransactionManager txManager = new DataSourceTransactionManager(); txManager.setDataSource(dataSource); return txManager; } @Bean public JobDao jobDao(DataSource dataSource) { JobDao jobDao = new JobDaoImpl(); jobDao.setDataSource(dataSource); return jobDao; } @Bean public BusinessService businessService(JobDao jobDao) { BusinessService businessService = new BusinessServiceImpl(); businessService.setJobDao(jobDao); return businessService; }}
package shangbo.spring.jdbc.example2;public interface BusinessService { void doWork(); void setJobDao(JobDao jobDao);}
package shangbo.spring.jdbc.example2;import java.util.ArrayList;import java.util.List;public class BusinessServiceImpl implements BusinessService { private JobDao jobDao; public void doWork() { ListjobIds = new ArrayList (); jobIds.add("IT"); jobIds.add("AD_PRES"); List jobs = jobDao.queryJobByIds(jobIds); for(Job job: jobs) { System.out.println(job); } } public void setJobDao(JobDao jobDao) { this.jobDao = jobDao; }}
package shangbo.spring.jdbc.example2;import java.util.List;import javax.sql.DataSource;public interface JobDao { // // DDL // void createTableJobs(); // // Insert // int insertJob(Job job); int[] insertJob(Listjobs); // // Update // int updateJob(Job job); // // Query // Job queryJobById(String jobId); List queryJobByIds(List jobIds); List queryAllJob(); // // Delete // int deleteJobById(String jobId); // // Call Stored Procedure // // this.jdbcTemplate.update("call SUPPORT.REFRESH_ACTORS_SUMMARY(?)", Long.valueOf(unionId)); // // Call Function // // // Setter // void setDataSource(DataSource dataSource);}
package shangbo.spring.jdbc.example2;import java.util.Collections;import java.util.List;import java.util.Map;import javax.sql.DataSource;import org.springframework.jdbc.core.BeanPropertyRowMapper;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.stereotype.Repository;import org.springframework.transaction.annotation.Transactional;@Repositorypublic class JobDaoImpl implements JobDao { private NamedParameterJdbcTemplate jdbcTemplate; // // DDL // @Transactional public void createTableJobs() { String sql = "create table jobs (job_id varchar2(10), job_title varchar2(35), min_salary number(6,0), max_salary number(6,0))"; jdbcTemplate.getJdbcOperations().execute(sql); } // // Insert // @Transactional public int insertJob(Job job) { String sql = "insert into jobs values (:jobId, :jobTitle, :minSalary, :maxSalary)"; return jdbcTemplate.update(sql, new BeanPropertySqlParameterSource(job)); } @Transactional public int[] insertJob(Listjobs) { String sql = "insert into jobs values (:jobId, :jobTitle, :minSalary, :maxSalary)"; // 批量插入 SqlParameterSource[] batch = SqlParameterSourceUtils.createBatch(jobs.toArray()); return jdbcTemplate.batchUpdate(sql, batch); } // // Update // @Transactional public int updateJob(Job job) { String sql = "update jobs set job_title=:jobTitle, min_salary=:minSalary, max_salary=:maxSalary where job_id = :jobId"; return jdbcTemplate.update(sql, new BeanPropertySqlParameterSource(job)); } // // Query // @Transactional(readOnly = true) public Job queryJobById(String jobId) { String sql = "select * from jobs where job_id = :jobId"; SqlParameterSource namedParameters = new MapSqlParameterSource("jobId", jobId); return jdbcTemplate.queryForObject(sql, namedParameters, new BeanPropertyRowMapper (Job.class)); } @Transactional(readOnly = true) public List queryJobByIds(List jobIds) { String sql = "select * from jobs where job_id in (:jobIds)"; SqlParameterSource namedParameters = new MapSqlParameterSource("jobIds", jobIds); return jdbcTemplate.query(sql, namedParameters, new BeanPropertyRowMapper (Job.class)); } @Transactional(readOnly = true) public List queryAllJob() { String sql = "select * from jobs"; return jdbcTemplate.query(sql, new BeanPropertyRowMapper (Job.class)); } // // Delete // @Transactional public int deleteJobById(String jobId) { String sql = "delete from jobs where job_id = :jobId"; Map namedParameters = Collections.singletonMap("jobId", jobId); return jdbcTemplate.update(sql, namedParameters); } // // Setter // public void setDataSource(DataSource dataSource) { jdbcTemplate = new NamedParameterJdbcTemplate(dataSource); }}
package shangbo.spring.jdbc.example2;public class Job { private String jobId; private String jobTitle; private Integer minSalary; private Integer maxSalary; public String getJobId() { return jobId; } public void setJobId(String jobId) { this.jobId = jobId; } public String getJobTitle() { return jobTitle; } public void setJobTitle(String jobTitle) { this.jobTitle = jobTitle; } public Integer getMinSalary() { return minSalary; } public void setMinSalary(Integer minSalary) { this.minSalary = minSalary; } public Integer getMaxSalary() { return maxSalary; } public void setMaxSalary(Integer maxSalary) { this.maxSalary = maxSalary; } public String toString() { return "Job[jobId=" + jobId + ", jobTitle=" + jobTitle + ", minSalary=" + minSalary + ", maxSalary=" + maxSalary + "]"; }}
–
– 声 明:转载请注明出处 – Last Updated on 2017-06-17 – Written by ShangBo on 2017-06-17 – End