多平台统一管理软件接口,如何实现多平台统一管理软件接口
251
2022-12-05
JDBC Template基本使用方法详解
1.使用maven引用依赖
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.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
System.out.println(names);
}
public void testQueryMap1(){
String sql = "select * from student where id = ?";
Map
System.out.println(stu);
}
public void testQueryMap2(){
String sql = "select * from student";
List
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
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小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~