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://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.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;
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。