Spring连接数据库4章

<?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  配置数据源:连接一个什么样的数据库  -->
      <!-- class里的内容是 Libraries 下的web app labires里的spring-jdnc包儿下的 org.springframewor下的datasource下的DriverMangerDataSource -->
      
	      <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
	      <!--   1.1 配置数据库的驱动,想计算机连 接诶打印机 计算机可以连接任意型号的打印机 只需物理连接完成 安装打印机驱动即可 -->
	      <!-- value是点击 mysql-connect下的com.mysql。jdbc下的Driver -->
	      <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
	      <!-- 1.2 数据库的url告诉数据库在哪儿     jdbc:mysql://localhst:3306/first   数据库所在的服务器地址和端口  first 数据库名 -->
	      
	      
	      <property name="url" value="jdbc:mysql://localhost:3306/first"></property>
	      <!-- 1.3 数据库的用户名 -->
	      <property name="username" value="root"></property>
	      <!-- 1.4 数据库的密码 -->
	      <property name="password" value="728616"></property>
	      
	      
	      
      </bean>
      <!-- 配置jdbcTemplent -->
      <!-- 在Spring-jdbc 下的 core下的 jdbc-template -->
      <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" >
     <!--  name里的是最上面bean的id -->
      <property name="dataSource" ref="dataSource"></property>
      </bean>
      <bean id="accountDao" class="com.qcby.mysql.jdbc.AccountDaoImpl">
      <property name="jdbcTemplate" ref="jdbcTemplate"></property>
      </bean>
      
      
</beans>

spring连接数据库需要通过jdbcTemplet  

 然后写一个测试类 

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

public class JdbcTempletTest {
	@Test
	public void jdbcTest() {
		//加载配置文件
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		//获取jdbcTemplate实例
		JdbcTemplate jdbcTemplate= (JdbcTemplate) ac.getBean("jdbcTemplate");
		//执行对应的方法excute()
		jdbcTemplate.execute("CREATE TABLE account("+ 
				"id int PRIMARY KEY auto_increment,"
				+ "username VARCHAR(50),"
				+"balance double)");
		System.out.println("111");
	}

}

添加操作所需的一些文件

首先定义java文件定义其中数据库中的字段   并用get和set 头String方法重写

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;
	}
	@Override
	public String toString() {
		return "Account [id=" + id + ", username=" + username + ", balance=" + balance + "]";
	}
	

	
}

,然后定义一个Dao接口,定义要执行的方法。

/*表明+dao
该接口中写的是对表进行所有的操作方法*/

public interface AccountDao {
	//添加
	int addAccount(Account account);

}

然后创建一gr用来实现接口的类。在其中创建一个jdbcTemplete类,并添加它的set方法。

@Service
public class AccountDaoImpl implements AccountDao{
	//创建一个JdbcTemplete对象并添加一个set方法用来注入
	@Autowired
	private JdbcTemplate jdbcTemplate;

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	@Override
	public int addAccount(Account account) {
		//1定义要执行的sql语句
		//参数不能写死 需要用户输入 所以用占位符?来表示要输入的参数
		String sql="insert into account(username,balance) values(?,?)";
		//2定义数组存储sql语句需要的参数
		Object[] object=new Object[] {
				account.getUsername(),
				account.getBalance()
		};
		//3执行update操作
		int num=this.jdbcTemplate.update(sql,object);
		//4根据返回结果返回相应的状态
		
		// TODO Auto-generated method stub
		return num;
	}

}

然后是测试类

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

public class AccountDaoText {
	@Test
	public void addAccountTest() {
		//加载配置文件
				ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
				//获取jdbcTemplate实例
				AccountDao accountDao= (AccountDao) ac.getBean("accountDao");
				//执行方法需要什么样的参数就创建什么样的参数
				Account account=new Account();
				account.setUsername("里斯");
				account.setBalance(2000.0);
				int num=accountDao.addAccount(account);
				if (num>0) {
					System.out.println("插入成功");
				}else {
					System.out.println("插入失败");
				}
		
	}

}

查询

userDao中写入

 userDaoimpl

 

 测试文件

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值