Spring-jdbcの使用(jdbcTemplate対象は超詳細解説を使用)


Springプロジェクトを作成しました.
自分も仕事中に出会いました.以前使っていたmybatisとはずいぶん違います.自分の記憶のために、他の初心者と勉強するために、このブログを書きました.
1.jarパッケージ依存

	
	
		org.springframework
		spring-context
		4.3.5.RELEASE
	
	
	
		mysql
		mysql-connector-java
		5.1.35
	
	
	
		junit
		junit
		4.11
		test
	
	
	
		org.springframework
		spring-tx
		4.3.5.RELEASE
	
	
	
		org.springframework
		spring-expression
		4.3.5.RELEASE
	
	
	
		org.springframework
		spring-jdbc
		4.3.5.RELEASE
	

	
		org.springframework
		spring-webmvc
		4.3.5.RELEASE
	

	
	
		commons-dbcp
		commons-dbcp
		1.4
	

	
	
		org.slf4j
		slf4j-api
		1.7.25
	

2.データテーブルの作成
CREATE TABLE `account` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(50) DEFAULT NULL,
  `balance` double DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
3.データ接続設定propertiesファイルの内容
url=jdbc:mysql://localhost:3306/spring?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
driver=com.mysql.jdbc.Driver
username=root
password=123
initialSize=2
maxActive=10
4.Spring容器、xmlファイル配置


	
	
	
	
	

	
	
	

		

		

		

		

		

		

	

	
		
	


5.Acceountエンティティクラスを作成する
	public class Account {
		private Integer id;   //  ID
		private String userName;//   
		private Double balance;//    
		...  get  set  ,  toString()
		}
### 6.  dao   
	public interface AccountDao {
		//  
		public int addAccount(Account account);
		
		//  
		public int update(Account account);
		
		//  
		public int deleteAccount(int id);
		
		//  
		public Account findById(int id);
		
		//    
		public List findAll();
	}
### 7.  dao      
public class AccountDaoImpl implements AccountDao{
	//    jdbcTemplate
	@Autowired
	private JdbcTemplate jdbcTemplate;
	//   JdbcTemplate     setter  
	public void setJdbcTeplate(JdbcTemplate jdbcTemplate){
		this.jdbcTemplate=jdbcTemplate;
	}
	//    
	public int addAccount(Account account) {
		String sql="insert into account(username,balance) value(?,?)";
		Object[] obj=new Object[]{
				account.getUserName(),
				account.getBalance(),
		};
		//      
		int num=this.jdbcTemplate.update(sql, obj);
		return num;
	}
	//    
	public int update(Account account) {
		String sql="update account set username=?,balance=? where id=?";
		Object[] obj=new Object[]{
				account.getUserName(),
				account.getBalance(),
				account.getId()
		};
		int num = this.jdbcTemplate.update(sql,obj);
		return num;
	}
	//    
	public int deleteAccount(int id) {
		String sql="delete from account where id=?";
		int num = this.jdbcTemplate.update(sql,id);
		return num;
	}
	//    
	public Account findById(int id) {
		String sql="select * from account where id=?";
		/*jdbcTemplate queryForList          
		 *     List  ,       
		 *                     
		 */
		List> row=jdbcTemplate.queryForList(sql, id);
		System.out.println(row);
		Account account=new Account();
		if(row.size()>0){
			Map map=row.get(0);
			account.setId(id);
			account.setUserName((String) map.get("username"));
			account.setBalance((Double) map.get("balance"));
		}
		
		return account;
	}
	//      
	public List findAll() {
		String sql="select * from account";
		List> rows=jdbcTemplate.queryForList(sql);
		return rows;
	}

}
6.制御層
削除の変更を追加
追加したコントロール層は、削除と変更を省略します.
public class AddAccount {
	public static void main(String args[]){
		//      
		ApplicationContext applicationContext=
				new ClassPathXmlApplicationContext("dao.xml");
		//        
		AccountDaoImpl accountDaoImpl=(AccountDaoImpl)applicationContext.getBean("accountDaoImpl");
		Account account=new Account();
		account.setUserName("  ");
		account.setBalance(10000.00);
		int num=accountDaoImpl.addAccount(account);
		if(num>0){
			System.out.println("     "+num+"   !");
		}else{
			System.out.println("        !");
		}
		
	}
}
7.IDで対応するアカウントを検索する
public class FindById {
	public static void main(String args[]){
		ApplicationContext applicationContext=
				new ClassPathXmlApplicationContext("dao.xml");
		AccountDaoImpl accountDaoImpl=(AccountDaoImpl)applicationContext.getBean("accountDaoImpl");
		int id=2;
		Account account=accountDaoImpl.findById(id);
		
		System.out.println(account);
		
	}
}
8.すべてのアカウントを照会する
public class FindAll {
	public static void main(String args[]){
		ApplicationContext applicationContext=
				new ClassPathXmlApplicationContext("dao.xml");
		AccountDaoImpl accountDaoImpl=(AccountDaoImpl)applicationContext.getBean("accountDaoImpl");
		int id=1;
		List rows=accountDaoImpl.findAll();
		//     
		for(int i=0;i
この例は最適化されていません.この上で最適化処理ができます.高級操作は次のページをご参照ください.