spring boot学习笔记

Spring家族

Spring Framework
Spring boot
spring cloud

快速开始一个spring项目

@SpringBootApplication
@RestController//引入了web
public class TeeApplication {

	public static void main(String[] args) {
		SpringApplication.run(TeeApplication.class, args);
	}
	@RequestMapping("/hello")//hello路径
	public String hello(){
		return "hello spring";
	}
}

在terminal中输入,检查项目启动是否成功

//路径访问
curl http://localhost:8080/hello
//健康检查
curl http://localhost:8080/actuator/health
//生成一个可执行的jar包
mvn clean package -Dmaven.test.skip
//执行该jar包
java -jar jar-package-name

配置数据源

	@Override
	public void run(String... args) throws Exception{
		showConnection();
	}
	private void  showConnection() throws SQLException {
		log.info(dataSource.toString());
		Connection con = dataSource.getConnection();
		log.info(con.toString());
		con.close();
	}

在浏览器中输入,查看配置的beans

http://localhost:8080/actator/beans

spring boot
spring boot
数据库查询

	private void showdata(){
		jdbcTemplate.queryForList("SELECT * FROM FOO").forEach(row->log.info(row.toString()));
    }

多数据源配置,排除spring boot的自动配置

//@SpringBootApplication(exclude = {
		DataSourceAutoConfiguration.class,
		DataSourceTransactionManagerAutoConfiguration.class,
		JdbcTemplateAutoConfiguration.class
})
//@Slf4j
public class TeeApplication{

手动配置DataSource

	@Bean
	@ConfigurationProperties("foo.datasource")
	public DataSourceProperties fooDataSourceProperties(){
		return new DataSourceProperties();
	}
	@Bean
	public DataSource fooDataSource(){
		DataSourceProperties dataSourceProperties = fooDataSourceProperties();
		log.info("foo datasource:{}",dataSourceProperties.getUrl());
		return 	dataSourceProperties.initializeDataSourceBuilder().build();
	}
	@Bean
	@Resource
	public PlatformTransactionManager fooTxManager(DataSource fooDataSource){
		return new DataSourceTransactionManager(fooDataSource);
	}
	@Bean
	@ConfigurationProperties("bar.datasource")
	public DataSourceProperties barDataSourceProperties(){
		return new DataSourceProperties();
	}
	@Bean
	public DataSource barDataSource(){
		DataSourceProperties dataSourceProperties = barDataSourceProperties();
		log.info("bar datasource:{}",dataSourceProperties.getUrl());
		return 	dataSourceProperties.initializeDataSourceBuilder().build();
	}
	@Bean
	@Resource
	public PlatformTransactionManager barTxManager(DataSource barDataSource){
		return new DataSourceTransactionManager(barDataSource);
	}

application.properties中写入数据源

foo.datasource.url = jdbc:h2:mem:foo
foo.datasource.username = sa
foo.datasource.password =

bar.datasource.url = jdbc:h2:mem:bar
bar.datasource.username = sa
bar.datasource.password =

数据源连接池

HikariCP
hikaricp
hikaricp
AilibabaDruid
AilibabaDruid
application.properties中配置

spring.datasource.url=jdbc:h2:mem:foo
spring.datasource.username=sa
spring.datasource.password=dz1bVEiHxhLeyHjC3kT779p500fj0kg3a/xpctOC5HwYvlMaR5FPO/0pAvudExqqBkvKzD3bf83M4Y/vQ4nSAQ==
spring.datasource.druid.initialSize=5
spring.datasource.druid.minIdle=5
spring.datasource.druid.maxActive=20
spring.datasource.druid.filters=conn,config,stat,slf4j

spring.datasource..druidconnectionProperties=config.decrypt=true;config.decrypt.key=${public-key}
spring.datasource.druid.filter.config.enabled=true

spring.datasource.druid.testWhileIdle=false
spring.datasource.druid.testOnBorrow=false
spring.datasource.druid.testOnReturn=false

pom文件中排除HikariCP

<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
			<exclusions>
				<exclusion>
					<groupId>org.apache.tomcat</groupId>
					<artifactId>tomcat-jdbc</artifactId>
				</exclusion>
				<exclusion>
					<groupId>com.zaxxer</groupId>
					<artifactId>HikariCP</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

durid
在这里插入图片描述

public void selectForUpdate(){
        log.info("select {}",jdbcTemplate.queryForObject("SELECT ID FROM FOO WHERE ID=1 FOR UPDATE",Long.class));
        try {
            Thread.sleep(200);
        }catch (InterruptedException e){
            //log.info("exception");
        }
    }
 //主函数中
 new Thread(()->fooservice.selectForUpdate()).start();
 new Thread(()->fooservice.selectForUpdate()).start();

数据库操作
在这里插入图片描述

常用的Bean注解
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

jdbc操作
在这里插入图片描述
数据库操作
Foo对象

import lombok.Builder;
import lombok.Data;

@Data//set get 方法
@Builder// 构造方法
public class Foo {
    private Long id;
    private String bar;
}

数据操作

@Slf4j
@Repository
public class FooDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private SimpleJdbcInsert simpleJdbcInsert;//jdbc操作的辅助类

