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