标签:arch gdb max core 命名参数 div add 导入 font
Spring主要提供JDBC模板方式、关系数据库对象化方式、SimpleJdbc方式、事务管理来简化JDBC编程
Spring提供了3个模板类:
JdbcTemplate主要提供以下4类方法:
1、首先建立person表
1 CREATE TABLE `person` ( 2 `id` int(11) NOT NULL AUTO_INCREMENT, 3 `name` varchar(255) DEFAULT NULL, 4 `age` int(11) DEFAULT NULL, 5 PRIMARY KEY (`id`) 6 )
2、项目引入包
druid-1.0.29.jar
mysql-connector-java-5.1.42-bin.jar
Spirng包(第一个Spring项目中说明了那些包)
3、创建Person类
1 class Person{ 2 private int id; 3 private String name; 4 private int age; 5 6 public int getId() { 7 return id; 8 } 9 10 public void setId(int id) { 11 this.id = id; 12 } 13 14 public String getName() { 15 return name; 16 } 17 18 public void setName(String name) { 19 this.name = name; 20 } 21 22 public int getAge() { 23 return age; 24 } 25 26 public void setAge(int age) { 27 this.age = age; 28 } 29 30 @Override 31 public String toString() { 32 return "Person{" + 33 "id=" + id + 34 ", name=‘" + name + ‘\‘‘ + 35 ", age=" + age + 36 ‘}‘; 37 } 38 }
4、创建配置文件configdb.xml和db.properties
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 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"> 6 7 <!-- 导入资源文件 --> 8 <context:property-placeholder location="classpath:db.properties"></context:property-placeholder> 9 10 <!-- 配置druid数据库连接池 --> 11 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> 12 <property name="driverClassName" value="${jdbc.driverClass}"></property> 13 <property name="url" value="${jdbc.url}"></property> 14 <property name="username" value="${jdbc.user}"></property> 15 <property name="password" value="${jdbc.password}"></property> 16 17 <!-- 最大并发连接数 --> 18 <property name="maxActive" value="${jdbc.maxActive}" /> 19 <!-- 初始化连接数量 --> 20 <property name="initialSize" value="${jdbc.initialSize}" /> 21 <!-- 配置获取连接等待超时的时间 --> 22 <property name="maxWait" value="${jdbc.maxWait}" /> 23 <!-- 最小空闲连接数 --> 24 <property name="minIdle" value="${jdbc.minIdle}" /> 25 <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> 26 <property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}" /> 27 <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> 28 <property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}" /> 29 <property name="validationQuery" value="${jdbc.validationQuery}" /> 30 <property name="testWhileIdle" value="${jdbc.testWhileIdle}" /> 31 <property name="testOnBorrow" value="${jdbc.testOnBorrow}" /> 32 <property name="testOnReturn" value="${jdbc.testOnReturn}" /> 33 <property name="poolPreparedStatements" value="${jdbc.poolPreparedStatements}" /> 34 <property name="maxOpenPreparedStatements" value="${jdbc.maxOpenPreparedStatements}" /> 35 </bean> 36 37 <!-- 配置Spring 的JDBCTemplate --> 38 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 39 <property name="dataSource" ref="dataSource"></property> 40 </bean> 41 </beans>
1 jdbc.user=root 2 jdbc.password=hzg1984110 3 jdbc.driverClass=com.mysql.jdbc.Driver 4 jdbc.url=jdbc:mysql:///hzgtest 5 6 jdbc.maxActive=30 7 jdbc.initialSize=5 8 jdbc.maxWait=60000 9 jdbc.minIdle=5 10 jdbc.timeBetweenEvictionRunsMillis=60000 11 jdbc.minEvictableIdleTimeMillis=300000 12 jdbc.validationQuery=SELECT ‘x‘ 13 jdbc.testWhileIdle=true 14 jdbc.testOnBorrow=false 15 jdbc.testOnReturn=false 16 jdbc.poolPreparedStatements=false 17 jdbc.maxOpenPreparedStatements=100
5、创建Main方法
1 public static void main(String[] args) { 2 ApplicationContext ctx = new ClassPathXmlApplicationContext("configdb.xml"); 3 JdbcTemplate jdbcTemplate = (JdbcTemplate) ctx.getBean("jdbcTemplate"); 4 //插入数据 5 String sql = "insert into person(name,age) values(?,?)"; 6 jdbcTemplate.update(sql,"hzg",32); 7 8 //批量插入数据 9 sql = "insert into person(name,age) values(?,?)"; 10 List<Object[]> batchArgs = new ArrayList<>(); 11 batchArgs.add(new Object[]{"A",20}); 12 batchArgs.add(new Object[]{"B",30}); 13 batchArgs.add(new Object[]{"C",40}); 14 jdbcTemplate.batchUpdate(sql,batchArgs); 15 16 //根据ID查询数据 17 sql = "SELECT id,name,age FROM person WHERE id = ?"; 18 RowMapper<Person> personRowMapper = new BeanPropertyRowMapper<>(Person.class); 19 Person person = jdbcTemplate.queryForObject(sql,personRowMapper,1); 20 System.out.println(person); 21 22 //查询数据集合 23 sql = "SELECT id,name,age FROM person WHERE id > ?"; 24 RowMapper<Person> rowMapper = new BeanPropertyRowMapper<>(Person.class); 25 List<Person> personList= jdbcTemplate.query(sql,rowMapper,1); 26 System.out.println(personList); 27 28 //查询具体的值 29 sql = "SELECT count(1) FROM person WHERE id > ?"; 30 int excuteCount = jdbcTemplate.queryForObject(sql,Integer.class,1); 31 System.out.println(excuteCount); 32 }
标签:arch gdb max core 命名参数 div add 导入 font
原文地址:http://www.cnblogs.com/hzg110/p/6828622.html