Spring 持久化操作

下面演示向数据库Springtest中表tb_user中插入数据:
(1)通过xml实现DataSource(数据源注入)。
(2)使用jdbcTemplate访问数据。
(3)使用ORM工具访问数据。
以第一种方式介绍:
1、创建包cn.xc.entity.在该包下建立一个实体类StudentInfo.
类中定义对应数据库表中的属性。
代码如下:
package cn.xc.entity;
public class StudentInfo {
private int id;
private String name;
private int age;
private String score;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getScore() {
return score;
}
public void setScore(String score) {
this.score = score;
}
}
2、创建包cn.xc.dao。
在该包下建立接口StudentDao。
在该接口中定义用来向数据库表中插入数据的方法: addStudent().
代码如下:
package cn.xc.dao;
import cn.xc.entity.StudentInfo;
public interface StudentDao {
public void addStudent(StudentInfo student);
}

注:student是StudentInfo的实体对象。
3、创建包cn.xc.Impdao。在该包下创建类StudentDaoImpl并实现StudentDao接口。
代码如下:
package cn.xc.Impdao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.sql.DataSource;
import cn.xc.dao.StudentDao;
import cn.xc.entity.StudentInfo;
public class StudentDaoImpl implements StudentDao {
private DataSource dataSource;
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public void addStudent(StudentInfo student) {
String name=student.getName();
int age=student.getAge();
String score=student.getScore();
Connection conn = null;
PreparedStatement ps = null;
try {
//获取数据库连接
conn = dataSource.getConnection();
//添加数据的SQL语句
String sql = “insert into tb_user(name,age,score) values(?,?,?)”;
ps = conn.prepareStatement(sql);
ps.setString(1, name);
ps.setInt(2, age);
ps.setString(3, score);
//执行SQL语句
ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
ps.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

4、创建Spring的配置文件applicationContext.xml。
(1)定义数据源DataSource。它是Spring中的DriverManagerDataSource类的实例。
(2)配置类StudentDaoImpl。注入DataSource属性值。
在这里插入图片描述
5、创建包cn.xc.test。
在该包下创建测试类Test。
代码如下:
package cn.xc.test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import cn.xc.dao.StudentDao;
import cn.xc.entity.StudentInfo;
public class Test {
public static void main(String[] args){
//装载配置文件
Resource resource = new ClassPathResource(“applicationContext.xml”);
BeanFactory factory = new XmlBeanFactory(resource);
//实例化StudentInfo对象
StudentInfo student = new StudentInfo();
student.setName(“李四”);
student.setAge(new Integer(22));
student.setScore(“98”);
//获取student
StudentDao studentDAO = ( StudentDao) factory.getBean(“student”);
//执行添加方法
studentDAO.addStudent(student);
System.out.println(“数据添加成功”);
}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小Java开发者

“是一种鼓励,你懂的”

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值