spring----JdbcTemplate的使用----笔记

jdbc的配置文件
用于在spring 的配置文件里使用

initialSize=5
maxIdle=50
minIdle=5
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/student?useSSL=false
jdbcusername=root

springbean配置文件



<?xml version="1.0" encoding="UTF-8"?>
<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"
	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-4.3.xsd">

	<context:property-placeholder location="classpath:c3p0.properties"/>
	
	<bean id="dataSources"  class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="initialPoolSize" value="${initialSize}"></property>
		
		<property name="maxPoolSize" value="${maxIdle}"></property>
		<property name="minPoolSize" value="${minIdle}"></property>
		
		<property name="driverClass" value="${driverClassName}"></property>
		<property name="jdbcUrl" value="${url}"></property>
		<property name="user" value="${jdbcusername}"></property>
	</bean>
		

	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<constructor-arg name="dataSource" ref="dataSources"></constructor-arg>
	
	</bean>
	
</beans>

封装一条记录

package top.demo.tempjdbc;

public class Student {

	private int id;
	private String name;
	private String idCar;
	private String home;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public Student() {
		super();
	}
	public String getName() {
		return name;
	}
	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", idCar=" + idCar + ", home=" + home + "]";
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getIdCar() {
		return idCar;
	}
	public void setIdCar(String idCar) {
		this.idCar = idCar;
	}
	public String getHome() {
		return home;
	}
	public void setHome(String home) {
		this.home = home;
	}
	public Student(String name, String home) {
		super();
		this.name = name;
		this.home = home;
	}
	
	
}

封装dao
由于跟dbutils等工具使用没什么太大区别就不一一封装了 毕竟测试 封装也没意义

package top.demo.tempjdbc;

import javax.swing.tree.RowMapper;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

public class StudentDao {
	
	
	private JdbcTemplate jdbc;
	
	public StudentDao(JdbcTemplate jdbc) {	
		this.jdbc=jdbc;	
	}
	
	/*
	 * +-----------+-------------+------+-----+---------+----------------+
| Field     | Type        | Null | Key | Default | Extra          |
+-----------+-------------+------+-----+---------+----------------+
| id_index  | int(11)     | NO   | PRI | NULL    | auto_increment |
| name      | varchar(10) | YES  |     | NULL    |                |
| id_car    | int(11)     | YES  |     | NULL    |                |
| home_addr | varchar(30) | YES  |     | NULL    |                |
+-----------+-------------+------+-----+---------+----------------+
	 * 
	 * 
	 * */
	
	public int insert(Student st) {
		
		String sql="insert into studentinfo (name,id_car,home_addr) value (?,?,?)";
		
		if(st.getHome()==null||st.getHome().equals("")) st.setHome("杭州");
		if(st.getName()==null||st.getName().equals("")) st.setName("test");
		if(st.getIdCar()==null||st.getIdCar().equals("")) st.setIdCar("123456789");
		int res=jdbc.update(sql,st.getName(),st.getIdCar(),st.getHome() );
		
		return res;
	}
	
	public Student getOne(int id) {
		
		String sql="select id_index as id,name,id_car as idCar,home_addr as home from studentinfo where id_index=?";
		
		BeanPropertyRowMapper<Student> rowMapper=new <Student>BeanPropertyRowMapper(Student.class);
		
		Student student=jdbc.queryForObject(sql, rowMapper,id);
		
		return student;
	}
	
	

}

测试

package top.demo.tempjdbc;



import java.sql.SQLException;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;



public class MAIN {

	public static void main(String argv[]) throws SQLException {
		
		ApplicationContext app=new ClassPathXmlApplicationContext("testtempjdbc.xml");
		
		JdbcTemplate jdbc=(JdbcTemplate) app.getBean("jdbcTemplate");
		
		/*
		 * 报错org.springframework.dao.DataAccessException 
		 * 导入spring-tx...jar包即可
		 * 
		 * */
		
		StudentDao dao=new StudentDao(jdbc);
		
		//int res=dao.insert(new Student("他","杭州"));
		//System.out.println(res);
		//输出1
		
		System.out.println(dao.getOne(1));
		//Student [id=1, name=1, idCar=123, home=中国]

	}
	
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值