您的位置:首页 > 编程语言 > Java开发

spring对JDBC的支持

2017-11-04 15:04 344 查看
spring对JDBC的支持:

        (1)加入数据源c3p0,mysql的驱动,以及spring的jar包,然后选中所有的jar包,右击Build path-->add to Build path

                     



           (2)在src新建一个数据库文件,取名为db.propertites,主要内容如下:

jdbc.user=root
jdbc.password=112233
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.Url=jdbc:mysql:///spring

jdbc.initialPoolSize=5
jdbc.maxPoolSize=10          (3)在src新建一个spring的配置文件,beans-jdbc.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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!--配置自动扫描包 -->
<context:component-scan base-package="com.xjj.spring.jdbc"></context:component-scan>

<!--导入资源文件 -->
<context:property-placeholder location="classpath:db.properties"></context:property-placeholder>

<!--配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.Url}"></property>

<property name="initialPoolSize" value="${jdbc.initialPoolSize}"></property>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
</bean>

<!--配置spring的JdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>

</beans>              
         (4)新建一个包:com.xjj.spring.jdbc,新建一个main类测试:

public static void main(String[] args) {
ApplicationContext context =new ClassPathXmlApplicationContext("beans-jdbc.xml");
DataSource dataSource =context.getBean(DataSource.class);
System.out.println(dataSource);
}               如果控制台:输出以下代码,说明数据源配置正确。
                    



             (5)此时可以通过JDBCTemplate类对数据库进行操作了,首先在mysql数据库里新建一个名为spring的数据库,新建一张student表。

                public class Main {

private static JdbcTemplate jdbcTemplate = null;
private static StudentDao studentDao;

public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans-jdbc.xml");
DataSource dataSource = context.getBean(DataSource.class);
System.out.println(dataSource);
// 实例化,即创建对象的过程
jdbcTemplate = (JdbcTemplate) context.getBean("jdbcTemplate");
// studentDao = context.getBean(StudentDao.class);
// getStudent();//测试StudentDao的getStudent方法

Update();// 修改数据
BacthUpdate();// 执行批量的更新
queryForObject();// 从数据库里获取一条记录,实际对应一个对象
testQueryForList();// 查到实体的集合
QueryForObject2();// 获取单个列的值,或统计查询

}

/**
* 修改数据,把许健杰改为小许;
*/
public static void Update() {
String sql = "update student set name=? where id =?";
jdbcTemplate.update(sql, "小许", 1);
System.out.println("Update()....");
}

/**
* 执行批量的更新:批量Insert,Update,Delete, 最后一个参数是Object[]的List类型,因为修改一条记录,
* 就是一个Object数组,所以多条记录就是Object的集合
*
*/
public static void BacthUpdate() {
String sql = "insert into student(na、me,age,score) values (?,?,?)";
List<Object[]> batchArgs = new ArrayList<Object[]>();
batchArgs.add(new Object[] { "刘德华", 50, 89 });
batchArgs.add(new Object[] { "张学友", 53, 90 });
batchArgs.add(new Object[] { "梅艳芳", 51, 100 });
jdbcTemplate.batchUpdate(sql, batchArgs);
System.out.println("BacthUpdate()");
}

/**
* 查到实体的集合 注意调用的不是queryForList方法
*
*/
public static void testQueryForList() {
String sql = "select id,name,age,score from student where id > ?";
RowMapper<Student> rowMapper = new BeanPropertyRowMapper<>(Student.class);
List<Student> students = jdbcTemplate.query(sql, rowMapper, 3);
System.out.println(students);
}

/**
*
* 从数据库里获取一条记录,实际对应一个对象 注意不是queryForObject(String sql,Class<Employee>
* requiredType,Object... args)方法 而需要调用queryForObject(String sql,
* RowMapper<Employee> rowMapper,Object... args);
* 1.其中的RowMapper指定如何去映射结果集的行,常用的实现类为BeanPropertyRowMapper
* 2.使用SQL中列名和类的属性名的映射。例如name-->name
* 3.不支持级联属属性,JdbcTemplate归根结底是一个JDBC的小工具,而不是ORM框架
*
*/

public static void queryForObject() {
String sql = "select id,name,age,score from student where id = ? ";
RowMapper<Student> rowMapper = new BeanPropertyRowMapper<>(Student.class);
Student student = jdbcTemplate.queryForObject(sql, rowMapper, 4);
System.out.println(student);
}

/**
* 获取单个列的值,或统计查询 使用queryForObject(String sql, Class<Long> requiredType);
*/
public static void QueryForObject2() {
String sql = "select count(id) from student";
long count = jdbcTemplate.queryForObject(sql, Long.class);

System.out.println("count:" + count);
}

}
           (6)结束



              

       

                        

                     

                   

                  


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: