springJDBC的几种方法
1、简单粗暴,直接在类中创建连接池使用
- package com.xiaostudy;
- import org.apache.commons.dbcp.BasicDataSource;
- import org.springframework.jdbc.core.JdbcTemplate;
- /**
- * @desc 测试类
- *
- * @author xiaostudy
- *
- */
- public class Test {
- public static void main(String[] args) {
- //创建连接池
- BasicDataSource dataSource = new BasicDataSource();
- dataSource.setDriverClassName("com.mysql.jdbc.Driver");
- dataSource.setUrl("jdbc:mysql://localhost:3306/user");
- dataSource.setUsername("root");
- dataSource.setPassword("123456");
- //创建模板
- /*JdbcTemplate jdbcTemplate = new JdbcTemplate();
- jdbcTemplate.setDataSource(dataSource);*/
- JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
- //添加
- jdbcTemplate.update("insert into spring_user(name, password) values(?, ?);", "xiaostudy", "123456");
- //修改
- jdbcTemplate.update("update spring_user set name=?,password=? where id=?;", "xiaostudy", "123456", "2");
- //删除
- jdbcTemplate.update("delete from spring_user where name=? and password=?;", "xiaostudy", "123456");
- }
- }
2、较第一种,就是把业务分开
2.1、domain类User.java
- package com.xiaostudy;
- /**
- * @desc domain类
- * @author xiaostudy
- *
- */
- public class User {
- private Integer id;
- private String name;
- private String password;
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- @Override
- public String toString() {
- return "User [id=" + id + ", name=" + name + ", password=" + password + "]";
- }
- }
2.2、dao类UserDao.java
- package com.xiaostudy;
- import org.apache.commons.dbcp.BasicDataSource;
- import org.springframework.jdbc.core.JdbcTemplate;
- /**
- * @desc Dao类
- * @author xiaostudy
- *
- */
- public class UserDao {
- /**
- * @desc 获取模板的方法
- * @return JdbcTemplate 返回类型
- */
- public JdbcTemplate getJdbcTemplate() {
- // 创建连接池
- BasicDataSource dataSource = new BasicDataSource();
- dataSource.setDriverClassName("com.mysql.jdbc.Driver");
- dataSource.setUrl("jdbc:mysql://localhost:3306/user");
- dataSource.setUsername("root");
- dataSource.setPassword("123456");
- // 创建模板
- JdbcTemplate jdbcTemplate = new JdbcTemplate();
- jdbcTemplate.setDataSource(dataSource);
- return jdbcTemplate;
- }
- /**
- * @desc 添加用户
- * @param user 参数
- * @return int 返回类型
- */
- public int insertUser(User user) {
- JdbcTemplate jdbcTemplate = getJdbcTemplate();
- return jdbcTemplate.update("insert into spring_user(name, password) values(?, ?);", user.getName(),
- user.getPassword());
- }
- /**
- * @desc 修改用户
- * @param user 参数
- * @param id 参数
- * @return int 返回类型
- */
- public int updateUser(User user, int id) {
- JdbcTemplate jdbcTemplate = getJdbcTemplate();
- return jdbcTemplate.update("update spring_user set name=?,password=? where id=?;", user.getName(),
- user.getPassword(), id);
- }
- /**
- * @desc 删除用户
- * @param user 参数
- * @return int 返回类型
- */
- public int deleteUser(User user) {
- JdbcTemplate jdbcTemplate = getJdbcTemplate();
- return jdbcTemplate.update("delete from spring_user where name=? and password=?;", user.getName(),
- user.getPassword());
- }
- }
2.3、测试类Test.java
- package com.xiaostudy;
- /**
- * @desc 测试类
- * @author xiaostudy
- *
- */
- public class Test {
- public static void main(String[] args) {
- User user = new User();
- user.setName("xiaostudy");
- user.setPassword("123456");
- UserDao userDao = new UserDao();
- // userDao.insertUser(user);
- // userDao.updateUser(user, 1);
- userDao.deleteUser(user);
- }
- }
3、较第二种,接入spring中
3.1、domain类User.java
- package com.xiaostudy;
- /**
- * @desc damain类
- * @author xiaostudy
- *
- */
- public class User {
- private Integer id;
- private String name;
- private String password;
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- @Override
- public String toString() {
- return "User [id=" + id + ", name=" + name + ", password=" + password + "]";
- }
- }
3.2、dao类UserDao.java
- package com.xiaostudy;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import org.springframework.jdbc.core.JdbcTemplate;
- /**
- * @desc dao类
- * @author xiaostudy
- *
- */
- public class UserDao {
- /**
- * @desc 获取模板的方法
- * @return JdbcTemplate 返回类型
- */
- public JdbcTemplate getJdbcTemplate() {
- //从spring容器中获取连接池对象
- ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
- return applicationContext.getBean("jdbcTemplate", JdbcTemplate.class);
- }
- /**
- * @desc 添加用户
- * @param user 参数
- * @return int 返回类型
- */
- public int insertUser(User user) {
- JdbcTemplate jdbcTemplate = getJdbcTemplate();
- return jdbcTemplate.update("insert into spring_user(name, password) values(?, ?);", user.getName(), user.getPassword());
- }
- /**
- * @desc 修改用户
- * @param user 参数
- * @param id 参数
- * @return int 返回类型
- */
- public int updateUser(User user, int id) {
- JdbcTemplate jdbcTemplate = getJdbcTemplate();
- return jdbcTemplate.update("update spring_user set name=?,password=? where id=?;", user.getName(), user.getPassword(), id);
- }
- /**
- * @desc 删除用户
- * @param user 参数
- * @return int 返回类型
- */
- public int deleteUser(User user) {
- JdbcTemplate jdbcTemplate = getJdbcTemplate();
- return jdbcTemplate.update("delete from spring_user where name=? and password=?;", user.getName(), user.getPassword());
- }
- }
3.3、spring配置文件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:aop="http://www.springframework.org/schema/aop"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context.xsd">
- <!-- 将domain类添加到容器中 -->
- <bean id="user" class="com.xiaostudy.User"></bean>
- <!-- 将dao类添加到容器中 -->
- <bean id="userDao" class="com.xiaostudy.UserDao"></bean>
- <!-- 将连接池添加到容器中 -->
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
- <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
- <property name="url" value="jdbc:mysql://localhost:3306/user"></property>
- <property name="username" value="root"></property>
- <property name="password" value="123456"></property>
- </bean>
- <!-- 将模板添加到容器中 -->
- <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
- <property name="dataSource" ref="dataSource"></property>
- </bean>
- </beans>
3.4、测试类Test.java
- package com.xiaostudy;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- /**
- * @desc 测试类
- * @author xiaostudy
- *
- */
- public class Test {
- public static void main(String[] args) {
- //获取spring容器
- ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
- //从容器中获取domain对象
- User user = applicationContext.getBean("user", User.class);
- user.setName("huangwu");
- user.setPassword("123456");
- //从容器中获取dao对象
- UserDao userDao = applicationContext.getBean("userDao", UserDao.class);
- //userDao.insertUser(user);
- userDao.updateUser(user, 2);
- //userDao.deleteUser(user);
- }
- }
4、较第三种,把在spring配置文件中的连接池信息提取到一个配置文件中
4.1、domain类User.java
- package com.xiaostudy;
- /**
- * @domain类
- * @author xiaostudy
- *
- */
- public class User {
- private Integer id;
- private String name;
- private String password;
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- @Override
- public String toString() {
- return "User [id=" + id + ", name=" + name + ", password=" + password + "]";
- }
- }
4.2、dao类UserDao.java
- package com.xiaostudy;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import org.springframework.jdbc.core.JdbcTemplate;
- /**
- * @desc dao类
- * @author xiaostudy
- *
- */
- public class UserDao {
- /**
- * @desc 获取模板的方法
- * @return JdbcTemplate 返回类型
- */
- public JdbcTemplate getJdbcTemplate() {
- ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
- return applicationContext.getBean("jdbcTemplate", JdbcTemplate.class);
- }
- /**
- * @desc 添加用户
- * @param user 参数
- * @return int 返回类型
- */
- public int insertUser(User user) {
- JdbcTemplate jdbcTemplate = getJdbcTemplate();
- return jdbcTemplate.update("insert into spring_user(name, password) values(?, ?);", user.getName(), user.getPassword());
- }
- /**
- * @desc 修改用户
- * @param user 参数
- * @param id 参数
- * @return int 返回类型
- */
- public int updateUser(User user, int id) {
- JdbcTemplate jdbcTemplate = getJdbcTemplate();
- return jdbcTemplate.update("update spring_user set name=?,password=? where id=?;", user.getName(), user.getPassword(), id);
- }
- /**
- * @desc 删除用户
- * @param user 参数
- * @return int 返回类型
- */
- public int deleteUser(User user) {
- JdbcTemplate jdbcTemplate = getJdbcTemplate();
- return jdbcTemplate.update("delete from spring_user where name=? and password=?;", user.getName(), user.getPassword());
- }
- }
4.3、连接池的配置文件dataSource.properties
- jdbc.driverClassName=com.mysql.jdbc.Driver
- jdbc.url=jdbc:mysql://localhost:3306/user
- jdbc.username=root
- jdbc.password=123456
4.4、spring配置文件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:aop="http://www.springframework.org/schema/aop"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context.xsd">
- <!-- 将domain类添加到容器中 -->
- <bean id="user" class="com.xiaostudy.User"></bean>
- <!-- 将dao类添加到容器中 -->
- <bean id="userDao" class="com.xiaostudy.UserDao"></bean>
- <!-- 将连接池的配置文件添加到容器中 -->
- <context:property-placeholder location="classpath:dataSource.properties"/>
- <!-- 将连接池添加到容器中 -->
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
- <property name="driverClassName" value="${jdbc.driverClassName}"></property>
- <property name="url" value="${jdbc.url}"></property>
- <property name="username" value="${jdbc.username}"></property>
- <property name="password" value="${jdbc.password}"></property>
- </bean>
- <!-- 将模板添加到容器中 -->
- <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
- <property name="dataSource" ref="dataSource"></property>
- </bean>
- </beans>
4.5、测试类Test.java
- package com.xiaostudy;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- /**
- * @desc 测试类
- *
- * @author xiaostudy
- *
- */
- public class Test {
- public static void main(String[] args) {
- //获取spring容器
- ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
- //从容器中获取domain对象
- User user = applicationContext.getBean("user", User.class);
- user.setName("lisi");
- user.setPassword("123456");
- //从容器中获取dao对象
- UserDao userDao = applicationContext.getBean("userDao", UserDao.class);
- //userDao.insertUser(user);
- userDao.updateUser(user, 2);
- //userDao.deleteUser(user);
- }
- }
5、较DBCP,用C3P0连接池,需要改变的就是连接池的包要改变,和连接池配置的名称要改
6、较第五种,把模板添加到容器改成让dao去添加模板
6.1、domain类User.java
- package com.xiaostudy;
- /**
- * @desc domain类
- * @author xiaostudy
- *
- */
- public class User {
- private Integer id;
- private String name;
- private String password;
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- @Override
- public String toString() {
- return "User [id=" + id + ", name=" + name + ", password=" + password + "]";
- }
- }
6.2、dao类UserDao.java
- package com.xiaostudy;
- import org.springframework.jdbc.core.JdbcTemplate;
- import org.springframework.jdbc.core.support.JdbcDaoSupport;
- /**
- * @desc dao类
- * @author xiaostudy
- *
- */
- public class UserDao extends JdbcDaoSupport {
- /**
- * @desc 添加用户
- * @param user 参数
- * @return int 返回类型
- */
- public int insertUser(User user) {
- //从继承的父类中获取模板
- JdbcTemplate jdbcTemplate = getJdbcTemplate();
- return jdbcTemplate.update("insert into spring_user(name, password) values(?, ?);", user.getName(), user.getPassword());
- }
- /**
- * @desc 修改用户
- * @param user 参数
- * @param id 参数
- * @return int 返回类型
- */
- public int updateUser(User user, int id) {
- JdbcTemplate jdbcTemplate = getJdbcTemplate();
- return jdbcTemplate.update("update spring_user set name=?,password=? where id=?;", user.getName(), user.getPassword(), id);
- }
- /**
- * @desc 删除用户
- * @param user 参数
- * @return int 返回类型
- */
- public int deleteUser(User user) {
- JdbcTemplate jdbcTemplate = getJdbcTemplate();
- return jdbcTemplate.update("delete from spring_user where name=? and password=?;", user.getName(), user.getPassword());
- }
- }
6.3、连接池的配置文件dataSource.properties
- jdbc.driverClassName=com.mysql.jdbc.Driver
- jdbc.url=jdbc:mysql://localhost:3306/user
- jdbc.username=root
- jdbc.password=123456
6.4、spring配置文件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:aop="http://www.springframework.org/schema/aop"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context.xsd">
- <!-- 将domain类添加到容器中 -->
- <bean id="user" class="com.xiaostudy.User"></bean>
- <!-- 将dao类添加到容器中 -->
- <bean id="userDao" class="com.xiaostudy.UserDao">
- <!-- 将连接池给dao,让dao去注入 -->
- <property name="dataSource" ref="dataSource"></property>
- </bean>
- <!-- 将连接池的配置文件添加到容器中 -->
- <context:property-placeholder location="classpath:dataSource.properties"/>
- <!-- 将连接池添加到容器中 -->
- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
- <property name="driverClass" value="${jdbc.driverClass}"></property>
- <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
- <property name="user" value="${jdbc.user}"></property>
- <property name="password" value="${jdbc.password}"></property>
- </bean>
- </beans>
6.5测试类Test.java
- package com.xiaostudy;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- /**
- * @desc 测试类
- * @author xiaostudy
- *
- */
- public class Test {
- public static void main(String[] args) {
- //获取spring容器
- ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
- //从容器中获取domain对象
- User user = applicationContext.getBean("user", User.class);
- user.setName("sssss");
- user.setPassword("123456");
- //从容器中获取dao对象
- UserDao userDao = applicationContext.getBean("userDao", UserDao.class);
- //userDao.insertUser(user);
- userDao.updateUser(user, 2);
- //userDao.deleteUser(user);
- }
- }
springJDBC的几种方法的更多相关文章
- Spring使用jdbcJdbcTemplate和三种方法配置数据源
三种方法配置数据源 1.需要引入jar包:spring-jdbc-4.3.2.RELEASE.jar <!-- spring内置,springJdbc,配置数据源 --> <bean ...
- JS 判断数据类型的三种方法
说到数据类型,我们先理一下JavaScript中常见的几种数据类型: 基本类型:string,number,boolean 特殊类型:undefined,null 引用类型:Object,Functi ...
- DataTable 转换成 Json的3种方法
在web开发中,我们可能会有这样的需求,为了便于前台的JS的处理,我们需要将查询出的数据源格式比如:List<T>.DataTable转换为Json格式.特别在使用Extjs框架的时候,A ...
- Android之数据存储的五种方法
1.Android数据存储的五种方法 (1)SharedPreferences数据存储 详情介绍:http://www.cnblogs.com/zhangmiao14/p/6201900.html 优 ...
- 两个变量交换的四种方法(Java)
对于两种变量的交换,我发现四种方法,下面我用Java来演示一下. 1.利用第三个变量交换数值,简单的方法. (代码演示一下) class TestEV //创建一个类 { public static ...
- C#中实现并发的几种方法的性能测试
C#中实现并发的几种方法的性能测试 0x00 起因 去年写的一个程序因为需要在局域网发送消息支持一些命令和简单数据的传输,所以写了一个C/S的通信模块.当时的做法很简单,服务端等待链接,有用户接入后开 ...
- Atitti 载入类的几种方法 Class.forName ClassLoader.loadClass 直接new
Atitti 载入类的几种方法 Class.forName ClassLoader.loadClass 直接new 1.1. 载入类的几种方法 Class.forName ClassLo ...
- windows下获取IP地址的两种方法
windows下获取IP地址的两种方法: 一种可以获取IPv4和IPv6,但是需要WSAStartup: 一种只能取到IPv4,但是不需要WSAStartup: 如下: 方法一:(可以获取IPv4和I ...
- Python下载网页的几种方法
get和post方式总结 get方式:以URL字串本身传递数据参数,在服务器端可以从'QUERY_STRING'这个变量中直接读取,效率较高,但缺乏安全性,也无法来处理复杂的数据(只能是字符串,比如在 ...
随机推荐
- sql优化(2)
转自:https://www.cnblogs.com/Jacck/p/8030455.html 看到一篇非常全面的SQL优化文章,在开发的工作中往往不考虑性能上的缺失(在一开始的时候数据量不大也看不出 ...
- 爬虫入门【10】Pyspider框架简介及安装说明
Pyspider是python中的一个很流行的爬虫框架系统,它具有的特点如下: 1.可以在Python环境下写脚本 2.具有WebUI,脚本编辑器,并且有项目管理和任务监视器以及结果查看. 3.支持多 ...
- C++名人的网站 转
正如我们可以通过计算机历史上的重要人物了解计算机史的发展,C++相关人物的网站也可以使我们得到最有价值的参考与借鉴. 正如我们可以通过计算机历史上的重要人物了解计算机史的发展,C++相关人物的网站也可 ...
- MQ中间件对比
- springMvc获取特殊值
1.获取数组
- 什么是EJB
学习EJB可以加深对J2EE平台的认识. 百科定义EJB: 被称为java企业bean,服务器端组件,核心应用是部署分布式应用程序.用它部署的系统不限定平台.实际上ejb是一种产品,描述了应用组件要解 ...
- JS操作时间 - UNIX时间戳简单介绍
准备知识 GMT(Greenwich Mean Time) - 格林尼治时间.UTC(Universal Time Code) - 国际协调时间.PST(Pacific Standard Time,太 ...
- python全栈开发从入门到放弃之网络基础
一.操作系统基础 操作系统:(Operating System,简称OS)是管理和控制计算机硬件与软件资源的计算机程序,是直接运行在“裸机”上的最基本的系统软件,任何其他软件都必须在操作系统的支持下才 ...
- zend studio设置utf8
1. windows -> preference -> general -> workspace 2.项目右键 -> properities -> resource 3. ...
- webservice -- cxf客户端调用axis2服务端
背景: 有个项目, 需要由第三方提供用户信息, 实现用户同步操作, 对方给提供webservice接口(axis2实现)并也使用axis2作主客户端调用我方提供的webservice接口 起初, 由于 ...