1. JdbcTemplate的基本使用:
@Test
public void test1() throws PropertyVetoException {
//创建数据源对象
ComboPooledDataSource dataSource = new ComboPooledDataSource();
//加载配置文件
ResourceBundle rb = ResourceBundle.getBundle("jdbc");
String driver = rb.getString("driver");
String url = rb.getString("url");
String username = rb.getString("username");
String password = rb.getString("password");
dataSource.setDriverClass(driver);
dataSource.setJdbcUrl(url);
dataSource.setUser(username);
dataSource.setPassword(password);
//根据数据源创建JdbcTemplate对象
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
//执行数据库操作
int row = jdbcTemplate.update("insert into users values(?,?,?)", 5,"tom", "1234");
System.out.println(row);
}
配置文件
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
username=root
password=******
2.Spring JdbcTemplate
applicationContext.xml
<!-- 加载配置文件(别忘加context命名空间)-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<!--数据源对象-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driver}"></property>
<property name="jdbcUrl" value="${url}"></property>
<property name="user" value="${user}"></property>
<!--这里有个坑,不能使用${username},因为在这里spring使用username指示计算机的用户名-->
<property name="password" value="${password}"></property>
</bean>
<!--jdbc模板对象-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
测试:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class JdbcTemplateTest {
@Autowired
private JdbcTemplate jdbcTemplate;
@Test
public void testinsert(){
int row = jdbcTemplate.update("insert into users values (?,?,?)", null,"zzj", "123");
System.out.println(row);
}
@Test
public void testUpdate(){
int row = jdbcTemplate.update("update users set name=? where id=?", "zzk", "2");
System.out.println(row);
}
@Test
public void testDelete(){
int row = jdbcTemplate.update("delete from users where id =?", "2");
System.out.println(row);
}
@Test
public void testQuery(){
List<User> res = jdbcTemplate.query("select * from users", new BeanPropertyRowMapper<User>(User.class));
System.out.println(res);
}
@Test
public void testQueryforObject(){
User user = jdbcTemplate.queryForObject("select * from users where id=?", new BeanPropertyRowMapper<>(User.class), "1");
System.out.println(user);
}
@Test
public void testCount(){
Long sum = jdbcTemplate.queryForObject("select count(*) from users", Long.class);
System.out.println(sum);
}
}
3.Spring 事务控制
3.1编程式事务控制
相关对象
3.2基于XML的声明式事务控制
<!--配置平台事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--通知,事务的增强-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*"/>
<tx:method name="transfer" isolation="READ_COMMITTED" propagation="REQUIRED" timeout="-1" read-only="false"/>
</tx:attributes>
</tx:advice>
<!--配置事务的aop织入-->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.service.Impl.*.*(..))"></aop:advisor>
</aop:config>
3.3基于注解的声明式事务控制
需要在applicationContext.xml中添加的配置
<!--配置平台事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--事务的注解驱动-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>