什么是JdbcTemplate
不引入其它框架,Spring框架对jdbc操作的封装,使用JdbcTemplate对数据库的操作
JdbcTemplate的使用
导入依赖
<?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>spring-study-again</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.23</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
</dependencies>
</project>
数据库连接信息
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/study_spring_again?useSSL=false&serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=123456
配置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"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--引入数据库配置信息 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--配置数据库连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<property name="url" value="${jdbc.url}"/>
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--创建JdbcTemplate对象-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!--注入数据库连接池-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!--开启注解扫描-->
<context:component-scan base-package="com.jdbc"/>
</beans>
@AllArgsConstructor
@Data
@NoArgsConstructor
public class Book {
private int bookId;
private String bookName;
private int deleteStatus;
}
public interface BookMapper {
int insertBook(Book book);
}
@Repository
public class BookMapperImpl implements BookMapper{
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public int insertBook(Book book) {
String sql="insert into book values(?,?,?)";
Object[] args = {book.getBookId(),book.getBookName(),book.getDeleteStatus()};
return jdbcTemplate.update(sql,args);
}
}
public interface BookService {
int insertBook(Book book);
}
@Service(value = "bookService")
public class BookServiceImpl implements BookService{
@Autowired
private BookMapper bookMapper;
@Override
public int insertBook(Book book) {
return bookMapper.insertBook(book);
}
}
@Test
public void testJdbc() {
ApplicationContext context = new ClassPathXmlApplicationContext("TestBean1.xml");
BookService bookService = context.getBean("bookService", BookService.class);
int insertBook = bookService.insertBook(new Book(3, "spring入门到入土", 0));
System.out.println(insertBook);
}