Spring4.3x教程之四JDBC使用

Spring内置了一个实现了JDBC的模块。
我们想要使用JDBC模块,那么就得有数据库的操作了。
使用步骤:
1、引用jar包
2、创建数据库和表
3、创建类
4、创建配置文件
5、测试
建表语句:

CREATE TABLE `tb_student` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(10) DEFAULT NULL,
 `sex` varchar(1) DEFAULT NULL,
 `age` int(11) DEFAULT NULL,
 `qq` varchar(20) DEFAULT NULL,
 `sign` varchar(50) DEFAULT NULL,
 PRIMARY KEY (`id`))

Student类:

public class Student {

    private int id;
    private String name;
    private String sex;
    private String qq;
    private String sign;
    private int age;
    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 String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getQq() {
        return qq;
    }
    public void setQq(String qq) {
        this.qq = qq;
    }
    public String getSign() {
        return sign;
    }
    public void setSign(String sign) {
        this.sign = sign;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public Student(String name, String sex, String qq, String sign, int age) {
        super();
        this.name = name;
        this.sex = sex;
        this.qq = qq;
        this.sign = sign;
        this.age = age;
    }
    public Student() {
        super();
    }
}

创建的Dao类,因为要使用JDBC末模板所以我们需要继承JdbcDaoSupport

public class StudentDao extends JdbcDaoSupport{

    //保存
    public boolean save(Student student) {
        //新增语句,非查询SQL
        int ct=getJdbcTemplate().update(
                "insert into tb_student(name,age,sex,qq,sign) values(?,?,?,?,?)", 
                student.getName(),student.getAge(),student.getSex(),student.getQq(),student.getSign());

        return ct>0?true:false;
    }
    //查询全部
    public List<Student> queryAll() {
        //Class要求查询的列只能有一个
        return getJdbcTemplate().query("select * from tb_student", new ResultType());
    }
    //模糊查询
    public List<Student> queryLikeName(String name) {
        return getJdbcTemplate().query("select * from tb_student where name like ?", 
                new ResultType(), "%"+name+"%");
    }

    private class ResultType implements RowMapper<Student>{

        @Override
        public Student mapRow(ResultSet rs, int index) throws SQLException {
            Student student=new Student(rs.getString("name"), rs.getString("sex"), rs.getString("qq"), rs.getString("sign"), rs.getInt("age"));
            student.setId(rs.getInt("id"));
            return student;
        }   
    }
}

下面是applicationContext.xml的内容:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:context="http://www.springframework.org/schema/context"
      xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--SPring JDBC模块+AOP使用  -->
    <!--加载数据库的连接配置文件 -->
    <context:property-placeholder location="classpath:dbconfig.properties" />
    <!--配置数据库来连接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">
        <!--驱动类全称 -->
        <property name="driverClass" value="${jdbc.driverClassName}" />
        <!--数据库的url地址 -->
        <property name="jdbcUrl" value="${jdbc.url}" />
        <!--用户名 -->
        <property name="user" value="${jdbc.username}" />
        <!--密码 -->
        <property name="password" value="${jdbc.password}" />
        <!--配置最大的连接数 -->
        <property name="maxPoolSize" value="${jdbc.maxsize}"></property>
        <!--配置最小的连接数 -->
        <property name="minPoolSize" value="${jdbc.minsize}"></property>
        <!--配置连接最大空闲时间 -->
        <property name="maxIdleTime" value="${jdbc.idletime}"></property>
    </bean>

    <!--配置dao对象 -->
    <bean id="sdao" class="cn.code404.dao.StudentDao" scope="prototype">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--配置service对象 -->
    <bean id="sservice" class="cn.code404.service.StudentService" scope="prototype">
        <property name="dao" ref="sdao"></property>
    </bean>
</beans>

dbconfig.properties数据库的配置信息:

#数据库连接的配置文件
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring_jdbc?characterEncoding=utf-8
jdbc.username=lx
jdbc.password=lx
jdbc.maxsize=100
jdbc.minsize=5
jdbc.idletime=60

这就是Spring的JDBC模块的基本使用。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值