spring再学习之整合JDBC
一、JDBCTemplate JDBC模板
user类
package cn.itcast.bean; import java.util.Date; public class User {
private Integer id; private String username; private Date birthday; private String sex; private String address; 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 == null ? null : username.trim();
} public Date getBirthday() {
return birthday;
} public void setBirthday(Date birthday) {
this.birthday = birthday;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex == null ? null : sex.trim();
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address == null ? null : address.trim();
}
}
准备数据库:
package cn.itcast.a_jdbctemplate; import java.beans.PropertyVetoException; import org.junit.Test;
import org.springframework.jdbc.core.JdbcTemplate; import com.mchange.v2.c3p0.ComboPooledDataSource; //演示JDBC模板
public class Demo { @Test
public void fun1() throws Exception {
//0准备连接池C3p0
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql:///mybatis");
dataSource.setUser("root");
dataSource.setPassword("mysqladmin"); //1创建JDBC模板对象
JdbcTemplate it = new JdbcTemplate(); it.setDataSource(dataSource); //2、书写sql语句,并执行
String sql="insert into user(username) value('rose')";
it.update(sql); } }
结果:
二、UserDao用JDBCJDBCTemplate实现
public interface UserDao { //增
void save (User u);
//删
void delete(Integer id);
//改
void update(User u);
//查
User getById(Integer id);
//查
int getTotalCount();
//查 }
实现类:
手动注入JDBC模板
private JdbcTemplate it;
package cn.itcast.a_jdbctemplate; import java.sql.ResultSet;
import java.sql.SQLException; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper; import cn.itcast.bean.User; public class UserDaoImpl implements UserDao{
private JdbcTemplate it;
@Override
public void save(User u) {
String sql="insert into user value(null,?)";
// TODO Auto-generated method stub
it.update(sql, u.getUsername());
} public JdbcTemplate getIt() {
return it;
} public void setIt(JdbcTemplate it) {
this.it = it;
} @Override
public void delete(Integer id) {
// TODO Auto-generated method stub } @Override
public void update(User u) {
// TODO Auto-generated method stub } @Override
public User getById(Integer id) { String sql = "select * from user where id=?"; return it.queryForObject(sql, new RowMapper<User>() { @Override
public User mapRow(ResultSet rs, int arg1) throws SQLException { User u = new User();
u.setId(rs.getInt("id"));
u.setUsername(rs.getString("name"));
// TODO Auto-generated method stub
return u;
} }, id); // TODO Auto-generated method stub
} @Override
public int getTotalCount() { String sql = "select count(*) from user";
// TODO Auto-generated method stub
return it.queryForObject(sql,Integer.class);
} }
配置文件:
顺序:
db.properties配置文件
jdbc.jdbcUrl=jdbc:mysql:///mybatis
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=*****
spring配置文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd "> <!--指定Spring读取db.properties -->
<context:property-placeholder location="classpath:db.properties" /> <!-- 1.将连接池交给Spring管理 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 2.将JDBCTemplate放入Spring容器 -->
<bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 3.将UserDao放入spring容器 -->
<bean name="userDao" class="cn.itcast.a_jdbctemplate.UserDaoImpl">
<property name="it" ref="jdbcTemplate"></property> </bean> </beans>
测试:
package cn.itcast.a_jdbctemplate; import javax.annotation.Resource; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import cn.itcast.bean.User; //演示JDBC模板
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo02 {
@Resource(name="userDao")
private UserDao ud;
@Test
public void fun() throws Exception{
int totalCount = ud.getTotalCount();
System.out.println(totalCount);
}
}
结果:
数据库资料:
控制台输出结果:
14
三、通过继承来准备JDBC模板
配置文件修改:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd "> <!--指定Spring读取db.properties -->
<context:property-placeholder location="classpath:db.properties" /> <!-- 1.将连接池交给Spring管理 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 2.将UserDao放入spring容器 -->
<bean name="userDao" class="cn.itcast.a_jdbctemplate.UserDaoImpl">
<!-- <property name="it" ref="jdbcTemplate"></property>-->
<property name="dataSource" ref="dataSource"></property>
</bean> </beans>
spring再学习之整合JDBC的更多相关文章
- Spring学习5-Spring整合JDBC及其事务处理(注解方式)
一.整合的步骤 1.步骤一:首先要获得DataSource连接池(推荐使用B方式): 要对数据库执行任何的JDBC操作,需要有一个Connection.在Spring中,Connection对象是 ...
- Spring Boot 学习笔记--整合Thymeleaf
1.新建Spring Boot项目 添加spring-boot-starter-thymeleaf依赖 <dependency> <groupId>org.springfram ...
- Spring Boot2.0之 整合JDBC
很入门的知识,大家了解下就OK maven配置文件pom: spring: datasource: url: jdbc:mysql://localhost:3306/test username: ro ...
- spring再学习之设计模式
今天我们来聊一聊,spring中常用到的设计模式,在spring中常用的设计模式达到九种. 第一种:简单工厂 三种工厂模式:https://blog.csdn.net/xiaoddt/article/ ...
- spring再学习之AOP事务
spring中的事务 spring怎么操作事务的: 事务的转播行为: 事务代码转账操作如下: 接口: public interface AccountDao { //加钱 void addMoney( ...
- spring再学习之注解
1.使用注解配置spring <?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi= ...
- spring再学习之配置详解
applicationContext.xml文件配置: bean元素: <?xml version="1.0" encoding="UTF-8"?> ...
- spring再学习之简单测试
一.spring是怎么工作的那,通过一个类装载进容器进行描述: 首先创建一个类user: package cn.itcast.bean; public class User { public User ...
- Spring再学习
一.主要版本变更 框架最早发布于2004年,其后发布了几个重大的版本更新:在Spring 2.0中提供对XML命名空间和AspectJ的支持:Spring 2.5中新增了注解驱动的配置:在Spring ...
随机推荐
- 1V转3.3V稳压供电的芯片电路图
1V转3.3V供电是简单的,仅需要一个芯片和三个外围元件即可组成这样的一个1V转3.3V的电路图和升压电路了.可以持续稳定地供电3.3V给模块或者MCU灯电路.让后端工作稳定,同时也能控制电路的功耗. ...
- 通过js给某个标签添加内容或者删除标签
添加内容 //先保存div中原来的html var tag = document.getElementById("tag").innerHTML; //构造新的内容 var cou ...
- 消息队列之rabbitmq学习使用
消息队列之rabbitmq学习使用 1.RabbitMQ简介 1.1.什么是RabbitMQ? RabbitMQ是一个开源的消息代理和队列服务器,用来通过普通协议在完全不同的应用之间共享数据,Rabb ...
- Py迭代和迭代器,生成器,生产者和消费者模型
迭代器iter 1.迭代的含义: 每次生成的结果依赖于上一次.问路,先问第一个人,第一个人不知道他就说第二个人知道,然后去找第二个人.第二个人不知道就说第三个人知道,然后去找第三个人 2.递归的含义: ...
- 转 Fiddler4 手机抓包
Fiddler4 手机抓包 文章转自:https://www.cnblogs.com/zhengna/p/10876954.html 1.要对计算机Fiddler进行配置,允许远程计算机连接. 2. ...
- 淘宝APP消息推送模型
为什么到了2020年,"统一推送联盟"依旧无法起显著作用? - 知乎 https://www.zhihu.com/question/370632447 https://mp.wei ...
- 编码占用的字节数 1 byte 8 bit 1 sh 1 bit 中文字符编码 2. 字符与编码在程序中的实现 变长编码 Unicode UTF-8 转换 在网络上传输 保存到磁盘上 bytes
小结: 1.UNICODE 字符集编码的标准有很多种,比如:UTF-8, UTF-7, UTF-16, UnicodeLittle, UnicodeBig 等: 2 服务器->网页 utf-8 ...
- GC 卡顿 优化 三色标记优势
小结: 1. 三色标记的一个明显好处是能够让用户程序和 mark 并发的进行 Go GC 卡顿由秒级降到毫秒级以下:到底做了哪些优化? https://mp.weixin.qq.com/s/2BMGG ...
- Elasticsearch从0到千万级数据查询实践(非转载)
1.es简介 1.1 起源 https://www.elastic.co/cn/what-is/elasticsearch,es的起源,是因为程序员Shay Banon在使用Apache Lucene ...
- 洛谷P6218
感觉此题是P4317 花神的数论题的变形版 Description 求一段区间内二进制中 \(0\) 的个数不小于 \(1\) 的个数的数的个数 Solution 数位 DP 先考虑状态转移方程式,如 ...