采用xml+注解方式练习使用Spring JdbcTemplate搭配C3P0
Dao类
package com.maty;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.jdbc.core.RowMapper;
import com.mchange.v2.c3p0.ComboPooledDataSource;
/**
* @author maty e-mail:512181558@qq.com
* @version 创建时间:2018年5月23日 下午7:58:19
* 类说明
*/
public class UserDao
{
@Resource(name="jdbcTemplate")
private JdbcTemplate jdbcTemplate;
public void add()
{
String sql = "select * from user";
List<User> list = jdbcTemplate.query(sql, new MyRowMapper());
System.out.println(list);
//System.out.println("******************"+jdbcTemplate.getDataSource()+"******************");
}
}
class User
{
private String username;
private String password;
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
@Override
public String toString()
{
return "User [username=" + username + ", password=" + password + "]";
}
}
class MyRowMapper implements RowMapper<User>
{
public User mapRow(ResultSet rs, int rowNum) throws SQLException
{
User user = new User();
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
return user;
}
}
Service类
package com.maty;
import javax.annotation.Resource;
/**
* @author maty e-mail:512181558@qq.com
* @version 创建时间:2018年5月23日 下午9:20:45
* 类说明
*/
public class UserService
{
@Resource(name="userDao")
private UserDao userDao;
public void service()
{
System.out.println("我是service");
userDao.add();
}
}
XML配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd ">
<!-- 开启包的扫描 -->
<context:component-scan base-package="com"></context:component-scan>
<!-- 将类注入到Spring中 -->
<!-- C3P0 -->
<bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost/snmp"></property>
<property name="user" value="root"></property>
<property name="password" value="wangxiaowei"></property>
</bean>
<!-- JdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="comboPooledDataSource"></property>
</bean>
<!-- UserDao -->
<bean id="userDao" class="com.maty.UserDao"></bean>
<!-- UserService -->
<bean id="userService" class="com.maty.UserService"></bean>
</beans>
测试类
package com.maty;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author maty e-mail:512181558@qq.com
* @version 创建时间:2018年5月23日 下午8:12:08
* 类说明
*/
public class TestDemo
{
public static void main(String[] args)
{
ApplicationContext context = new ClassPathXmlApplicationContext("applicationcontext.xml");
UserService service = (UserService)context.getBean("userService");
service.service();
}
}
结果
我是service
[User [username=maty, password=28], User [username=tye, password=38]]