1、声明并创建要使用的接口方法
package spring.dao;
import java.util.List;
import spring.model.TableA;
/**
* @author jangle
* @email jangle@jangle.xyz
* @time 2022年10月6日 下午2:15:59
*
*/
public interface AccountDao {
/**
* 使用preparedStatement 进行查询
*
* @author jangle
* @time 2022年10月9日 下午2:02:34
* @param name
* @return
*/
public List<TableA> find(String name);
}
2、实现这个声明的接口(核心代码)
这里使用了模板方法,对查询过程进行了封装。
package spring.dao.impl;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreatorFactory;
import org.springframework.jdbc.core.RowMapper;
import spring.dao.AccountDao;
import spring.model.TableA;
/**
* @author jangle
* @email jangle@jangle.xyz
* @time 2022年10月6日 下午2:18:34
*
*/
public class AccountDaoJdbcImpl implements AccountDao {
private JdbcTemplate jdbcTemplate;
@Override
public List<TableA> find(String name) {
// 1.首先创建PreparedStatementCreatorFactory工厂
PreparedStatementCreatorFactory psCreatorFactory =
new PreparedStatementCreatorFactory("select * from table_a where name = ?",
new int[] {Types.VARCHAR});
return jdbcTemplate.query(psCreatorFactory.newPreparedStatementCreator(new Object[] {name}),
new RowMapper<TableA>() {
@Override
public TableA mapRow(ResultSet rs, int rowNum) throws SQLException {
TableA a = new TableA();
a.setId(rs.getLong("id"));
a.setName(rs.getString("name"));
a.setAge(rs.getLong("age"));
a.setAddress(rs.getString("address"));
a.setBalance(rs.getLong("balance"));
return a;
}
});
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
}
3、编写spring的配置文件
在文件中添加数据源,配置刚定义的bean。
package spring.m4_2_2;
/**
* @author jangle
* @email jangle@jangle.xyz
* @time 2022年10月7日 上午7:14:02
*
*/
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import spring.dao.AccountDao;
import spring.dao.impl.AccountDaoJdbcImpl;
@Configuration
public class Configuration4_2_2 {
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/demo?characterEncoding=UTF8");
dataSource.setUsername("jangle_demo");
dataSource.setPassword("1");
return dataSource;
}
@Bean
public JdbcTemplate jdbcTemplate() {
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(dataSource());
return jdbcTemplate;
}
@Bean
public AccountDao accountDao() {
AccountDaoJdbcImpl accountDao = new AccountDaoJdbcImpl();
accountDao.setJdbcTemplate(jdbcTemplate());
return accountDao;
}
}
4、运行主程序测试代码
package spring.m4_3_4;
import java.util.List;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import spring.dao.AccountDao;
import spring.m4_2_2.Configuration4_2_2;
import spring.model.TableA;
/**
*
* 在jdbcTemplate中使用prepareStatement
* @author jangle
* @email jangle@jangle.xyz
* @time 2022年10月9日 下午2:19:38
*
*/
public class M4_3_4 {
@SuppressWarnings("resource")
public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext =
new AnnotationConfigApplicationContext(Configuration4_2_2.class);
AccountDao dao = applicationContext.getBean(AccountDao.class);
List<TableA> list = dao.find("名称");
System.out.println(list.size());
for(TableA a:list) {
System.out.println(a.getId());
System.out.println(a.getName());
}
}
}
5、运行结果
2
1
名称
2
名称
附、数据库表结构:
CREATE TABLE `table_a` (
`id` INT(10) UNSIGNED NOT NULL,
`name` VARCHAR(50) NOT NULL DEFAULT '' COLLATE 'utf8_general_ci',
`age` INT(11) NULL DEFAULT NULL,
`address` VARCHAR(50) NULL DEFAULT NULL COLLATE 'utf8_general_ci',
`balance` INT(11) NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
)