10.spring:常用的数据库代码.....(不长使用了解即可)

时间:2023-11-19 11:17:32

测试代码:
ApplicationContext ctx = new ClassPathXmlApplicationContext("JDBC.xml");
DataSource datasource = (DataSource)ctx.getBean("dataSource",DataSource.class);
//测试能否连上
//System.out.println(datasource);

JdbcTemplate jdbcTemplate = (JdbcTemplate) ctx.getBean("jdbcTemplate");

//执行update
//String sql = "update student set sname=? where id =?";
//jdbcTemplate.update(sql, "MrChengs",3);
String sql = "update student set sname = ? where id = ?";
jdbcTemplate.update(sql,"Mc",4 );

//批量更新:可以执行的目标方法insert update delete
//batchArgs:是Object[] 事物List类型,因为需要修改一条Object的数组,那么多条就需要多个数组
//String sql = "insert student(sname,sclass) values(?,?)";
//List<Object []> batchArgs = new ArrayList<>();
//batchArgs.add(new Object []{"Rui",1221});
//batchArgs.add(new Object []{"Ruis",1212});
//jdbcTemplate.batchUpdate(sql, batchArgs);
String sql1 = "insert into student(sname,sclass) values(?,?)";
List<Object []> batchArgs = new ArrayList<Object []>();
batchArgs.add(new Object [] {"asd",12});
batchArgs.add(new Object [] {"qwe",321});
jdbcTemplate.batchUpdate(sql1, batchArgs);

//从数据库中获取一条记录,实际得到的是一个对象
//queryForObject(String sql, Object[] args, RowMapper<T> rowMapper)
//其中的RowMapper指定如何去映射结果集的行,常用的实现类为BeanPropertyRowMapper
//使用SQL中的列的别名完成列名和属性别名的映射
//不支持级联属性:jdbcTemplate到底是jdbc的小工具,而不是ORM框架
//String sql = "select * from student where id =?";
//RowMapper<Student> rowMapper = new BeanPropertyRowMapper<Student>(Student.class);
//Student student = jdbcTemplate.queryForObject(sql, rowMapper, 3);
//System.out.println(student);
String sql2 = "select * from student where id = ?";
RowMapper<Student> rowMapper = new BeanPropertyRowMapper<Student>();
Student stu = jdbcTemplate.queryForObject(sql2, rowMapper);

//查到实体类的集合
//.query(String sql, RowMapper<Student> rowMapper, Object... args)
//String sql = "select * from student where id < ?";
//RowMapper<Student> rowMapper = new BeanPropertyRowMapper<Student>(Student.class);
//List<Student> stu = jdbcTemplate.query(sql, rowMapper,10);
//System.out.println(stu);

//获取单个列数据的值
//queryForObject(String sql, Class<Long> requiredType)
//String sql = "select count(id) from student";
//long count = jdbcTemplate.queryForObject(sql, Long.class);
//System.out.println(count);

///////////////////////////////////////////////////////////////////
//具名参数
//NamedParameterJdbcTemplate
//可以为参数起名字,参数多的情况下,这种方法的稳定正确性
NamedParameterJdbcTemplate namedParameterJdbcTemplate = (NamedParameterJdbcTemplate) ctx.getBean("namedParameterJdbcTemplate");

//.update(String sql, SqlParameterSource paramSource)
//String sql = "insert into student(sname,sclass) values(:sname,:sclass)";
//Map<String, Object> paramMap = new HashMap<String, Object>();
//paramMap.put("sname", "jack");
//paramMap.put("sclass", 3424);
//namedParameterJdbcTemplate.update(sql, paramMap);

//update(String sql, SqlParameterSource paramSource)
//String sql = "insert into student(sname,sclass) values(:sname,:sclass)";
//Student stu = new Student();
//stu.setSname("Tom");
//stu.setSclass(0000);
//SqlParameterSource paramSource = new BeanPropertySqlParameterSource(stu);
//namedParameterJdbcTemplate.update(sql, paramSource);

//事务简介
//用来确保数据的完整性和一致性
//他们被当作一个独立的单元,要么全部起作用,要么全部不起作用
//事物的四个特性
//-->原子性,一致性,隔离性,持久性

//从不同的事物管理机制API中抽取了一整套事物机制,开发人员不必了解底层的事物的Api,就可以利用这些事物管理机制
//有了这些事物管理机制,事物管理代码就能独立于特定的事物技术了

xml文件的配置:

<bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
  <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  <property name="url" value="jdbc:mysql://127.0.0.1:3307/shijian" />
  <property name="username" value="root" />
  <property name="password" value="1234" />
</bean>

<!-- 配置Spring的JdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  <property name="dataSource" ref="dataSource"></property>
</bean>

<!--namedParameterJdbcTemplate具名参数,可以使用具名参数,其没有无参的构造器,必须为其构造函数指定参数 -->
  <bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
  <constructor-arg ref="dataSource" ></constructor-arg>
</bean>