文章目录
一、JdbcTemplate(概念和准备)
1、什么是JdbcTemplate
Spring框架对JDBC进行封装,使用JdbcTemplate方便实现对数据库操作
2、准备工作
(1)引入相关jar包或在Maven导入依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.22</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.22</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.8</version>
</dependency>
(2)在spring配置文件配置数据库连接池
<!--数据库连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<property name="url" value="jdbc:mysql://localhost:3306/user_db?useSSL=false&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
</bean>
(3)配置JdbcTemplate对象,注入DataSource
<!--JdbcTemplate对象-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!--注入dataSourde-->
<property name="dataSource" ref="dataSource"></property>
</bean>
(4)创建service类,创建dao类,在dao注入jdbcTemplate对象
@Service
public class BookService {
//注入dao
@Autowired
private BookDao bookDao;
}
//BookDao接口
public interface BookDao {
}
@Repository
public class BookDaoImpl implements BookDao{
//注入JdbcTemplate
@Autowired
private JdbcTemplate jdbcTemplate;
}
二、JdbcTemplate操作数据库(增删改)
1、对应数据库创建实体类
public class User {
private int userId;
private String userName;
private String ustatus;
//Getter和Setter方法
...
}
2、编写service和dao
(1)在dao进行数据库添加操作
//bookDao接口
public interface BookDao {
//添加信息
public void add(Book book);
//修改信息
public void update(Book book);
//删除信息
public void delete(int id);
}
//业务逻辑层
@Service
public class BookService {
//注入dao
@Autowired
private BookDao bookDao;
//添加的方法
public void addBook(Book book){
bookDao.add(book);
}
//修改的方法
public void updateBook(Book book){
bookDao.update(book);
}
//删除的方法
public void deleteBook(int id){
bookDao.delete(id);
}
}
(2)调用JdbcTemplate对象里面update方法实现添加操作
有两个参数
- 第一个参数:sql语句
- 第二个参数:可变参数,设置sql语气值
//dao接口实现类
@Repository
public class BookDaoImpl implements BookDao{
//注入JdbcTemplate
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void add(Book book) {
//1、创建sql语句insert into t_book values(?,?,?)
String sql = "insert into user_db.t_book values(?,?,?)";
//2、调用方法实现
Object[] args = {book.getUserId(), book.getUserName(), book.getUstatus()};
int update = jdbcTemplate.update(sql,args);
System.out.println(update);
}
@Override
public void update(Book book) {
String sql = "update user_db.t_book set user_name=?,ustatus=? where user_id=?";
Object[] args = {book.getUserName(), book.getUstatus(),book.getUserId()};
int update = jdbcTemplate.update(sql, args);
System.out.println(update);
}
@Override
public void delete(int id) {
String sql = "delete from user_db.t_book where user_id=?";
int update = jdbcTemplate.update(sql, id);
System.out.println(update);
}
}
3、测试类
//增加信息测试
@Test
public void MyTest(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
BookService bookService = context.getBean("bookService", BookService.class);
Book book = new Book();
book.setUserId(1);
book.setUserName("java");
book.setUstatus("a");
bookService.addBook(book);
}
//修改信息测试
@Test
public void MyTest02(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
BookService bookService = context.getBean("bookService", BookService.class);
Book book = new Book();
book.setUserId(1);
book.setUserName("酷小亚");
book.setUstatus("abc");
bookService.updateBook(book);
}
//删除信息测试
@Test
public void MyTest03(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
BookService bookService = context.getBean("bookService", BookService.class);
bookService.deleteBook(1);
}
三、JdbcTemplate操作数据库(查询)
1、对应数据库创建实体类
public class User {
private int userId;
private String userName;
private String ustatus;
//Getter和Setter方法
...
//toString 方法
}
2、编写service和dao
(1)在dao进行数据库添加操作
//bookDao接口
public interface BookDao {
//查询信息
void select(int id);
}
//业务逻辑层
@Service
public class BookService {
//注入dao
@Autowired
private BookDao bookDao;
//查询的方法
public void selectBook(int id){
bookDao.select(id);
}
}
(2)调用JdbcTemplate对象里面queryForObject方法实现添加操作
//dao接口实现类
@Repository
public class BookDaoImpl implements BookDao{
//注入JdbcTemplate
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void select(int id) {
String sql = "select * from user_db.t_book where user_id = ?";
Book book = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Book>(Book.class), id);
System.out.println(book);
}
}
3、测试类
//增加信息测试
@Test
public void MyTest04(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
BookService bookService = context.getBean("bookService", BookService.class);
bookService.selectBook(2);
}