除了Spring自帶的Jar包,還要下載幾個Jar包,這裏我吐槽下CSDN,下個Jar包都要積分,真是想錢想瘋了,其實能夠Google獲得java
我把全部依賴的Jar包貼出來mysql
MySql數據庫的搭建和使用我就不說了,百度便可。如今的MySQL Workbench挺好用的spring
咱們先來看下sql
普通的JDBC的寫法數據庫
import org.junit.Test; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DriverManagerDataSource; public class JdbcDemo1 { @Test public void demo1() { //建立鏈接池
DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql:///spring_database"); dataSource.setUsername("root"); dataSource.setPassword("1234"); //建立jdbc模板
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); jdbcTemplate.update("insert into account values (null,?,?)", "大傻", 10000d); } }
下面是SpringJDBC寫法apache
public class Account { private Integer id; private String name; private Double money; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Double getMoney() { return money; } public void setMoney(Double money) { this.money = money; } @Override public String toString() { return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}'; } }
配置文件微信
applicationContext6.xmlapp
<?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.xsd">
<!--引入屬性文件-->
<!--第一種方式:經過bean標籤(不多用)-->
<!-- <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:jdbc.properties"/> </bean>-->
<!--第二種方式:經過context標籤引入-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--配置Spring內置的鏈接池-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!--屬性注入-->
<property name="driverClassName" value="${jdbc.driverClass}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--配置DBCP鏈接池-->
<!-- <bean id="dataSource1" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driverClass}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean>-->
<!--配置C3P0鏈接池-->
<!--<bean id="dataSource2" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClass}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean>-->
<!--配置Spring的JDBC的模板-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
jdbc.propertieside
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///spring_database
jdbc.username=root
jdbc.password=1234
執行this
import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import javax.annotation.Resource; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext6.xml") public class SpringJDBC { @Resource(name = "jdbcTemplate") private JdbcTemplate jdbcTemplate; @Test //保存操做
public void demo() { jdbcTemplate.update("insert into account value (null,?,?)", "大靜", 20000d); } @Test //修改操做
public void demo1() { jdbcTemplate.update("update account set name = ?,money = ? where id = ?", "dbcp", 3000d, 4); } @Test //刪除操做
public void demo2() { jdbcTemplate.update("delete from account where id = ?", 6); } @Test //查詢操做
public void demo3() { String name = jdbcTemplate.queryForObject("select name from account where id = ?", String.class, 2); System.out.println("name=" + name); } @Test //統計個數
public void demo4() { Long count = jdbcTemplate.queryForObject("select count(*) from account", Long.class); System.out.println("count=" + count); } @Test //封裝到一個對象
public void demo5() { Account account = jdbcTemplate.queryForObject("select * from account where id = ?", new MyRowMapper(), 5); System.out.println(account); } @Test //查詢多條記錄
public void demo6() { List<Account> list = jdbcTemplate.query("select * from account", new MyRowMapper()); for (Account account : list) { System.out.println(account); } } class MyRowMapper implements RowMapper<Account> { @Override public Account mapRow(ResultSet resultSet, int i) throws SQLException { Account account = new Account(); account.setId(resultSet.getInt("id")); account.setName(resultSet.getString("name")); account.setMoney(resultSet.getDouble("money")); return account; } } }
Spring除了能夠集成JDBC模板,還能夠集成DBCP或者C3P0模板
執行結果就是在MySQL Workbench中查看
歡迎關注個人微信公衆號:安卓圈