目录
JdbcTemplate是类似org.apache.commons.dbutils.QueryRunner,用于操作数据库的模板,与QueryRunner一样,都需要配置DataSource
首先添加依赖:spring-context + spring-jdbc + druid + mysql-connector-java + junit(用于测试)
XML文件配置
pom.xml 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>jdbcTemplate01</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.18</version>
</dependency>
<!-- jdbcTemplate-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.18</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.9</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.27</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
</dependencies>
</project>
package com.qfedu.demo.model;
/**
* 模板类, 数据库jdbc01中的user表
*/
public class User {
private Integer id, age;
private String username, gender, hobby, address, grade;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", age=" + age +
", username='" + username + '\'' +
", gender='" + gender + '\'' +
", hobby='" + hobby + '\'' +
", address='" + address + '\'' +
", grade='" + grade + '\'' +
'}';
}
}
package com.qfedu.demo.dao;
import com.qfedu.demo.model.User;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
/**
* 操作类,用于操作数据库
*/
public class UserDao {
private JdbcTemplate jdbcTemplate;
//用于在applicationContext.xml配置
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
//添加数据
public Integer addUser(User user) {
return jdbcTemplate.update("insert into user(username,gender,age,address,grade,hobby) values (?,?,?,?,?,?)", user.getUsername(), user.getGender(), user.getAge(), user.getAddress(), user.getGrade(), user.getHobby());
}
//删除数据
public Integer deleteById(Integer id) {
return jdbcTemplate.update("delete from user where id=?", id);
}
//根据id查找数据
public User getUserById(Integer id) {
return jdbcTemplate.queryForObject("select * from user where id=?", new BeanPropertyRowMapper<>(User.class), id);
}
//获取所有数据
public List<User> getAllUser() {
return jdbcTemplate.query("select * from user", new BeanPropertyRowMapper<>(User.class));
}
}
package com.qfedu.demo.dao;
import com.qfedu.demo.model.User;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
/**
* 测试类
*/
public class UserDaoTest01 {
ClassPathXmlApplicationContext ctx = null;
UserDao userDao = null;
//每个Test执行前执行改方法
@Before
public void before() {
ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
userDao = ctx.getBean(UserDao.class);
}
//测试添加数据
@Test
public void Test01() {
User user = new User();
user.setUsername("lisi");
user.setGender("男");
user.setAge(22);
user.setAddress("广州");
user.setGrade("本科");
user.setHobby("玩");
Integer effRow = userDao.addUser(user);
System.out.println("effRow = " + effRow);
}
//测试删除数据
@Test
public void Test02() {
Integer effRow = userDao.deleteById(11);
System.out.println("effRow = " + effRow);
}
//测试根据id获取数据
@Test
public void Test03() {
User user = userDao.getUserById(10);
System.out.println("user = " + user);
}
//获取所有数据
@Test
public void Test04() {
List<User> users = userDao.getAllUser();
for (User user : users) {
System.out.println("user = " + user);
}
}
}
appliactionContext.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="db.properties"/>
<!--配置数据源-->
<bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
<property name="url" value="${db.url}"/>
<property name="username" value="${db.username}"/>
<property name="password" value="${db.password}"/>
</bean>
<!-- <bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">-->
<!-- <property name="url" value="jdbc:mysql:///jdbc01"/>-->
<!-- <property name="username" value="root"/>-->
<!-- <property name="password" value="123456"/>-->
<!-- </bean>-->
<!--配置jdbcTemplate,相当于设置QueryRunner-->
<bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置com.qfedu.demo.dao.UserDao.jdbcTemplate -->
<bean class="com.qfedu.demo.dao.UserDao" id="userDao">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
</beans>
Java代码配置
操作类
package com.qfedu.demo01;
import com.qfedu.demo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* 操作类,用于操作数据库
*
* xml配置时,将UserDao注入spring容器
* <bean class="com.qfedu.demo.UserDao" id="userDao">
* <property name="jdbcTemplate" ref="jdbcTemplate"/>
* </bean>
*/
//注入spring容器
@Repository
public class UserDao {
@Autowired
private JdbcTemplate jdbcTemplate;
//获取所有数据
public List<User> getAllUser() {
return jdbcTemplate.query("select * from user", new BeanPropertyRowMapper<>(User.class));
}
}
Java代码配置类
package com.qfedu.demo01;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
/**
* 配置类
*/
@Configuration //声明该类是配置类
@ComponentScan //包扫描,默认扫描本类所在包,@ComponentScan(basePackages = "com.qfedu.demo01")
public class JavaConfig {
/**
* 配置DataSource
* <bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
* <property name="url" value="${db.url}"/>
* <property name="username" value="${db.username}"/>
* <property name="password" value="${db.password}"/>
* </bean>
*/
//注册到spring
@Bean
DataSource dataSource() {
DruidDataSource ds = new DruidDataSource();
ds.setUrl("jdbc:mysql:///jdbc01");
ds.setUsername("root");
ds.setPassword("123456");
return ds;
}
/**
* 配置JdbcTemplate
* <bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate">
* <property name="dataSource" ref="dataSource"/>
* </bean>
*/
@Bean
JdbcTemplate jdbcTemplate() {
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(dataSource());
return jdbcTemplate;
}
}
测试类
package com.qfedu.demo01;
import com.qfedu.demo.User;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import java.util.List;
/**
* 测试类
*/
public class JavaConfigTest01 {
private AnnotationConfigApplicationContext ctx = null;
private UserDao userDao = null;
@Before
public void before() {
ctx = new AnnotationConfigApplicationContext(JavaConfig.class);
userDao = ctx.getBean(UserDao.class);
}
@Test
public void getAllUser() {
List<User> users = userDao.getAllUser();
for (User user : users) {
System.out.println("user = " + user);
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/14614.html