    public void insertData(){
        Arrays.asList("a","b").forEach(bar ->{
            jdbcTemplate.update("INSERT INTO FOO (BAR) VALUES (?)",bar);
        });
        HashMap<String,String> row = new HashMap<>();
        row.put("BAR","d");
        Number id = simpleJdbcInsert.executeAndReturnKey(row);
        log.info("ID of d: {}",id.longValue());
    }
    public void listData(){
        log.info("count: {}",jdbcTemplate.queryForObject("SELECT COUNT(*) FROM FOO",Long.class));
        List<String> list = jdbcTemplate.queryForList("SELECT BAR FROM FOO", String.class);
        list.forEach(s -> log.info("BAR {}",s));

        List<Foo> foolist = jdbcTemplate.query("SELECT * FROM FOO",new RowMapper<Foo>(){
            @Override
            public Foo mapRow(ResultSet rs, int rowNum) throws SQLException{//rowmap与result连接
                return Foo.builder()
                        .id(rs.getLong(1))
                        .bar(rs.getString(2))
                        .build();
            }
        });
        foolist.forEach(f -> log.info("Foo: {}",f));
    }
}

批处理方法
在这里插入图片描述

jdbcTemplate.batchUpdate("INSERT INTO FOO (BAR) VALUES (?)",
                new BatchPreparedStatementSetter() {
                    @Override
                    public void setValues(PreparedStatement preparedStatement, int i) throws SQLException {
                        preparedStatement.setString(1,"b-"+i);
                    }
                    @Override
                    public int getBatchSize() {//批处理次数
                        return 2;
                    }
                }
        );
        List<Foo> list = new ArrayList<>();
        list.add(Foo.builder().id(100L).bar("b-100").build());
        list.add(Foo.builder().id(101L).bar("b-101").build());
        namedParameterJdbcTemplate
                .batchUpdate("INSERT INTO FOO (ID,BAR) VALUES (:id,:bar)",
                        SqlParameterSourceUtils.createBatch(list));

事务抽象
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
编程式事务
在这里插入图片描述

public void run(String... args) throws Exception{
		log.info("count before transaction {}",getCount());
		transactionTemplate.execute(new TransactionCallbackWithoutResult(){
			@Override
			protected void doInTransactionWithoutResult(TransactionStatus transactionStatus){
				jdbcTemplate.execute("INSERT INTO FOO (ID,BAR) VALUES (4,'AA1')");
				log.info("count in transaction {}",getCount());
				transactionStatus.setRollbackOnly();
			}
		});

		log.info("count after transaction {}",getCount());
  }

声明式事务
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

测试类

   @Autowired
    private JdbcTemplate jdbcTemplate;
    //@Autowired
    //private FooService fooService;
    @Override
    @Transactional
    public void insertRecord(){
        jdbcTemplate.execute("INSERT INTO FOO (BAR) VALUES ('aaa1')");
    }
    @Override
    @Transactional(rollbackFor = UnexpectedRollbackException.class)
    public void insertThenRollback() throws UnexpectedRollbackException{
        jdbcTemplate.execute("INSERT INTO FOO (BAR) VALUES ('bbb1')");
        throw new UnexpectedRollbackException("sss");
    }
    @Override
    public void invokeInsertThenRollback() throws UnexpectedRollbackException{
        //insertThenRollback();//没有aop代理增强,没有事务
        ((FooService) (AopContext.currentProxy())).insertThenRollback();//当前类的代理对象
        //fooService.insertThenRollback();//注入实例
    }

测试代码

public void run(String... args) throws Exception{
		fooservice.insertRecord();
		log.info("aaa1 {}",
				jdbcTemplate.queryForList("SELECT COUNT(*) FROM FOO WHERE BAR='aaa1'",long.class));
		try{
			fooservice.insertThenRollback();
		}catch(Exception e) {
			log.info("bbb1 {}",
					jdbcTemplate.queryForList("SELECT COUNT(*) FROM FOO WHERE BAR='bbb1'",long.class));
		}
		try{
			fooservice.invokeInsertThenRollback();
		}catch(Exception e) {
			log.info("bbb1 {}",
					jdbcTemplate.queryForList("SELECT COUNT(*) FROM FOO WHERE BAR='bbb1'",long.class));
		}
	}

