Spring4.3x教程之四JDBC使用
来源:互联网 发布:淘宝盖楼有什么技巧 编辑:程序博客网 时间:2024/06/05 16:18
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.Driverjdbc.url=jdbc:mysql://localhost:3306/spring_jdbc?characterEncoding=utf-8jdbc.username=lxjdbc.password=lxjdbc.maxsize=100jdbc.minsize=5jdbc.idletime=60
这就是Spring的JDBC模块的基本使用。
阅读全文
1 0
- Spring4.3x教程之四JDBC使用
- Spring4.3x教程之二SpEL表达式的使用
- Spring4.X系列之Spring JDBC
- Spring4.3x教程之三AOP详解
- Spring4.3x教程之五事物详解
- Spring4.3x教程之六注解详解
- Spring4.3x教程之一IOC&DI
- SpingMVC4.3x教程之四@RequestBody和@ResponseBody
- Spring4.X系列之IOC
- spring4.x系列学习笔记(四)
- vv_jeffy之spring4.x集成myibatis3.x
- spring4 JDBC
- spring4.2+struts2.5+jdbc实例-PIMS(四)
- JAVA WEB之Spring4.x JdbcTemplate
- Spring4.X系列之AOP基础篇
- Spring4.X系列之AOP-@AspectJ
- SpringMVC4.3x教程之三拦截器的使用
- SpringMVC4.3x教程之九RESTFul使用
- Maven eclipse 打war包
- ActiveMQ实例
- Prototype功能预览十:面向未来技术的框架
- PHP超全局变量$_ENV详解,及$_ENV为空的可能原因
- C++变量的实现与机制
- Spring4.3x教程之四JDBC使用
- python学习笔记(pdb)
- XMind健康播报:夏季饮食计划
- poi百万级数据导出excel
- 使用IntelliJ IDEA查看类的继承关系图形
- 字符串旋转逆序操作
- mybatis的占位符区别
- JSP文件超过65535
- JS日期插件