SSM-4-JDBC
jdbc配置文件模板
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd"> <bean id="dataSource" class= "org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://192.168.134.128:3306/spring" /> <property name="username" value="root" /> <property name="password" value="root" /> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource" /> </bean> <bean id="accountDao" class="com.itheima.jdbc.AccountDaoImpl"> <property name="jdbcTemplate" ref="jdbcTemplate" /> </bean> </beans>
|
jdbc Template常用方法
- execute
- update
- query
execute
直接执行语句
update
- int update(String sql)
- int update(PreparedStatementCreator psc)
- int update(String sql, PreparedStatementSetter pss)
- int update(String sql,Object… args)
update后返回影响的记录条数
query
- List query(String sql, RowMapper rowMapper)
- List query (String sql, PreparedStatementSetter pss, RowMapper rowMapper )
- List query ( String sql, ObjectD args, RowMapper rowMapper)
举例说明
定义Account
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| package com.itheima.jdbc;
public class Account { private Integer id; private String username; private Double balance;
public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }
public String getUsername() { return username; }
public void setUsername(String username) { this.username = username; }
public Double getBalance() { return balance; }
public void setBalance(Double balance) { this.balance = balance; }
public String toString() { return "Account [id=" + id + ", " + "username=" + username + ", balance=" + balance + "]"; } }
|
定义Dao层接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| package com.itheima.jdbc;
import java.util.List;
public interface AccountDao { public int addAccount(Account account);
public int updateAccount(Account account);
public int deleteAccount(int id);
public Account findAccountById(int id);
public List<Account> findAllAccount(); }
|
定义实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| package com.itheima.jdbc;
import java.util.List;
import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper;
public class AccountDaoImpl implements AccountDao { private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(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 updateAccount(Account account) { String sql = "update account set username=?,balance=? where id = ?"; Object[] params = new Object[] { account.getUsername(), account.getBalance(), account.getId() }; int num = this.jdbcTemplate.update(sql, params); 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 findAccountById(int id) { String sql = "select * from account where id = ?"; RowMapper<Account> rowMapper = new BeanPropertyRowMapper<Account>(Account.class); return this.jdbcTemplate.queryForObject(sql, rowMapper, id); }
public List<Account> findAllAccount() { String sql = "select * from account"; RowMapper<Account> rowMapper = new BeanPropertyRowMapper<Account>(Account.class); return this.jdbcTemplate.query(sql, rowMapper); }
}
|
测试类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
| package com.itheima.jdbc;
import java.util.List;
import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.jdbc.core.JdbcTemplate;
public class JdbcTemplateTest {
@Test public void mainTest() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); JdbcTemplate jdTemplate = (JdbcTemplate) applicationContext.getBean("jdbcTemplate"); jdTemplate.execute("create table account(" + "id int primary key auto_increment," + "username varchar(50)," + "balance double)"); System.out.println("账户表account创建成功!"); }
@Test public void addAccountTest() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); AccountDao accountDao = (AccountDao) applicationContext.getBean("accountDao"); Account account = new Account(); account.setUsername("tom"); account.setBalance(1000.00); int num = accountDao.addAccount(account); if (num > 0) { System.out.println("成功插入了" + num + "条数据!"); } else { System.out.println("插入操作执行失败!"); } }
@Test public void updateAccountTest() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); AccountDao accountDao = (AccountDao) applicationContext.getBean("accountDao"); Account account = new Account(); account.setId(1); account.setUsername("tom"); account.setBalance(2000.00); int num = accountDao.updateAccount(account); if (num > 0) { System.out.println("成功修改了" + num + "条数据!"); } else { System.out.println("修改操作执行失败!"); } }
@Test public void deleteAccountTest() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); AccountDao accountDao = (AccountDao) applicationContext.getBean("accountDao"); int num = accountDao.deleteAccount(1); if (num > 0) { System.out.println("成功删除了" + num + "条数据!"); } else { System.out.println("删除操作执行失败!"); } }
@Test public void findAccountByIdTest() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); AccountDao accountDao = (AccountDao) applicationContext.getBean("accountDao"); Account account = accountDao.findAccountById(1); System.out.println(account); }
@Test public void findAllAccountTest() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); AccountDao accountDao = (AccountDao) applicationContext.getBean("accountDao"); List<Account> account = accountDao.findAllAccount(); for (Account act : account) { System.out.println(act); } } }
|
本文作者:NoOne
本文地址: https://noonegroup.xyz/posts/58aa25c9/
版权声明:转载请注明出处!