Spring JDBC的开发
相比于传统的Jdbc实现,在Jdbc API的基础上封装了一套实现JdbcTemplate,JdbcTemplate的优点如下:
(1)配置基于模板设置
(2)完成了资源的创建和释放的工作
(3)完成了对JDBC的核心流程的工作,包括SQL语句的创建和执行,简化了对JDBC的操作
(4)仅需要传递DataSource就可以把它实例化
(5)JdbcTemplate只需要创建一次,减少了代码复用的烦恼
(6)JdbcTemplate是线程安全类
1、jar包引入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
//这引得是spring-starter-jdbc包
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.21</version>
</dependency>
//连接池
2、DB.properties文件
该文件在resources文件夹下
mysql_driver=com.mysql.cj.jdbc.Driver
mysql_url=jdbc:mysql://localhost:3306/final_hotel_system?serverTimezone=UTC
mysql_username=root
mysql_passwd=root
这种文件其实体现了一种思想,设想如果数据库的相关配置信息出了问题,一般的使用人员很难处理,如果用这种方式,只需在相关的properties文件中修改即可,简易,便于实用。
3、xml文件配置
该文件命名为applicationContext-jdbc.xml在resources文件夹下面。
<context:property-placeholder location="DB.properties"></context:property-placeholder>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${mysql_driver}"></property>
<property name="url" value="${mysql_url}"></property>
<property name="userName" value="${mysql_username}"></property>
<property name="password" value="${mysql_passwd}"></property>
</bean>
//这个是datasource的bean
//以上这个是datasource的bean
<bean id="jdbcTemplete" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" value="#{dataSource}"></property>
</bean>
//以上这个是jdbcTemplate 的bean
<context:component-scan base-package="com.zzxtit.aop.jdbc"></context:component-scan>
//以上这个是自动扫描
4、包内具体代码
package com.zzxtit.aop.jdbc;
public interface UserDao {
public void insertUserInfor(UserInfor us);
public UserInfor getUserInforById(int userId);
public void deleteUserInforById(int i);
public void updatePasswordById(int userId, String newPassword);
}
//看见interface接口,
package com.zzxtit.aop.jdbc;
public class UserInfor {
private int userId;
private String password;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "UserInfor [userId=" + userId + ", password=" + password + "]";
}
public UserInfor() {
super();
}
public UserInfor(int userId, String password) {
super();
this.userId = userId;
this.password = password;
}
}
//以上添加信息类
package com.zzxtit.aop.jdbc;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
@Repository
public class UserDaoImol implements UserDao{
@Autowired
private JdbcTemplate jdbcTemplate;
public void insertUserInfor(UserInfor us) {
String sql="insert into user_infor(user_id,password) "
+ "values(?,?)";
jdbcTemplate.update(sql, us.getUserId(), us.getPassword());
}
public UserInfor getUserInforById(int userId) {
String sql = "select * from user_infor where user_id = ?";
List<UserInfor> ui = jdbcTemplate.query(sql, new BeanPropertyRowMapper<UserInfor>(UserInfor.class), userId);
if(ui != null && ui.size() > 0) {
return ui.get(0);
}else {
return null;
}
}
public void deleteUserInforById(int userId) {
String sql = "delete from user_infor where user_id = ?";
jdbcTemplate.update(sql,userId);
}
public void updatePasswordById(int userId, String newPassword) {
String sql = "update user_infor set password = ? where user_id = ?";
jdbcTemplate.update(sql, newPassword,userId );
}
}
//以上是实现类
Main方法
package com.zzxtit.aop.jdbc;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/*
* 需要记录的操作
* 1、navicate 将操作转化为sql语句的方法[GIT]
* 2、报错记录 (1)使用cj.jdbc必须添加 时间戳 [git](2) 创建bean时默认使用无参构造方法[git]
* 3、添加数据类型的bean还是需要使用new的方法创建的
* 4、添加新方法时需要在接口添加以及实现类添加
* 5、使用jdbctemplate删除数据
*/
public class Main {
public static void main(String[] args) {
ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContest-jdbc.xml");
UserDao ud = ioc.getBean(UserDao.class);
UserInfor ui = ud.getUserInforById(123);
System.out.println(ui);
UserInfor ui1 = new UserInfor(1234,"1234");
ud.insertUserInfor(ui1);
ud.deleteUserInforById(1234);
ud.updatePasswordById(123, "321");
}
}
诸位有问题留言老铁们,留言。