框架(二)Spring框架之使用JDBC模板实际案例

本文详细介绍如何使用Spring JDBC模板进行数据库的增删改查操作,包括配置文件设置、XML配置详解及具体代码实现。

通过框架学习(一)3.4节,JDBC完整模板完成增删改查操作详细步骤【==总结

===========Spring JDBC模板的使用
1.引入必要的Jar包
在这里插入图片描述
2.抽取连接配置文件jdbc.properties

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///spring_1
jdbc.username=root
jdbc.password=123456

3.配置xml配置文件applicationContext.xml

<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:aop="http://www.springframework.org/schema/aop"
    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/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 1.引入配置文件 -->
      <context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 2.引入相应的值 -->
      <bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="${jdbc.driverClass}"/>         //===========jdbc.properties里的键
            <property name="jdbcUrl" value="${jdbc.url}"/>
            <property name="user" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>
      </bean>
<!--  3.配置Spring的JDBC模板     -->
       <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <property name="dataSource" ref="datasource"></property>
       </bean>
</beans>

4.代码完成数据库的增删改查

/**
 * @author Leonard
 *	JDBC完成增删改查
 */
@RunWith(SpringJUnit4ClassRunner.class)//====@Test用于测试
@ContextConfiguration("classpath:applicationContext.xml")//=======加载xml配置文件
public class spring_2 {
	@Resource(name="jdbcTemplate")//===========引入jdbc模板
	private JdbcTemplate jdbctemplate;
	/*==========增删改查操作=================*/
	@Test
	public void insert(){
		jdbctemplate.update("insert into account values(null,?,?)", "张三",230000d);
	}
	@Test	
	public void delete(){
		jdbctemplate.update("delete from account where id = ?", 1);
	}
	@Test	
	public void update(){
		jdbctemplate.update("update account set name = ? , money=? where id = ?", "李四",240000d,7);
	}
	/*================查询操作================*/
	//1.查询一个数据
	@Test
	public void select(){
		String name = jdbctemplate.queryForObject("select name from account where id=?", String.class, 5);
		System.out.println(name);
	}
	//2.统计个数
	@Test
	public void count(){
		Long count = jdbctemplate.queryForObject("select count(*) from account", Long.class);
		System.out.println("个数======="+count);
	}
	//3.将查询的数据封装到一个对象Account==创建domain层,set,get,重写toString
	@Test
	public void obj(){
		Account account = jdbctemplate.queryForObject("select * from account where id = ?", new MyRowMapper(), 5);
		System.out.println("单条数据========"+account);
	}
	//4.查询多条数据封装到对象
	@Test
	public void objs(){
		List<Account> list = jdbctemplate.query("select * from account", new MyRowMapper());
		System.out.println("集合列表======="+list);
	}

	/*=========数据封装=========================*/
	class MyRowMapper implements RowMapper<Account>{

		@Override
		public Account mapRow(ResultSet rs, int rowNum) throws SQLException {
			Account account = new Account();
			account.setId(rs.getInt("id"));
			account.setName(rs.getString("name"));
			account.setMoney(rs.getDouble("money"));
			
			return account;
		}
	}
} 

得到的查询结果

ccc
单条数据========Account [id=5, name=ccc, money=333333.0]
集合列表=======[Account [id=2, name=bbb, money=222222.0], Account [id=3, name=ccc, money=333333.0], Account [id=4, name=ccc, money=333333.0], Account [id=5, name=ccc, money=333333.0], Account [id=7, name=李四, money=240000.0], Account [id=8, name=hhh, money=20000.0], Account [id=9, name=hhh, money=20000.0], Account [id=10, name=eee, money=555555.0], Account [id=11, name=eee, money=555555.0], Account [id=12, name=eee, money=555555.0], Account [id=13, name=张三, money=230000.0], Account [id=14, name=张三, money=230000.0], Account [id=15, name=张三, money=230000.0], Account [id=16, name=张三, money=230000.0], Account [id=17, name=张三, money=230000.0], Account [id=18, name=张三, money=230000.0], Account [id=19, name=张三, money=230000.0], Account [id=20, name=张三, money=230000.0]]
个数=======18
基于遗传算法的新的异构分布式系统任务调度算法研究(Matlab代码实现)内容概要:本文档围绕基于遗传算法的异构分布式系统任务调度算法展开研究,重点介绍了一种结合遗传算法的新颖优化方法,并通过Matlab代码实现验证其在复杂调度问题中的有效性。文中还涵盖了多种智能优化算法在生产调度、经济调度、车间调度、无人机路径规划、微电网优化等领域的应用案例,展示了从理论建模到仿真实现的完整流程。此外,文档系统梳理了智能优化、机器学习、路径规划、电力系统管理等多个科研方向的技术体系与实际应用场景,强调“借力”工具与创新思维在科研中的重要性。; 适合人群:具备一定Matlab编程基础,从事智能优化、自动化、电力系统、控制工程等相关领域研究的研究生及科研人员,尤其适合正在开展调度优化、路径规划或算法改进类课题的研究者; 使用场景及目标:①学习遗传算法及其他智能优化算法(如粒子群、蜣螂优化、NSGA等)在任务调度中的设计与实现;②掌握Matlab/Simulink在科研仿真中的综合应用;③获取多领域(如微电网、无人机、车间调度)的算法复现与创新思路; 阅读建议:建议按目录顺序系统浏览,重点关注算法原理与代码实现的对应关系,结合提供的网盘资源下载完整代码进行调试与复现,同时注重从已有案例中提炼可迁移的科研方法与创新路径。
【微电网】【创新点】基于非支配排序的蜣螂优化算法NSDBO求解微电网多目标优化调度研究(Matlab代码实现)内容概要:本文提出了一种基于非支配排序的蜣螂优化算法(NSDBO),用于求解微电网多目标优化调度问题。该方法结合非支配排序机制,提升了传统蜣螂优化算法在处理多目标问题时的收敛性和分布性,有效解决了微电网调度中经济成本、碳排放、能源利用率等多个相互冲突目标的优化难题。研究构建了包含风、光、储能等多种分布式能源的微电网模型,并通过Matlab代码实现算法仿真,验证了NSDBO在寻找帕累托最优解集方面的优越性能,相较于其他多目标优化算法表现出更强的搜索能力和稳定性。; 适合人群:具备一定电力系统或优化算法基础,从事新能源、微电网、智能优化等相关领域研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①应用于微电网能量管理系统的多目标优化调度设计;②作为新型智能优化算法的研究与改进基础,用于解决复杂的多目标工程优化问题;③帮助理解非支配排序机制在进化算法中的集成方法及其在实际系统中的仿真实现。; 阅读建议:建议读者结合Matlab代码深入理解算法实现细节,重点关注非支配排序、拥挤度计算和蜣螂行为模拟的结合方式,并可通过替换目标函数或系统参数进行扩展实验,以掌握算法的适应性与调参技巧。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值