赞
踩
类一(user_dao):
- public interface user_dao {
- public void addmoney();
- public void reducemoney();
- public void query();
- }
类二(user_dao_impl):
- @Repository
- public class user_dao_impl implements user_dao {
- @Autowired
- private JdbcTemplate jdbcTemplate;
- @Override
- public void addmoney() {
- int a = jdbcTemplate.update("update users1 set money = money-100 where uname=?", "张三");
- System.out.println(a);
- }
-
- @Override
- public void reducemoney() {
- int i = jdbcTemplate.update("update users1 set money = money+100 where uname=?", "李四");
- System.out.println(i);
- }
-
- @Override
- public void query() {
- List<user> query = jdbcTemplate.query("select * from users1", new BeanPropertyRowMapper<>(user.class));
- System.out.println(query);
- }
- }
类三(pojo.user)
- public class user {
- private String uname;
- private Double money;
-
- public user(String uname, Double money) {
- this.uname = uname;
- this.money = money;
- }
-
- public user() {
- }
-
- public String getUname() {
- return uname;
- }
-
- public void setUname(String uname) {
- this.uname = uname;
- }
-
- public Double getMoney() {
- return money;
- }
-
- public void setMoney(Double money) {
- this.money = money;
- }
-
- @Override
- public String toString() {
- return "user{" +
- "uname='" + uname + '\'' +
- ", money=" + money +
- '}';
- }
- }
类四(UserService)
- @org.springframework.stereotype.Service
- @Transactional(readOnly = false)
- public class UserService {
- @Autowired
- private user_dao_impl user_dao_impl;
- public void testMoney(){
- user_dao_impl.addmoney();
- user_dao_impl.reducemoney();
- user_dao_impl.query();
- }
- }
配置类(模拟XML文件):
- @Configuration
- @ComponentScan(basePackages = "com")
- @EnableTransactionManagement
- public class DoConfig {
- @Bean
- public DruidDataSource getDataSource(){
- DruidDataSource druidDataSource = new DruidDataSource();
- druidDataSource.setUsername("root");
- druidDataSource.setUrl("jdbc:mysql:///users?useUnicode=true&characterEncoding=utf-8");
- druidDataSource.setDriverClassName("com.mysql.jdbc.Driver");
- druidDataSource.setPassword("123456");
- return druidDataSource;
- }
- @Bean
- public JdbcTemplate getJdbcTemplate(DruidDataSource druidDataSource){
- JdbcTemplate jdbcTemplate = new JdbcTemplate();
- jdbcTemplate.setDataSource(druidDataSource);
- return jdbcTemplate;
- }
- @Bean
- public DataSourceTransactionManager getTransactional(DruidDataSource druidDataSource){
- DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
- dataSourceTransactionManager.setDataSource(druidDataSource);
- return dataSourceTransactionManager;
- }
- }
测试类:
- public class Test01 {
- @Test
- public void test01(){
- ApplicationContext applicationContext = new AnnotationConfigApplicationContext(DoConfig.class);
- UserService userService = applicationContext.getBean("userService", UserService.class);
- userService.testMoney();
- }
- }
总结Spring中常见的注释:
@Component @Service 一般用在Service层 @Controller 一般用在Web层 @Repository 一般用在Dao层 @Transactional 开启事务一般用在Service层 @Configuration 通常用于全注解开发时写xml文件的类@ComponentScan 组件扫描
事务的4大特性:
(1).原子性:两个的操作要么都成功,要么都失败. (2).一致性:在操作中总数保持不变 (3).隔离性:多个人去操作数据,不会受其影响 (4).持久性:操作后数据会同步更新到数据库中
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。