Spring JDBC的開發(附代碼鏈接)

最後附有網盤鏈接(程序打包+數據庫)

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");
	} 
 
}

諸位有問題留言老鐵們,留言。

鏈接

鏈接:https://pan.baidu.com/s/1_8Hs6V06GdX7ia5fYV4MlQ
提取碼:7s8u

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章