JDBC Template基本使用方法详解

网友投稿 251 2022-12-05


JDBC Template基本使用方法详解

1.使用maven引用依赖

junit

junit

4.11

org.springframework

spring-context

5.2.5.RELEASE

org.springframework

spring-core

5.2.5.RELEASE

org.springframework

spring-beans

5.2.5.RELEASE

org.springframework

spring-aop

5.2.5.RELEASE

org.springframework

spring-jdbc

5.2.5.RELEASE

org.springframework

spring-tx

5.2.5.RELEASE

mysql

mysql-connector-java

8.0.19

2.编写spring的xml文件

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

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd">

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

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd">

3.ddl操作(建表之类)

import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import org.springframework.jdbc.core.JdbcTemplate;

public class test {

@Test

public void test(){

ApplicationContext applicationContext=new ClassPathXmlApplicationContext("myApplication.xml");

JdbcTemplate springTemplate = (JdbcTemplate) applicationContext.getBean("jdbcTemplate");

springTemplate.execute("create table test(id int,username varchar(10))");

}

}

4.增删改:

对应的使用例子

public void testUpdate(){

String sql = "insert into student(name,sex) values(?,?)";

jdbcTemplate.update(sql,new Object[]{"张飞","男"});

}

public void testUpdate2(){

String sql = "update student set sex=? where id=?";

jdbcTemplate.update(sql,"女",1003);

}

public void testBatchUpdate(){

String[] sqls={

"insert into student(name,sex) values('关羽','女')",

"insert into student(name,sex) values('刘备','男')",

"update student set sex='女' where id=2001"

};

jdbcTemplate.batchUpdate(sqls);

}

public void testBatchUpdate2(){

String sql = "insert into selection(student,course) values(?,?)";

List list = new ArrayList();

list.add(new Object[]{1005,1001});

list.add(new Object[]{1005,1003});

jdbcTemplate.batchUpdate(sql,list);

}

5.查询

public void testQuerySimple1(){

String sql = "select count(*) from student";

int count = jdbcTemplate.queryForObject(sql,Integer.class);

System.out.println(count);

}

public void testQuerySimple2(){

String sql = "select name from student where sex=?";

List names = jdbcTemplate.queryForList(sql,String.class,"女");

System.out.println(names);

}

public void testQueryMap1(){

String sql = "select * from student where id = ?";

Map stu = jdbcTemplate.queryForMap(sql,1003);

System.out.println(stu);

}

public void testQueryMap2(){

String sql = "select * from student";

List> stus = jdbcTemplate.queryForList(sql);

System.out.println(stus);

}

public void testQueryEntity1(){

String sql = "select * from student where id = ?";

Student stu = jdbcTemplate.queryForObject(sql, new StudentRowMapper(), 1004);

System.out.println(stu);

}

@org.junit.Test

public void testQueryEntity2(){

String sql = "select * from student";

List stus = jdbcTemplate.query(sql,new StudentRowMapper());

System.out.println(stus);

}

private class StudentRowMapper implements RowMapper{

public Student mapRow(ResultSet resultSet, int i) throws SQLException {

Student stu = new Student();

stu.setId(resultSet.getInt("id"));

stu.setName(resultSet.getString("name"));

stu.setSex(resultSet.getString("sex"));

stu.setBorn(resultSet.getDate("born"));

return stu;

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:SpringBoot Application注解原理及代码详解
下一篇:JAVA线程池专题(概念和作用)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~