说明
仅供学习交流使用,
笔记作于【尚硅谷Spring5框架教程(idea版)-哔哩哔哩】
四、JdbcTemplate
JdbcTemplate(概念和准备)
1、什么是JdbcTemplate
(1)Spring框架对JDBC进行封装,使用JdbcTemplate方便实现对数据库操作
2、准备工作
(1)引入相关jar包
druid.jar
mysql驱动.jar
spring-jdbc-5.3.10.jar
spring-tx-5.3.10.jar
spring-orm-5.3.10.jar
(2)在spring配置文件配置数据库连接池
src/bean1/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" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 组件扫描 --> <context:component-scan base-package="com.atguigu"></context:component-scan> <!-- 数据库连接池 --> <!-- 配置连接池 --> <!-- DruidDataSource dataSource = new DruidDataSource(); --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close" > <property name="url" value="jdbc:mysql://localhost:3306/user_db?serverTimezone=UTC"/> <property name="username" value="root"/> <property name="password" value="root"/> <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/> </bean> <!-- JdbcTemplate对象 --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <!--注入dataSource--> <property name="dataSource" ref="dataSource"></property> </bean> </beans>
(3)配置JdbcTemplate对象,注入DataSource
bean.xml
<!-- JdbcTemplate对象 --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <!-- 注入DataSource--> <property name="dataSource" ref="dataSource"></property> </bean>
(4)创建service类,创建dao类,在dao注入JdbcTemplate对象
bean1.xml
<!-- 开启组件扫描--> <context:component-scan base-package="com.atguigu"></context:component-scan>
spring5/新建service包/新建BookService类
package com.atguigu.spring5.service; import com.atguigu.spring5.dao.BookDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class BookService { //注入dao @Autowired private BookDao bookDao; }
spring5/新建dao包/新建BookDao接口
package com.atguigu.spring5.dao; public interface BookDao{ }
dao包/新建BookDaoImpl类
package com.atguigu.spring5.dao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; @Repository public class BookDapImpl implements BookDao{ //注入JdbcTemplate @Autowired private JdbcTemplate jdbcTemplate; }
JdbcTemplate 操作数据库(添加)
1、对应数据库创建实体类
spring5/新建entity包/新建User类
package com.atguigu.spring5.entity; public class User { private String userId; private String username; private String ustatus; public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getUstatus() { return ustatus; } public void setUstatus(String ustatus) { this.ustatus = ustatus; } }
2、编写service和dao
(1)在dao进行数据库添加的操作
dao/新建UserDao
package com.atguigu.spring5.dao; import com.atguigu.spring5.entity.User; public interface UserDao { //添加的方法 public void add(User user); }
dao/新建UserDaoImpl
package com.atguigu.spring5.dao; import com.atguigu.spring5.entity.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; @Repository public class UserDapImpl implements UserDao{ //注入JdbcTemplate @Autowired private JdbcTemplate jdbcTemplate; //添加的方法 @Override public void add(User user) { //1.创建sql语句 String sql="insert into t_user values (?,?,?)"; //2.调用方法实现 Object[] args={user.getUserId(), user.getUsername(), user.getUstatus()}; int update = jdbcTemplate.update(sql,args); System.out.println(update); } }
service/新建UserService
package com.atguigu.spring5.service; import com.atguigu.spring5.dao.UserDao; import com.atguigu.spring5.entity.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserService { //注入dao @Autowired private UserDao userDao; //添加的方法 public void addUser(User user){ userDao.add(user); } }
(2)调用JdbcTemplate对象里面的update方法实现添加操作
jdbcTemplate.update(String sql,Object... args);
- 有两个参数
- 第一个参数:sql语句
- 第二个参数:可变参数,设置sql语句值
spring5/新建test包/新建TestUser类
package com.atguigu.spring5.test; import com.atguigu.spring5.entity.User; import com.atguigu.spring5.service.UserService; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestUser { @Test public void testJdbcTemplate(){ ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml"); UserService userService = context.getBean("userService", UserService.class); User user=new User(); user.setUserId("1"); user.setUsername("java"); user.setUstatus("a"); userService.addUser(user); } }