java jdbc 框架,JAVA 框架-Spring-jdbc

Spring数据库查询模版---JdbcTemplate,该类是spring提供的数据库查询类,不如mybatis好用

配置文件:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:p="http://www.springframework.org/schema/p"

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-4.3.xsd">

location="classpath:conf/db.properties" />

class="org.springframework.jdbc.datasource.DriverManagerDataSource"

p:username="${jdbc.user}" p:password="${jdbc.pword}"

p:url="${jdbc.url}" p:driverClassName="${jdbc.classname}">

p:dataSource-ref="dataSource">

数据库信息单独写在一个文件里:

#不要写username

jdbc.user=test0315

jdbc.pword=123456

jdbc.url=jdbc:oracle:thin:@localhost:1521:xe

jdbc.classname=oracle.jdbc.OracleDriver

增删改操作

update(String sql, Object... obj);

批量添加

batchUpdate(sql, List lists);

查询

queryForMap(String sql, Object... obj); // 查询出一个Map集合, 返回结果必须并且只有一个, 否则报错

queryForObject(String sql, RowMapper rowMapper); // 查询出一个对象, 返回结果必须并且只有一个, 否则报错, 这个方法必须添加一个RowMapper参数作为返回类型的映射

queryForList(String sql, Class> type); // 查询结果只能有一列, 并且只支持Integer和String类型的

queryForRowSqlSet(String sql, Object... obj);

query(String sql, new BeanPropertyRowMapper(T.class)); // 持久化查询

代码示例

package com.hanqi.test;

//引入包省略

class JunitTest {

private JdbcTemplate jdbcTemplate;

private ClassPathXmlApplicationContext c;

@BeforeEach

void setUp() throws Exception {

c = new ClassPathXmlApplicationContext("conf/spring.xml");

jdbcTemplate = c.getBean(JdbcTemplate.class);// 获取查询模板

}

@AfterEach

void tearDown() throws Exception {

c.close();

}

@Test

void test() {

// System.out.println(jdbcTemplate);

int r1 = jdbcTemplate.update("delete from appuser where ids = ?", 82);// 删除操作

int r2 = jdbcTemplate.update("insert into appuser(ids,uname,pword) values(sq_test.nextval,?,?)", "sina",

"1234");// 添加操作

// System.out.println(r);

String sql = "insert into appuser(ids,uname,pword) values(sq_test.nextval,?,?)";

List list = new ArrayList<>();

Object[] obj1 = { "qqq", "123" };

Object[] obj2 = { "www", "123" };

list.add(obj1);

list.add(obj2);

int[] a = jdbcTemplate.batchUpdate(sql, list);// 批量添加操作,sql语句会根据数据长度生成多条;

System.out.println(Arrays.toString(a));// 批量添加显示为[-2,-2],说明添加成功;

String sql2 = "select * from appuser where ids = ?";

Map map = jdbcTemplate.queryForMap(sql2, 61);

System.out.println(map);// 查询结果为map集合,键为列名

String sql3 = "select uname from appuser where ids = 65";

String b = jdbcTemplate.queryForObject(sql2, String.class);// 查什么类型写什么类型

System.out.println(b);

}

}

Spring声明式事务

在操作数据库的过程中如果出现异常,则进行事务回滚。

同时在数据库访问实现类上进行注解:@Transactional

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值