spring 框架如何处理对数据库的操作呢?
文档:JdbcTemplate APIs : /spring-framework-5.3.8/docs/javadoc-api/index.html
JdbcTemplate 是 Spring 提供的访问数据库的技术。可以将 JDBC 的常用操作封装为模板方法
已经提供了特别多的 API
-- 创建数据库
CREATE DATABASE spring
USE spring
-- 创建表 monster
CREATE TABLE monster(
id INT PRIMARY KEY,
`name` VARCHAR(64) NOT NULL DEFAULT '',
skill VARCHAR(64) NOT NULL DEFAULT ''
)CHARSET=utf8
INSERT INTO monster VALUES(100, '青牛怪', '吐火');
INSERT INTO monster VALUES(200, '黄袍怪', '吐烟');
INSERT INTO monster VALUES(300, '蜘蛛怪', '吐丝');
jdbc.userName=root
jdbc.password=hsp
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring
配置 JdbcTemplate_ioc.xml,将数据源分配给 JdbcTemplate bean
@Test
public void addDataByJdbcTemplate() {
ApplicationContext ioc = new
ClassPathXmlApplicationContext("JdbcTemplate_ioc.xml");
//得到 JdbcTemplate bean
JdbcTemplate bean = ioc.getBean(JdbcTemplate.class);
// 1. 添加方式 1
// String sql = "INSERT INTO monster VALUES(400, '红孩儿', '枪法厉害')";
// bean.execute(sql);
//2. 添加方式 2, 绑定参数
String sql = "INSERT INTO monster VALUES(?, ?, ?)";
int affected = bean.update(sql, 700, "红孩儿 2", "枪法厉害 2");
System.out.println("add ok affected= " + affected);
}
@Test
public void updateDataByJdbcTemplate() {
ApplicationContext ioc = new
ClassPathXmlApplicationContext("JdbcTemplate_ioc.xml");
//得到 JdbcTemplate bean
JdbcTemplate bean = ioc.getBean(JdbcTemplate.class);
String sql = "UPDATE monster SET skill = ? WHERE id=?";
int affected = bean.update(sql, "美女计", 300);
System.out.println("affected= " + affected);
}
@Test
public void addBatchDataByJdbcTemplate() {
ApplicationContext ioc = new
ClassPathXmlApplicationContext("JdbcTemplate_ioc.xml");
JdbcTemplate bean = ioc.getBean(JdbcTemplate.class);//添加..
String sql = "INSERT INTO monster VALUES(?, ?, ?)";
List
@Test
public void selectDataByJdbcTemplate() {
ApplicationContext ioc = new
ClassPathXmlApplicationContext("JdbcTemplate_ioc.xml");
//得到 JdbcTemplate bean
JdbcTemplate bean = ioc.getBean(JdbcTemplate.class);
String sql = "SELECT id as monsterId,name,skill FROM monster WHERE id =?";
//下面这个 rowmapper 是一个接口,可以将查询的结果,封装到你指定的 Monster 对象中.
RowMapper rowMapper = new
BeanPropertyRowMapper(Monster.class);
Monster monster = bean.queryForObject(sql, rowMapper, 100);
System.out.println("monster= " + monster);
}
@Test
public void selectMulDataByJdbcTemplate() {
ApplicationContext ioc = new
ClassPathXmlApplicationContext("JdbcTemplate_ioc.xml");
//得到 JdbcTemplate bean
JdbcTemplate bean = ioc.getBean(JdbcTemplate.class);
String sql = "SELECT id as monsterId,name,skill FROM monster WHERE id >=?";
//下面这个 rowmapper 是一个接口,可以将查询的结果,封装到你指定的 Monster 对象中.
RowMapper rowMapper = new BeanPropertyRowMapper(Monster.class);
List monster_list = bean.query(sql, rowMapper, 200);
for (Monster monster : monster_list) {
System.out.println(monster);
}
}
@Test
public void selectScalarByJdbcTemplate() {
ApplicationContext ioc = new
ClassPathXmlApplicationContext("JdbcTemplate_ioc.xml");
//得到 JdbcTemplate bean
JdbcTemplate bean = ioc.getBean(JdbcTemplate.class);
String sql = "SELECT name FROM monster WHERE id =100";
String name = bean.queryForObject(sql, String.class);
System.out.println(name);
}
src\JdbcTemplate_ioc.xml, 增加配置
测试:使用 Map 传入具名参数完成操作
注:sql 语句里的属性名,要和 map_parameter 的 key 保持一致
@Test
public void testDataByNamedParameterJdbcTemplate() {
ApplicationContext ioc = new
ClassPathXmlApplicationContext("JdbcTemplate_ioc.xml");
//得到 NamedParameterJdbcTemplate bean
NamedParameterJdbcTemplate namedParameterJdbcTemplate =
ioc.getBean(NamedParameterJdbcTemplate.class);
String sql = "INSERT INTO monster VALUES(:my_id, :name, :skill)";
Map map_parameter = new HashMap();
map_parameter.put("my_id", 800);
map_parameter.put("name", "螃蟹精");
map_parameter.put("skill", "钳子无敌大法");
namedParameterJdbcTemplate.update(sql, map_parameter);
System.out.println("add data ok~");
}
注:这里的 sql 里的属性名称,要和 Monster bean 的属性名保持一致
@Test
public void operDataBySqlparametersoruce() {
ApplicationContext ioc = new
ClassPathXmlApplicationContext("JdbcTemplate_ioc.xml");
//得到 NamedParameterJdbcTemplate bean
NamedParameterJdbcTemplate namedParameterJdbcTemplate =
ioc.getBean(NamedParameterJdbcTemplate.class);
String sql = "INSERT INTO monster VALUES(:monsterId, :name, :skill)";
Monster monster = new Monster(900, "狐狸精", "狐媚之术");
SqlParameterSource source = new BeanPropertySqlParameterSource(monster);
namedParameterJdbcTemplate.update(sql, source);
System.out.println("add ok~");
}
MonsterDao.java
@Repository
public class MonsterDao {
@Autowired
private JdbcTemplate jdbcTemplate;
//添加 monster
public void save(Monster monster) {
String sql = "INSERT INTO monster VALUES(?, ?, ?)";
jdbcTemplate.update(sql, monster.getMonsterId(),
monster.getName(), monster.getSkill());
}
}
@Test
public void operDataByDao() {
ApplicationContext ioc =
new ClassPathXmlApplicationContext("JdbcTemplate_ioc.xml");
MonsterDao bean = ioc.getBean(MonsterDao.class);
Monster monster = new Monster(1000, "大虾精", "夹子功");
bean.save(monster);
}