1、事务
JdbcTemplate
xml配置文件中引入外部属性文件创建数据源对象
<context:property-placeholder location="classpath:jdbc.properties"></context>
数据库增、删、改
编写sql语句
调用jdbcTemplate。Update(sql,可变参数)
查询
Emp emp=jdbcTemplate.queryForObject(String sql,new BeanPropertyRowMapper<>(Emp.class),实参);
List<Emp> list=jdbc.Template.query(sql,new BeanPropertyRowMapper<>(Emp.class))
返回单个值:
Public void ---
{
String sql=" "
Integer count=jdbcTemplate.queryForObject(sql,Integer.class_返回值类型.class)
}
2、基于注解的声明式事务
xml文件中加入
<bean id="xxx" class=" ">
<property name="datasource" ref="druidDataSource"></property>
</bean>
开启事务的注解驱动:
通过注解@Transactional所标识的方式或标识类中的方法,都会呗事务管理器管理
transaction-manager默认值transactionManager
<tx:annotation-driven [transaction-manager="xxx"]_默认,可以省略不写/>
@Transactional注解标识的位置:
@Transactional标在方法上,只影响该方法
@Transactional标在类上,影响类中的所有方法
@Transactional(属性)
(1)只读
readOnly=true,只可查询
(2)超时
timeOut=时间值,单位秒,默认-1,永不超时,若操作时间超过设定时间值,回滚释放资源
(3)回滚策略
rollbackFor:设置一个class类型的对象 norollbackFor:遇到不回滚
rollbackForClassName:设置一个字符串类型的全类名 norollbackForClassName:遇到不回滚
(4)隔离级别
READ UNCOMMITTED:读未提交
允许Transaction_1读取Transaction_2未提交的修改数据
READ COMMITTED:读已提交
允许Transaction_1读取Transaction_2已提交的修改数据
REPEATABLE READ:可重复读 字段
Transaction_1操作期间,其它不可对字段更新
SERIALIZABLE:串行化 表
Transaction_1对表操作期间,其它不可对表更新
属性名称 isolation=属性名
(5)传播行为
propagation=Propagation.REQUIRED 默认情况,表示若当前线程有事务可用则在该事务中运行
propagation=Propagation.REQUIRES_NEW,表示无论怎样都开启一个新事务
全注解配置--->配置类
@Configuration---配置类
@ComponentScan("com.atgui.spring6.tx")
@EnableTransactionManagment---事务管理
public class SpringConfig{
@Bean---方法
}
3、基于xml的声明式事务
配置事务增强:
<tx:advice id=" " transaction-manager="transactionManager_事务管理器id">
<tx:attributes>
<tx:method name="get*" read-only="true"/> //说明以get开头的方法只能读
</tx:attributes>
</tx:advice>
Resource接口:
Resource接口位于org.springframwork.core.io中,用于抽象对低级资源的访问
UrlResource实现类,实现对网络路径资源的访问
http开头 file开头
file:atgui.txt---建在项目的根目录下
ClassPathResource:访问类路径下的资源
FileSystemResource:文件资源系统
ResourceLoader接口:
该接口实现类的实例可以获得一个Resource实例
ResourceLoaderAware:
该接口实现类的实例可以获得一个ResourceLoader的引用
将Resource作为属性:
(1)写一个类A
(2)bean.xml
<bean id=" " class="A的路径">
<property name=随意 value="classpath:atgui.txt"/>
</bean>
(3)测试类
国际化 il8n
java.util.Locale用于指定当前用户所属语言环境等信息
配置文件命名规则
basename-必须_language-可选_country-可选.properties,如果同时存在message.properties与message_zh_CN.poperties且提供的locale符合,zh_CN优先
**注意:所有配置文件必须放在ClassPath中
ResourceBundle.getBundle(basename,new Locale(language,country)/Locale locale)