概述:
开发步骤:![](https://i-blog.csdnimg.cn/blog_migrate/f17e7278bd28e7ea46e35263c81000ca.png)
基础方式:
第一步:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.2.1</version>
<!-- spring集成的jdbc包start-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
<!-- spring集成的jdbc包end-->
第二部:
public class JdbcTemplateTest {
@Test
public void test1() throws PropertyVetoException {
//创建数据源对象
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
dataSource.setUser("root");
dataSource.setPassword("123456");
//创建数据库表实体
JdbcTemplate jdbcTemplate = new JdbcTemplate();
//设置数据源
jdbcTemplate.setDataSource(dataSource);
//执行操作
jdbcTemplate.update("insert into tb_user values(?,?)","马云",100);
}
}
Spring产生JdbcTemplate对象方式:
配置:
<!-- 配置数据源对象-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<!-- jdbc模板对象-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
使用:
@Test
public void test2() throws PropertyVetoException {
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
JdbcTemplate jdbcTemplate = app.getBean(JdbcTemplate.class);
//执行操作
jdbcTemplate.update("insert into tb_user values(?,?)","马化腾",100);
}
分离配置信息到.properties文件
jdbc.properties文件内容:
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/test
jdbc.username = root
jdbc.password = 123456
配置:
<?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
">
<!-- 加载properties配置文件 需要context声明-->
<context:property-placeholder location="jdbc.properties"/>
<!-- 配置数据源对象-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- jdbc模板对象-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
使用和上面一样。
数据库操作的使用:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class JdbcTemplateCRUTDTest {
@Autowired
private JdbcTemplate jdbcTemplate;
//更新
@Test
public void test1(){
int update = jdbcTemplate.update("update tb_user set age=? where name =?", 12, "马云");
System.out.println(update);
}
//查询多个 自动封装成对象
@Test
public void test2(){
List<User> users = jdbcTemplate.query("select * from tb_user",new BeanPropertyRowMapper<User>(User.class));
System.out.println(users);
}
//查询单个 自动封装成对象
@Test
public void test3(){
User user = jdbcTemplate.queryForObject("select * from tb_user where name=?",new BeanPropertyRowMapper<User>(User.class),"马云");
System.out.println(user);
}
//聚合查询,总人数
@Test
public void tes4(){
long count = jdbcTemplate.queryForObject("select count(*) from tb_user",long.class);//指定返回的类型
System.out.println(count);
}
//删除
@Test
public void test5(){
int update = jdbcTemplate.update("delete from tb_user where name =?", "马云");
System.out.println(update);
}
//添加
@Test
public void test6(){
int update = jdbcTemplate.update("insert into tb_user values(?,?)", "马云",100);
System.out.println(update);
}
}