JdbcTemplate操作数据库demo

1.JdbcTemplate操作数据库

1、什么是 JdbcTemplate
(1)Spring 框架对 JDBC 进行封装,使用 JdbcTemplate 方便实现对数据库操作

2、准备工作
(1)引入相关 jar 包
以下jar包都添加
在这里插入图片描述

(2)在 spring 配置文件配置数据库连接池

url=jdbc:mysql://localhost:3306/book?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT&useSSL=FALSE
user=root
password=root
driveClassName=com.mysql.cj.jdbc.Driver
initialSize=10
maxActive=10

注意:
左边名称可以随意命名,但是username不可使用,properties 中不能用 username 作为变量,这种方式会注入自己的系统环境变量的 用户名

serverTimezone=GMT GMT(Greenwich Mean Time):格林威治标准时间
MySQL驱动中默认时区是UTC,与本地时间(中国)相差八个小时,所以链接不上.加上serverTimezone=GMT可解决

5.0版本的则是 com.mysql.jdbc.Driver,在使用8.0版本驱动Driver的value值应该写为 com.mysql.cj.jdbc.Driver

useSSL=true/false,是否进行ssl连接
SSL(Secure Sockets Layer 安全套接字协议),在mysql进行连接的时候,如果mysql的版本是5.7之后的版本必须要加上useSSL=false,mysql5.7以及之前的版本会默认为false。一般情况下都是使用useSSL=false,尤其是在将项目部署到linux上时,一定要使用useSSL=false,useSSL=true是进行安全验证,一般通过证书或者令牌什么的,useSSL=false就是通过账号密码进行连接。

characterEncoding=UTF-8作用是指定所处理字符的解码和编码的格式。若项目的字符集和MySQL数据库字符集设置为同一字符集则url可以不加此参数。

(3)把外部 properties 属性文件引入到 spring 配置文件中

  • 引入 context 名称空间
  <!-- 数据库连接池 -->
    <!--引入外部属性文件-->
    <context:property-placeholder location="classpath:druid.properties"/>
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${driveClassName}"></property>
        <property name="url" value="${url}"></property>
        <property name="username" value="${user}"></property>
        <property name="password" value="${password}"></property>
        <property name="initialSize" value="${initialSize}"></property>
        <property name="maxActive" value="${maxActive}"></property>
    </bean>

(4)配置 JdbcTemplate 对象,注入 DataSource

<!-- JdbcTemplate 对象 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <!--注入 dataSource-->
        <property name="dataSource" ref="dataSource"></property>
    </bean>

(5)创建 service 类,创建 dao 类,在 dao 注入 jdbcTemplate 对象
注解需要引入context名称空间

<!--组件扫描-->
<context:component-scan base-package="spring5"></context:component-scan>
<?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 http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--组件扫描-->
    <context:component-scan base-package="spring5"></context:component-scan>
   
    <!-- 数据库连接池 -->
    <!--引入外部属性文件-->
    <context:property-placeholder location="classpath:druid.properties"/>
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${driveClassName}"></property>
        <property name="url" value="${url}"></property>
        <property name="username" value="${user}"></property>
        <property name="password" value="${password}"></property>
        <property name="initialSize" value="${initialSize}"></property>
        <property name="maxActive" value="${maxActive}"></property>
    </bean>
    <!-- JdbcTemplate 对象 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <!--注入 dataSource-->
        <property name="dataSource" ref="dataSource"></property>
    </bean>
</beans>

(6)创建 service 类,创建 dao 类,在 BookDaoImpldao 注入 jdbcTemplate 对象,在BookService注入BookDaoImpl对象

public interface BookDao {
    void add(User user);
}
@Repository
public class BookDaoImpl implements BookDao {
    //注入 JdbcTemplate
    @Autowired
    private JdbcTemplate jdbcTemplate;
}

@Service
public class BookService  {
    //注入 dao
    @Autowired
    private BookDao bookDao;
}

JdbcTemplate 操作数据库(添加)
1、对应数据库创建实体类

public class User {
    private Integer id;
    private String username;
    private String password;
    private String email;
    public User() {
    }
    public User(Integer id, String username, String password, String email) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.email = email;
    }
    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 String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
}

2、编写 service 和 dao
(1)在 dao 进行数据库添加操作,调用 JdbcTemplate 对象里面 update 方法实现添加操作

@Repository
public class BookDaoImpl implements BookDao {
    //注入 JdbcTemplate
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public void add(User user) {
        //update()实现对数据库中数据的增删改
        String sql = "insert into t_user(username,password,email) values (?,?,?)";
        int update = jdbcTemplate.update(sql, user.getUsername(), user.getPassword(), user.getEmail());
        System.out.println("影响条数:"+update);
    }
}


@Service
public class BookService  {
    //注入 dao
    @Autowired
    private BookDao bookDao;
    //添加方法
    public void addBook(User user){
        bookDao.add(user);
    }
}

3、测试类

public class BookDaoTest {
    @Test
    public void add() {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        BookService bookService = context.getBean("bookService", BookService.class);
        System.out.println(bookService);
        bookService.addBook(new User(null,"tina2","tina","tian2qq.com"));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值