Spring操作JdbcTemplate数据库的方法学习

网友投稿 258 2022-07-27


目录Spring操作JdbcTemplate一、准备工作1. 引入依赖2. 配置文件中配置数据库连接池3. 配置 JdbcTemplate 对象4. dao 中注入 JdbcTemplate 对象二、操作数据库1. 创建对应实体类2. 编写service 和 dao3. 编写测试

Spring操作JdbcTemplate

spring 对 jdbc 做了封装,就是 JdbcTemplate,可以让操作数据库更加方便。

一、准备工作

1. 引入依赖

在之前的基础上,再引入这些依赖。

2. 配置文件中配置数据库连接池

外部文件 jdbc.properties:

prop.driverClass=com.mysql.jdbc.Driver

prop.url=jdbc:mysql://localhost:3306/userDb

prop.username=root

prop.password=123456

配置文件引入:

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="hcCGQQHKcYttp://springframework.org/schema/context"

xmlns:aop="http://springframework.org/schema/aop"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="hcCGQQHKcYttp://springframework.org/schema/context"

xmlns:aop="http://springframework.org/schema/aop"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop.xsd">

3. 配置 JdbcTemplate 对象

注入 DataSource。

... ...

... ...

4. dao 中注入 JdbcTemplate 对象

创建 dao,在里面注入 JdbcTemplate 。

@Repository

public class BookDaoImpl implements BookDao {

http:// // 注入 jdbcTemplate

@Autowired

private JdbcTemplate jdbcTemplate;

}

创建 service 类,在里面注入 dao。

@Service

public class BookService {

// 注入dao

@Autowired

private BookDao bookDao;

}

二、操作数据库

以添加为例。

建一个很简单的表,里面有 3 个字段。

1. 创建对应实体类

创建数据表对应的实体类,并且生成 3 个属性的 get、set方法。

public class Book {

private String userId;

private String username;

private String userStatus;

public String getUserId() {

return userId;

}

public void setUserId(String userId) {

this.userId = userId;

}

... ...

2. 编写service 和 dao

service

@Service

public class BookService {

// 注入dao

@Autowired

private BookDao bookDao;

public void addBook(Book book) {

bookDao.add(book);

}

}

dao 的实现类。

@Repository

public class BookDaoImpl implements BookDao {

// 注入 jdbcTemplate

@Autowired

private JdbcTemplate jdbcTemplate;

@Override

public void add(Book book) {

String sql = "insert into t_book values (?, ?, ?)";

int result = jdbcTemplate.update(sql, book.getUserId(), book.getUsername(), book.getUserStatus());

System.out.println(result);

}

}

使用 jdbcTemplate.update() 方法进行添加,第一个参数是 sql,第二个不定长参数,成功则返回 1。

3. 编写测试

public class TestBook {

@Test

public void testJdbc() {

ApplicationContext context =

new ClassPathXmlApplicationContext("bean1.xml");

BookService bookService = context.getBean("bookService", BookService.class);

Book book = new Book();

book.setUserId("1");

book.setUsername("ceshi");

book.setUserStatus("3");

bookService.addBook(book);

}

}

运行结果:

八月 05, 2021 10:35:15 下午 com.alibaba.druid.pool.DruidDataSource info

信息: {dataSource-1} inited

1

Process finished with exit code 0

查看数据表

成功添加。

删除跟修改操作跟上面类似了,不再演示。

以上就是Spring操作JdbcTemplate数据库方法学习的详细内容,更多关于Spring操作JdbcTemplate的资料请关注我们其它相关文章!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java异常架构和异常关键字图文详解(Java异常结构)
下一篇:java.nio.file.WatchService 实时监控文件变化的示例代码
相关文章

 发表评论

暂时没有评论,来抢沙发吧~