博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring NamedParameterJdbcTemplate
阅读量:4049 次
发布时间:2019-05-25

本文共 6995 字,大约阅读时间需要 23 分钟。

– Start


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() {		List
jobIds = 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(List
jobs); // // 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(List
jobs) { 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

你可能感兴趣的文章
使用批处理命令给客户更新oracle数据库
查看>>
回首考研路:那年,那月,还有那条孤独的考研狗
查看>>
GCD小结
查看>>
IOS 第三方库介绍
查看>>
iPhone架构xmpp聊天工具 -xmpp协议初识《一》
查看>>
iOS提交后申请加急审核
查看>>
iOS7单元测试
查看>>
ios framework 通用库的制作
查看>>
出现( linker command failed with exit code 1)错误总结
查看>>
iOS开发中一些常见的并行处理
查看>>
iOS获取手机的Mac地址
查看>>
ios7.1发布企业证书测试包的问题
查看>>
如何自定义iOS中的控件
查看>>
iOS 开发百问
查看>>
Mac环境下svn的使用
查看>>
github简单使用教程
查看>>
如何高效利用GitHub
查看>>
GitHub详细教程
查看>>
Swift概览
查看>>
iOS系统方法进行AES对称加密
查看>>