在这里插入图片描述

@Override
    @Transactional(rollbackFor = UnexpectedRollbackException.class,propagation = Propagation.REQUIRES_NEW)
    //@Transactional(rollbackFor = UnexpectedRollbackException.class,propagation = Propagation.NESTED)
    public void insertThenRollback() throws UnexpectedRollbackException{
        jdbcTemplate.execute("INSERT INTO FOO (BAR) VALUES ('bbb1')");
        throw new UnexpectedRollbackException("sss");
    }

    @Override
    public void invokeInsertThenRollback() throws UnexpectedRollbackException{
        //insertThenRollback();//没有aop代理增强,没有事务
        ((FooService) (AopContext.currentProxy())).insertThenRollback();//当前类的代理对象
        //fooService.insertThenRollback();//注入实例
    }

JDBC异常
在这里插入图片描述
在这里插入图片描述
测试类

@RunWith(SpringRunner.class)
@SpringBootTest
public class TeeApplicationTests {
	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Test(expected = CustomDuplicatedKeyException.class)
	public void testThrowingCustomException() {
		jdbcTemplate.execute("INSERT INTO FOO (ID,BAR) VALUES (3,'A')");
		jdbcTemplate.execute("INSERT INTO FOO (ID,BAR) VALUES (3,'B')");
	}
}

pom文件错误码配置

 <property name="customTranslations">
            <bean class="org.springframework.jdbc.support.CustomSQLErrorCodesTranslation">
                <property name="errorCodes" value="23001,23505"/>
                <property name="exceptionClass" value="springhtwo.tee.CustomDuplicatedKeyException"/>
            </bean>
        </property>

错误类

public class CustomDuplicatedKeyException extends DuplicateKeyException {
    public CustomDuplicatedKeyException(String msg){
        super(msg);
    }
    public CustomDuplicatedKeyException(String msg,Throwable cause){
        super(msg,cause);
    }
}

在这里插入图片描述
在这里插入图片描述
多数据库、分库分表、读写分离
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
springboot学习笔记 spring基础 Spring概述 Spring的简史 xml配置 注解配置 java配置 Spring概述 Spring的模块 核心容器CoreContainer Spring-Core Spring-Beans Spring-Context Spring-Context-Support Spring-Expression AOP Spring-AOP Spring-Aspects Messaging Spring-Messaging WEB Spring-Web Spring-Webmvc Spring-WebSocket Spring-Webmvc-Portlet 数据访问/集成(DataAccess/Intefration) Spring-JDBC Spring-TX Spring-ORM Spring-OXM Spring-JMS Spring的生态 Spring Boot Spring XD Spring Cloud Spring Data Spring Integration Spring Batch Spring Security Spring HATEOAS Spring Social Spring AMQP Spring Mobile Spring for Android Spring Web Flow Spring Web Services Spring LDAP Spring Session Spring项目快速搭建 Maven简介 Maven安装 Maven的pom.xml dependencies dependency 变量定义 编译插件 Spring项目的搭建 Spring Tool Suite https://spring.io/tools/sts/all IntelliJ IDEA NetBeans https://netbeans.org/downloads/ Spring基础配置 依赖注入 声明Bean的注解 @Component组件,没有明确的角色 @Service在业务逻辑层(service层) @Repository在数据访问层(dao层) @Controller在展现层(MVC→SpringMVC) 注入Bean的注解 @Autowired:Spring提供的注解 @Inject:JSR-330提供的注解 @Resource:JSR-250提供的注解 Java配置 @Configuration声明当前类是一个配置类 @Bean注解在方法上,声明当前方法的返回值为一个Bean AOP @Aspect 声明是一个切面 拦截规则@After @Before @Around PointCut JoinPoint Spring常用配置 Bean的Scope Singleton Prototype Request Session GlobalSession SpringEL和资源调用 注入普通字符 注入操作系统属性 注入表达式云算结果 注入其他Bean的属性 注入文件内容 注入网址内容 注入属性文件 Bean的初始化和销毁 Java配置方式 注解方式 Profile @Profile 通过设定jvm的spring.profiles.active参数 web项目设置在Servlet的context parameter中 事件Application Event 自定义事件,集成ApplicationEvent 定义事件监听器,实现ApplicationListener 使用容器发布事件 Spring高级话题 Spring Aware BeanNameAware BeanFactoryAware
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值