本次博客学习用spring进行链接数据库的增删改查。
项目结构:
安装mysql数据库:很多人的博客园或者csdn都有压缩包版和exe安装版就不在叙述。
可视化数据库软件navicat安装和破解:https://www.jianshu.com/p/42a33b0dda9c
Account.java
1 package com.itheima.jdbc; 2 3 public class Account { 4 private Integer id; 5 private String username; 6 private Double balance; 7 @Override 8 public String toString() { 9 return "Account [id=" + id + ", username=" + username + ", balance=" + balance + "]"; 10 } 11 12 public Integer getId() { 13 return id; 14 } 15 public void setId(Integer id) { 16 this.id = id; 17 } 18 public String getUsername() { 19 return username; 20 } 21 public void setUsername(String username) { 22 this.username = username; 23 } 24 public Double getBalancce() { 25 return balance; 26 } 27 public void setBalancce(Double balancce) { 28 this.balance = balancce; 29 } 30 31 32 33 }
AccountDao.java
1 package com.itheima.jdbc; 2 3 import java.util.List; 4 5 public interface AccountDao { 6 public int addAccount(Account account); 7 public int updateAcount(Account account); 8 public int deleteAccount(int id); 9 public Account findAccountById(int id); 10 public List<Account> listAllAccounts(); 11 }
AccountDaoImpl.java
1 package com.itheima.jdbc; 2 3 import java.util.List; 4 5 import org.springframework.jdbc.core.BeanPropertyRowMapper; 6 import org.springframework.jdbc.core.JdbcTemplate; 7 import org.springframework.jdbc.core.RowMapper; 8 9 public class AccountDaoImpl implements AccountDao { 10 11 //声明JdbcTemplate属性及setter方法 12 private JdbcTemplate jdbcTemplate; 13 14 15 public JdbcTemplate getJdbcTemplate() { 16 return jdbcTemplate; 17 } 18 19 public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { 20 this.jdbcTemplate = jdbcTemplate; 21 } 22 23 @Override 24 public int addAccount(Account account) { 25 // TODO Auto-generated method stub 26 27 String sqlString="insert into account (username,balance)value(?,?)"; 28 Object[] objects=new Object[] { 29 account.getUsername(), 30 account.getBalancce() 31 }; 32 int num=this.jdbcTemplate.update(sqlString, objects); 33 return num; 34 } 35 36 @Override 37 public int updateAcount(Account account) { 38 // TODO Auto-generated method stub 39 String sqlString="update account set username=?,balance=?where id=?"; 40 Object[] objects=new Object[] { 41 account.getUsername(), 42 account.getBalancce(), 43 account.getId() 44 }; 45 int num=this.jdbcTemplate.update(sqlString, objects); 46 return num; 47 } 48 49 @Override 50 public int deleteAccount(int id) { 51 // TODO Auto-generated method stub 52 String sqlString="delete from account where id=?"; 53 int num=this.jdbcTemplate.update(sqlString,id); 54 return num; 55 } 56 57 @Override 58 public Account findAccountById(int id) { 59 // TODO Auto-generated method stub 60 String sqString="select * from account where id=?"; 61 // 创建一个BeanPropertyRowMapper 62 RowMapper<Account>rowMapper=new BeanPropertyRowMapper<Account>(Account.class); 63 // 将id绑定到sql语句中,通过RowMapper返回一个object返回单行记录 64 65 return this.jdbcTemplate.queryForObject(sqString, rowMapper,id); 66 } 67 68 @Override 69 public List<Account> listAllAccounts() { 70 // TODO Auto-generated method stub 71 String sqString="select * from account "; 72 RowMapper<Account>rowMapper=new BeanPropertyRowMapper<Account>(Account.class); 73 74 return this.jdbcTemplate.query(sqString, rowMapper); 75 } 76 77 }
applicationContext.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans 5 http://www.springframework.org/schema/beans/spring-beans-4.3.xsd"> 6 <!-- 配置数据源 --> 7 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 8 <!-- 数据库驱动 --> 9 <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> 10 <!-- 链接数据库的url --> 11 <property name="url" value="jdbc:mysql://localhost:3306/spring?useUnicode=true&characterEncoding=utf-8"></property> 12 <!-- 链接数据库的用户名 --> 13 <property name="username" value="root"></property> 14 <!-- 链接数据库的密码 --> 15 <property name="password" value="123456"></property> 16 </bean> 17 <!-- 配置jdbc模板 --> 18 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 19 <property name="dataSource" ref="dataSource"></property> 20 </bean> 21 22 <bean id="accountDao" class="com.itheima.jdbc.AccountDaoImpl"> 23 <!-- 将JdbcTemplate注入到accountDao中 --> 24 <property name="jdbcTemplate" ref="jdbcTemplate"></property> 25 </bean> 26 </beans>
JdbcTemplateTest.java
1 package com.itheima.jdbc; 2 3 import java.util.List; 4 5 import javax.sound.midi.VoiceStatus; 6 7 import org.junit.Test; 8 import org.springframework.context.ApplicationContext; 9 import org.springframework.context.support.ClassPathXmlApplicationContext; 10 import org.springframework.jdbc.core.JdbcTemplate; 11 12 public class JdbcTemplateTest { 13 14 15 16 @Test 17 public void mainTest() { 18 ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml"); 19 JdbcTemplate jdbcTemplate=(JdbcTemplate) applicationContext.getBean("jdbcTemplate"); 20 jdbcTemplate.execute("create table account("+ 21 "id int primary key auto_increment,"+ 22 "username varchar(50),"+ 23 "balance double)"); 24 System.out.println("数据库表创建成功"); 25 } 26 27 @Test 28 public void addAccountTest() { 29 ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml"); 30 AccountDao accountDao=(AccountDao) applicationContext.getBean("accountDao"); 31 Account account=new Account(); 32 account.setUsername("tom"); 33 account.setBalancce(1000.00); 34 int num=accountDao.addAccount(account); 35 if(num>0) { 36 System.out.println("插入了"+num+"条数据"); 37 }else { 38 System.out.println("fail"); 39 } 40 } 41 42 @Test 43 public void deleteAccountTest() { 44 ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml"); 45 AccountDao accountDao=(AccountDao) applicationContext.getBean("accountDao"); 46 int num=accountDao.deleteAccount(1); 47 if (num>0) { 48 System.out.println("删除成功"); 49 }else { 50 System.out.println("删除失败"); 51 } 52 } 53 54 @Test 55 public void findAccountByIdTest() { 56 ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml"); 57 AccountDao accountDao=(AccountDao)applicationContext.getBean("accountDao"); 58 Account account=accountDao.findAccountById(2); 59 System.out.println(account); 60 } 61 @Test 62 public void listAllAccount() { 63 ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml"); 64 AccountDao accountDao=(AccountDao)applicationContext.getBean("accountDao"); 65 List<Account> accounts=accountDao.listAllAccounts(); 66 for(Account account:accounts) { 67 System.out.println(account); 68 } 69 } 70 71 }
实验中,我的url必须这么写:jdbc:mysql://localhost:3306/spring?useUnicode=true&characterEncoding=utf-8否则会报错。