一、引言
在学习事务之前呢,首先我们需要能操作数据库,这样才能好演示事务的作用。
druid这个是阿里巴巴的数据库连接池,为什么要使用连接池呢?为了减少频繁的数据库连接操作,消耗性能,这里小编就不过多的解释,而且阿里巴巴这款连接池还能很好的进行监控等等功能,确实很强大!!!
Jdbctemplate则是Spring框架给我们提供的,连接数据库最底层的就是JDBC,而Spring对此进行了封装,然后提供给我们用,接下来,试试对数据库进行操作试试。
二、配置druid和Jdbctemplate
引入相对应的jar包先,spring相关的jar小编就不贴出来了。
<!-- 阿里巴巴连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.16</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<!-- JdbcTemplate -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.1.3.RELEASE</version>
</dependency>
如果掌握了Bean的配置,以下配置相信大伙都能看懂,小编就不多说了。
<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-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--引入外部属性文件-->
<context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
<!--配置阿里巴巴连接池,这个是最基本的连接数据库配置-->
<bean id="dataSource" destroy-method="close" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--配置JdbcTemplate,并且指定数据源-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
db.properties 属性配置文件,一般来说数据库连接配置都是存放在属性配置文件里面的,以下是最基本的配置。
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/taotao?useUnicode=true&characterEncoding=utf8&useSSL=false
jdbc.username=root
jdbc.password=123456
三、实现增删改查
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import java.util.*;
public class Main {
private JdbcTemplate jdbcTemplate;
{
BeanFactory beanFactory = new ClassPathXmlApplicationContext("applicationContext-shiwu.xml");
jdbcTemplate = (JdbcTemplate)beanFactory.getBean("jdbcTemplate");
}
/**
* 查询
*/
@Test
public void selectDatas(){
String sql = "select * from tb_user where id = ?";
RowMapper<TbUser> rowMapper = new BeanPropertyRowMapper<TbUser>(TbUser.class);
TbUser resultList = jdbcTemplate.queryForObject(sql,rowMapper,43);
System.out.println(resultList.toString());
}
/**
* 修改
*/
@Test
public void updateDatas(){
String sql = "update tb_user set username = ? where id = ?";
Integer result = jdbcTemplate.update(sql,"隔壁老王",40);
System.out.println(result);
}
/**
* 新增
*/
@Test
public void insertdatas(){
String sql = "insert into tb_user(username,password,created,updated) VALUES (?,?,?,?)";
List<Object[]> args = new ArrayList<Object[]>();
args.add(new Object[]{"奥特曼","123321",new Date(),new Date()});
int[] result = jdbcTemplate.batchUpdate(sql,args);
System.out.println(result[0]);
}
}