spring5.0学习笔记11

jdbc Template

package com.test11;

public class Stu {
	private Integer id;
	private String sname;
	private Float score;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public Float getScore() {
		return score;
	}
	public void setScore(Float score) {
		this.score = score;
	}
	public String getSname() {
		return sname;
	}
	public void setSname(String sname) {
		this.sname = sname;
	}
}
package com.test11;

import java.sql.DriverManager;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

/**
 * JdbcTemplate基本用法
 * @author Administrator
 *
 */
public class JdbcTemplateDemo1 {
	public static void main(String[] args) {
		//准备数据源,spring内置数据源
		DriverManagerDataSource ds = new DriverManagerDataSource();
		ds.setDriverClassName("com.mysql.jdbc.Driver");
		ds.setUrl("jdbc:mysql://localhost:3306/xiaonei");
		ds.setUsername("root");
		ds.setPassword("tingwei");
		//创建JdbcTemplate对象
		JdbcTemplate jt = new JdbcTemplate();
		//给jt设置数据源
		jt.setDataSource(ds);
		jt.execute("insert into stu(sname,score)values('ccc',60)"); //这里不要用60f
	}
}

三月 27, 2020 1:48:58 下午 org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName
信息: Loaded JDBC driver: com.mysql.jdbc.Driver

JdbcTemplate CRUD操作


<beans xmlns="http://www.springframework.org/schema/beans"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xmlns:aop="http://www.springframework.org/schema/aop" 
		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-2.5.xsd
				http://www.springframework.org/schema/context 
				http://www.springframework.org/schema/context/spring-context-2.5.xsd
				http://www.springframework.org/schema/tx 
				http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 
				http://www.springframework.org/schema/aop
				http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">	

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
	<property name="dataSource" ref="dataSource">property>
bean>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
	<property name="driverClassName" value="com.mysql.jdbc.Driver">property>
	<property name="url" value="jdbc:mysql://localhost:3306/xiaonei">property>
	<property name="username" value="root">property>
	<property name="password" value="tingwei">property>
bean>
beans>
package com.test11;

public class Stu {
	private Integer id;
	private String sname;
	private Float score;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public Float getScore() {
		return score;
	}
	public void setScore(Float score) {
		this.score = score;
	}
	public String getSname() {
		return sname;
	}
	public void setSname(String sname) {
		this.sname = sname;
	}
}
package com.test11;

import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * JdbcTemplate基本用法
 * @author Administrator
 *
 */
public class JdbcTemplateDemo1 {
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
		JdbcTemplate jt = (JdbcTemplate)ac.getBean("jdbcTemplate");
		
		//保存
		jt.execute("insert into stu(sname,score)values('eee',60)"); //这里不要用60f
		//更新
	//	String sql="update stu set sname=? score=? where id=?";
	//	jt.update(sql,"test",50,7);
		//删除
	//	jt.update("delete from stu where id=?",7);
		//查询所有
	//	List stus = jt.query("select * from stu", new StuRowMapper(),10f);
		//查询一个
		//查询返回一行一列
	}
}

class StuRowMapper implements org.springframework.jdbc.core.RowMapper{

	public Object mapRow(ResultSet arg0, int arg1) throws SQLException {
		// TODO Auto-generated method stub
		Stu stu = new Stu();
		stu.setId(arg0.getInt("id"));
		stu.setScore(arg0.getFloat("score"));
		stu.setSname(arg0.getString("sname"));
		return stu;
	}
	
}

三月 27, 2020 2:03:38 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@27bc2616: display name [org.springframework.context.support.ClassPathXmlApplicationContext@27bc2616]; startup date [Fri Mar 27 14:03:38 CST 2020]; root of context hierarchy
三月 27, 2020 2:03:38 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [bean.xml]
三月 27, 2020 2:03:38 下午 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@27bc2616]: org.springframework.beans.factory.support.DefaultListableBeanFactory@4d591d15
三月 27, 2020 2:03:38 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@4d591d15: defining beans [jdbcTemplate,dataSource]; root of factory hierarchy
三月 27, 2020 2:03:39 下午 org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName
信息: Loaded JDBC driver: com.mysql.jdbc.Driver
spring5.0学习笔记11_第1张图片

你可能感兴趣的:(spring教程)