We can focus on the author's account, focus on learning from scratch notes Spring Collection. We can also learn from a catalog visit the author's blog garden blog. The film documents the learning and the sharing of information on employment-based programmers dark horse class video, and record notes and their own views. Welcome to learn and discuss.
[Notes] Spring Spring learn from scratch learning route
Spring is a one-stop EE development framework, EE has developed solutions for each layer. Spring's persistence layer also provides a solution: ORM and JDBC module template.
Spring provides a lot of persistence layer technology simplifies the programming template class:
Getting JDBC template used
Step 1: Create web project, the introduction of the jar package
in addition to the introduction of Spring's six basic jar package and text packages and aop package, also need to introduce starter pack and spring of the mysql jdbc package and tx
Libs can find in the file after the download, unzip the folder ago, Baidu also uploaded to the cloud, and download. In the first download link of this anthology series [notes] Spring4 Spring learn from scratch learning route to find.
Step 2: Create a database and table
create database spring4_day03;
use spring4_day03;
create table account (
id int primary key auto_increment,
name varchar (20),
money double
);
The third step: to save the data using JDBC template
example
package com.tyust.jdbc.demo1;
import org.junit.Test;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
public class JdbcDemo1 {
@Test
//jdbc模板的使用类似于Dbutils.
public void demo1() {
//创建连接池
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUrl("jdbc:mysql:///spring4_day03");
ds.setUsername("root");
ds.setPassword("root");
//创建jdbc模板
JdbcTemplate jdbcTemplate = new JdbcTemplate(ds);
jdbcTemplate.update("insert into account values (null,?,?)","张飞",10000d);
}
}
The connection pooling and templates to manage Spring
Example
configuration file
<bean id = "dmds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="DriverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql:///spring4_day03"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dmds"></property>
</bean>
Test category
package com.tyust.jdbc.demo1;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class JdbcDemo2 {
@Resource(name = "jt")
private JdbcTemplate jt;
@Test
public void demo1() {
jt.update("insert into account values (null,?,?)","刘备",210000d);
}
}
Output
The use of open source database connection pool
1.DBCP use
The first step: the introduction of the jar package
Libs can find in the file after the download, unzip the folder ago, Baidu also uploaded to the cloud, and download. In the first download link of this anthology series [notes] Spring4 Spring learn from scratch learning route to find.
Step Two: Configure DBCP connection pool
<!-- 配置dbcp连接池 -->
<bean id = "ds" class="org.apache.commons.dbcp.BasicDataSource">
<property name="DriverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql:///spring4_day03"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
2.C3P0 use
The first step: the introduction of the jar package
Libs can find in the file after the download, unzip the folder ago, Baidu also uploaded to the cloud, and download. In the first download link of this anthology series [notes] Spring4 Spring learn from scratch learning route to find.
Step Two: C3P0 connection pool configuration
<!-- 配置C3p0连接池 -->
<bean id="ds" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///spring4_day03"/>
<property name="user" value="root"/>
<property name="password" value="root"></property>
</bean>
The introduction of external properties file
The first step: new-> file
define a property file
Step Two: In the Spring configuration file, the introduction of property files
<!-- 引入属性文件 -->
<!-- 第一种方式通过一个bean标签引入(很少使用) -->
<!-- <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"></property>
</bean> -->
<!-- 第二种方式通过context标签引入 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置C3p0连接池 -->
<bean id="ds" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"></property>
</bean>
CRUD template operations
@Test
// 修改操作
public void demo2() {
jt.update("update account set name = ? ,money= ? where id = ?", "何巨涛", 2000d, 7);
}
@Test
// 删除操作
public void demo3() {
jt.update("delete from account where id = ?", 6);
}
@Test
// 查询操作:
public void demo4() {
String name = jt.queryForObject("select name from account where id = ?", String.class, 5);
System.out.println(name);
}
@Test
// 统计查询
public void demo5() {
Long count = jt.queryForObject("select count(*) from account", Long.class);
System.out.println(count);
}
@Test
// 封装到一个对象中
public void demo6() {
Account account = jt.queryForObject("select * from account where id = ?", new MyRowMapper(), 5);
System.out.println(account);
}
@Test
// 查询多条记录
public void demo7() {
List<Account> list = jt.query("select * from account", new MyRowMapper());
for (Account account : list) {
System.out.println(account);
}
}
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.setMoney(rs.getDouble("money"));
account.setName(rs.getString("name"));
return account;
}
}