赞
踩
为了在特定领域帮助我们简化代码,Spring 封装了很多 『Template』形式的模板类。例如:RedisTemplate、RestTemplate 等等,包括我们今天要学习的 JDBCTemplate。
①加入依赖
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.11.RELEASE</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.3</version> </dependency> <!-- Spring 持久化层支持jar包 --> <!-- Spring 在执行持久化层操作、与持久化层技术进行整合过程中,需要使用orm、jdbc、tx三个jar包 --> <!-- 导入 orm 包就可以通过 Maven 的依赖传递性把其他两个也导入 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>5.3.1</version> </dependency> <!-- 数据源 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.31</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.2.11.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies>
②jdbc.properties
jdbc.user=root
jdbc.password=root
jdbc.url=jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf8
jdbc.driver=com.mysql.jdbc.Driver
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="username" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="url" value="${jdbc.url}"></property>
</bean>
[2]配置 JDBCTemplate
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
[3]在测试类装配 JdbcTemplate
@RunWith(value = SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = "classpath:ApplicationContext.xml")
public class SpringJunitTest {
@Autowired
private JdbcTemplate jdbcTemplate;
}
@RunWith(value = SpringJUnit4ClassRunner.class) @ContextConfiguration(value = "classpath:ApplicationContext.xml") public class SpringJunitTest { @Autowired private JdbcTemplate jdbcTemplate; /** * 增 */ @Test public void testAdd(){ String sql="insert into user values('5','牛魔王','1995-02-12','男','火焰山')"; jdbcTemplate.update(sql); } /** * 删 */ @Test public void testDelete(){ String sql="delete from user where id =?"; jdbcTemplate.update(sql,5); } /** * 改 */ @Test public void testUpdate(){ String sql="update user set username=? where id=?"; jdbcTemplate.update(sql,"红孩儿",5); } /** * 查--查一个字段 */ @Test public void testQuery(){ String sql="select username from user where id=? "; String username = jdbcTemplate.queryForObject(sql, String.class, 5); System.out.println(username); } /** * 查一个实体类 */ @Test public void testObject(){ String sql="select * from user where id=?"; User user = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(User.class),5); System.out.println(user.toString()); } }
其中查询实体类是借助RowMapper 完成查询
需要创建一个实体类与要查询的表对应
public class User { private Integer id; private String username; private Date birthday; private String sex; private String address; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", birthday=" + birthday + ", sex='" + sex + '\'' + ", address='" + address + '\'' + '}'; } }
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。