JdbcTemplate
2022-08-15 18:41:56 12 举报
AI智能生成
微服务框架之Spring
作者其他创作
大纲/内容
概述
它是spring框架中提供的一个对象,是对原始繁琐的Jdbc API对象的简单封装。spring框架为我们提供了很多的操作模板类。例如:操作关系型数据的JdbcTemplate和HibernateTemplate,操作nosql数据库的RedisTemplate,操作消息队列的JmsTemplate等等。<br>
开发步骤
导入spring-jdbc和spring-tx坐标
<!--导入spring的jdbc坐标--> <br><dependency><br> <groupId>org.springframework</groupId> <br><artifactId>spring-jdbc</artifactId> <br><version>5.0.5.RELEASE</version><br></dependency><br><!--导入spring的tx坐标--><br> <dependency> <br><groupId>org.springframework</groupId><br> <artifactId>spring-tx</artifactId><br> <version>5.0.5.RELEASE</version><br></dependency>
创建数据库表和实体
在数据库中创建实体,在实体类中创建实体
一般方式
创建JdbcTemplate对象执行数据库操作
//1、创建数据源对象<br>ComboPooledDataSource dataSource = new ComboPooledDataSource();<br>dataSource.setDriverClass("com.mysql.jdbc.Driver");<br>dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");<br>dataSource.setUser("root");<br>dataSource.setPassword("root");
//2、创建JdbcTemplate对象<br>JdbcTemplate jdbcTemplate = new JdbcTemplate();
//3、设置数据源给JdbcTemplate<br>jdbcTemplate.setDataSource(dataSource);
执行数据库操作
jdbcTemplate.update("insert into account values(?,?)","tom",5000);
Spring配置文件的方式
<!--数据源DataSource--> <br><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <br><property name="driverClass" value="com.mysql.jdbc.Driver"></property><br><property name="jdbcUrl" value="jdbc:mysql:///mybatis"></property> <br><property name="user" value="root"></property> <br><property name="password" value="root"></property><br></bean><br><!--JdbcTemplate--><br> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><br> <property name="dataSource" ref="dataSource"></property><br></bean>
@Test<br>public void testSpringJdbcTemplate() throws PropertyVetoException {<br>ApplicationContext applicationContext = new <br>ClassPathXmlApplicationContext("applicationContext.xml");<br>JdbcTemplate jdbcTemplate = applicationContext.getBean(JdbcTemplate.class);<br>jdbcTemplate.update("insert into account values(?,?)","lucy",5000);<br>}
Spring注解方式
@PropertySource("classpath:db.properties")
public class DataSourceConfiguration {
@Value("${driver}")
private String driver;
@Value("${url}")
private String url;
@Value("${user}")
private String username;
@Value("${password}")
private String password;
@Bean(name="dataSource")
public DataSource getDataSource() throws PropertyVetoException {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass(driver);
dataSource.setJdbcUrl(url);
dataSource.setUser(username);
dataSource.setPassword(password);
return dataSource;
}
// 产生JdbcTemplate的Bean对象,并且将dataSource数据源以参数的形式进行注入到JdbcTemplate的Bean对象中
@Bean("jdbcTemplate")
public JdbcTemplate returnJdbcTemplate(DataSource dataSource){
return new JdbcTemplate(dataSource);
}
}
<br>@Configuration<br>@ComponentScan({"com.dcs.dao","com.dcs.service"})<br>@Import({DataSourceConfiguration.class})<br>public class SpringConfig {<br><br>}
<br>@RunWith(SpringJUnit4ClassRunner.class)<br>@ContextConfiguration(classes = {SpringConfig.class})<br>public class SpringJunitTest {<br> @Autowired<br> private UserService userService;<br> @Autowired<br> private DataSource dataSource;<br> @Autowired<br> @Qualifier("jdbcTemplate")<br> private JdbcTemplate jdbcTemplate;<br> @Test<br> public void testUserService()throws Exception{<br> userService.save();<br> System.out.println(dataSource.getConnection());<br> }<br> @Test<br> public void testJdbc()throws Exception{<br> jdbcTemplate.update("insert into account values(?,?)","lcy",5000);<br><br> }<br> }
常用操作
更新操作:<br>
jdbcTemplate.update (sql,params)
示例
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml")
public class JdbcTemplateCRUDTest { @Autowired
private JdbcTemplate jdbcTemplate;
@Test
//测试修改操作
public void testUpdate(){
jdbcTemplate.update("update account set money=? where
name=?",1000,"tom");
} }
查询操作:<br>
一般查询:jdbcTemplate.query (sql,Mapper,params)<br>
示例
多个查询
@Test
public void testQueryAll(){
List<Account> accounts = jdbcTemplate.query("select * from account", new
BeanPropertyRowMapper<Account>(Account.class));
for (Account account : accounts) {
System.out.println(account.getName());
} }
单个查询
@Test
//测试查询单个对象操作
public void testQueryOne(){
Account account = jdbcTemplate.queryForObject("select * from account where
name=?", new BeanPropertyRowMapper<Account>(Account.class), "tom");
System.out.println(account.getName());
}
聚合查询:jdbcTemplate.queryForObject(sql,Mapper,params)
示例
//测试查询单个简单数据操作(聚合查询)
public void testQueryCount(){
Long aLong = jdbcTemplate.queryForObject("select count(*) from account",
Long.class);
System.out.println(aLong);
}
0 条评论
下一页