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); } }

最新文章

  1. latex公式编号
  2. Windows下nginx+php配置
  3. Using LINQ to SharePoint
  4. serv-u启动管理控制台后提示脚本错误解决方案
  5. scrollTop 鼠标往下移动到一定位置显示隐藏
  6. 一天一点MySQL复习——获取数据库系统时间、变量赋值、变量比较
  7. BZOJ 1053: [HAOI2007]反素数ant dfs
  8. 一个汉字的ASCII编码&amp;#12288;
  9. android体系架构
  10. 习题9-8 Uva1632
  11. python 去掉 pyc
  12. eclipse下搭建hibernate5.0环境
  13. PMBook - 上课体会
  14. css中自定义字体
  15. eclipse快速配置spring相关xml文件头信息
  16. #021 Java复习第一天
  17. protel dxp 2004安装与破解
  18. 【php】php实现数组分块
  19. MYSQL 查看最大连接数和修改最大连接数
  20. Ubuntu10.04 python2.6下安装matplotlib环境

热门文章

  1. 160719、Spring + Dubbo + zookeeper (linux) 框架搭建
  2. javamail 发送邮件demo(文字与附件)
  3. APP测试瞎话
  4. Avalondock 第四步 边缘停靠
  5. instanceof和isInstance(Object obj) 和isAssignableFrom(Class cls)的区别和联系
  6. &lt;2014 05 09&gt; 程序员:从C++转到Java需注意的地方
  7. C#__ 模拟鼠标单击事件
  8. How to make TWebBrowser get focus in Delphi
  9. JAVA寄存器
  10. linux sed批量替换多个文件内容