Spring数据库使用
这是我的第一篇csdn博客,主要是拿来做学习记录使用的,原理部分我不会重点讲述,主要是记录学习中遇到的一些问题以及解决方案。
碰巧今天下午没课,于是乎我就按照课本写了一下午的spring程序,毕竟也是刚刚接触spring,遇见很多平常没有遇见过的问题,基本上所有问题都是按照网上的参考资料解决的。整个spring学习的过程主要是参考学校教材和其他的零散资料。
开发环境
在目前阶段,所使用的开发环境主要如下
IDEA2018.2.5
Spring-5.1.4
使用前配置
1.在工具栏中点击file,在下拉列表中选择Project Structure,打开后界面如下所示,按照如Modules,Dependencies,+ 三步选择打开文件夹选择所需要的jar文件,所需要的文件如下
所需要的jar文件
创建类并定义类的属性
新建一个包,创Account类,类的的属性如下
package com.jdbc;
public class Account {
private Integer id;
private String username;
private Double balance;
@Override
public String toString() {
return "Account{" +
"id=" + id +
", username='" + username + '\'' +
", balance=" + 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;
}
}
编写配置文件
<?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">
<!-- 1配置数据源 -->
<bean id="dataSource" class=
"org.springframework.jdbc.datasource.DriverManagerDataSource">
<!--数据库驱动 -->
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<!--连接数据库的url -->
<!--如果Mysql数据库的端口为3306则可以不填写端口号 -->
<property name="url" value="jdbc:mysql://localhost/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>
<!--定义id为accountDao的Bean-->
<bean id="accountDao" class="com.jdbc.AccountDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>
</beans>
创建类的接口
定义Account类的接口 AccountDao
package com.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();
}
创建接口的实现类
创建Account类的接口的实现类AccountDaoImpl
package com.jdbc;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import java.util.List;
public class AccountDaoImpl implements AccountDao {
// 声明JdbcTemplate的属性及其setter方法
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[] obj=new Object[]{
account.getUsername(),
account.getBalance(),
account.getId()
};
int num=this.jdbcTemplate.update(sql,obj);
return num;
}
// 根据Id值删除账户信息
public int deleteAccount(int id) {
String sql="delete from account where id=?";
int num=this.jdbcTemplate.update(sql,id);
return num;
}
//根据Id值查找账户信息
public Account findAccountById(int id) {
String sql = "select * from account where id = ?";
RowMapper<Account> rowMapper = new BeanPropertyRowMapper<Account>(Account.class);
try{
return this.jdbcTemplate.queryForObject(sql, rowMapper, id);
}catch (EmptyResultDataAccessException e){
return null;
}
}
//显示全部账户信息
public List<Account> findAllAccount(){
String sql="select * from account";
RowMapper<Account> rowMapper=new BeanPropertyRowMapper<Account>(Account.class);
return this.jdbcTemplate.query(sql,rowMapper);
}
}
编写测试类
package com.jdbc;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
public class jdbcTemplateTest {
public static void main(String[] args){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
JdbcTemplate jdbcTemplate=(JdbcTemplate) applicationContext.getBean("jdbcTemplate");
jdbcTemplate.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 deleteAccount(){
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);
}
}
}
所遇到的问题
在这个过程中,我遇到了如下的问题
一
报错提示如下method findAccountByIdTest should have no parameters
刚开始的时候没仔细看报错的内容,一股脑的去百度,找了一堆却没有一个是正确的,后来仔细看错误提示才明白,问题所在,然后查找junit使用注意事项后才明白
junit4.12 版本中** @test **对应的测试类不需要参数,有参数会提示
二
org.springframework.dao.EmptyResultDataAccessException: Incerrect result size: expected 1,actual 0
这段报错的意思是没有查询到结果,返回值为空
刚刚开始觉得不可思议,后来打开数据库发现确实没有这个ID值所对应的数据,所以我在相应的语句段中加入了try-catch异常处理,处理结果为空的结果,关于这个问题我下次一定要在每个查询操作中处理返回值为空的情况。
关于junit
在整个过程中junit所需要的所有的文件夹都在idea安装目录下的lib文件夹内,可以直接使用,无需从其他地方下载。直接添加 junit.jar, junit-4.12.jar以及hamcrest-core文件夹,如下图
在使用junit的时候,先写@Test表明测试位置,然后Idea会自动在左侧栏中显示一个绿色的三角号,如下
写在最后
我想和你们一样厉害,哇。。。。。。。。。。。。。
/