Spring是EE开发的一站式框架,有EE开发的每一层模板
一.JDBC模板的使用入门
1.创建数据库和表
SHOW DATABASES
USE hero
SHOW TABLES
CREATE TABLE account (id INT PRIMARY KEY AUTO_INCREMENT,NAME VARCHAR(20),money DOUBLE)
SELECT * FROM account
2.创建JDBC模板
public static void main(String[]args){
//创建连接池
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql:///hero");
dataSource.setUsername("root");
dataSource.setPassword("123456");
//创建JDBC模板
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.update("insert into account values(null,?,?)","张三",1000.0);
}
二.将连接池和模板交给spring管理
1.在spring.xml中进行配置
<!--配置连接池-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!--属性注入-->
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql:///hero"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<!--配置spring的JDBC的模板-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
2.编写配置测试类
@Resource(name="jdbcTemplate")
private JdbcTemplate jdbcTemplate;
public static void main(String[]args){
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
JdbcTemplate jdbcTemplate = (JdbcTemplate) applicationContext.getBean("jdbcTemplate");
jdbcTemplate.update("insert into account values(null,?,?)","lisi",4000.0);
}
三.使用c3p0
<!--配置C3P0-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///hero"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
</bean>
四.抽取配置到属性文件
1.在resources目录下创建配置文件 jdbc.properties
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///hero
jdbc.username=root
jdbc.password=123456
2.在spring.xml中进行配置
<!--引入属性文件-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<!--配置C3P0-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--配置spring的JDBC的模板-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
五.使用JDBC完成增删改查的功能
1.增
jdbcTemplate.update("insert into account values(null,?,?)","zhaoliu",4000.0);
2.删
jdbcTemplate.update("delete from account where id=?",7);
3.改
jdbcTemplate.update("update account set name=?,money=? where id=?","anny",3000,4);
4.查
查询某个属性
String name = jdbcTemplate.queryForObject("select name from account where id=?",String.class,6);
System.out.println(name);