Skip to content

Commit 3d7aa43

Browse files
committed
Update Java Notes
1 parent 4a7e18e commit 3d7aa43

File tree

4 files changed

+108
-225
lines changed

4 files changed

+108
-225
lines changed

Distributed.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3721,7 +3721,7 @@ Consumer 限流机制:
37213721

37223722
TTL 全称 Time To Live(存活时间/过期时间)
37233723

3724-
* 当消息到达存活时间后,还没有被消费,会被自动清除
3724+
* 当消息到达存活时间后,还没有被消费,会被自动清除
37253725

37263726
* RabbitMQ可以**对消息设置过期时间**,也可以**对整个队列(Queue)设置过期时间**
37273727

Frame.md

Lines changed: 13 additions & 209 deletions
Original file line numberDiff line numberDiff line change
@@ -4484,6 +4484,8 @@ Mybatis核心配置文件消失
44844484

44854485
业务发起使用spring上下文对象获取对应的bean
44864486

4487+
**原理**:DAO接口不需要去创建实现类,因为MyBatis-Spring提供了一个动态代理的实现**MapperFactoryBean**,这个类可以让你直接注入数据映射器接口到service层 bean 中,底层将会为你创建JDK代理
4488+
44874489
* pom.xml,导入坐标
44884490

44894491
```xml
@@ -5212,209 +5214,7 @@ public class ClassName {
52125214

52135215

52145216

5215-
### 整合框架
5216-
5217-
#### Mybatis
5218-
5219-
**原理**:DAO接口不需要去创建实现类,因为MyBatis-Spring提供了一个动态代理的实现**MapperFactoryBean**,这个类可以让你直接注入数据映射器接口到service层 bean 中,底层将会为你创建JDK代理
5220-
5221-
![](https://gitee.com/seazean/images/raw/master/Frame/IoC注解整合MyBatis图解.png)
5222-
5223-
* pom.xml
5224-
5225-
```xml
5226-
<dependencies>
5227-
<dependency>
5228-
<groupId>org.mybatis</groupId>
5229-
<artifactId>mybatis</artifactId>
5230-
<version>3.5.3</version>
5231-
</dependency>
5232-
<dependency>
5233-
<groupId>mysql</groupId>
5234-
<artifactId>mysql-connector-java</artifactId>
5235-
<version>5.1.47</version>
5236-
</dependency>
5237-
<dependency>
5238-
<groupId>org.springframework</groupId>
5239-
<artifactId>spring-context</artifactId>
5240-
<version>5.1.9.RELEASE</version>
5241-
</dependency>
5242-
<dependency>
5243-
<groupId>org.springframework</groupId>
5244-
<artifactId>spring-jdbc</artifactId>
5245-
<version>5.1.9.RELEASE</version>
5246-
</dependency>
5247-
<dependency>
5248-
<groupId>com.alibaba</groupId>
5249-
<artifactId>druid</artifactId>
5250-
<version>1.1.16</version>
5251-
</dependency>
5252-
<dependency>
5253-
<groupId>org.mybatis</groupId>
5254-
<artifactId>mybatis-spring</artifactId>
5255-
<version>1.3.0</version>
5256-
</dependency>
5257-
</dependencies>
5258-
```
5259-
5260-
* java / domain
5261-
5262-
```java
5263-
public class Account implements Serializable {
5264-
private Integer id;
5265-
private String name;
5266-
private Double money;
5267-
.....
5268-
}
5269-
```
5270-
5271-
* java / dao / AccountDao
5272-
5273-
```java
5274-
public interface AccountDao {
5275-
@Insert("insert into account(name,money) values(#{name},#{money})")
5276-
void save(Account account);
5277-
5278-
@Delete("delete from account where id = #{id} ")
5279-
void delete(Integer id);
5280-
5281-
@Update("update account set name = #{name},money = #{money} where id=#{id}")
5282-
void update(Account account);
5283-
5284-
@Select("select * from account")
5285-
List<Account> findAll();
5286-
5287-
@Select("select * from account where id = #{id} ")
5288-
Account findById(Integer id);
5289-
}
5290-
```
5291-
5292-
* java / service
5293-
5294-
```java
5295-
public interface AccountService {
5296-
void save(Account account);
5297-
void delete(Integer id);
5298-
void update(Account account);
5299-
List<Account> findAll();
5300-
Account findById(Integer id);
5301-
}
5302-
```
5303-
5304-
```java
5305-
@Service("accountService")
5306-
//@Component("accountService")
5307-
public class AccountServiceImpl implements AccountService {
5308-
@Autowired //自动装配
5309-
private AccountDao accountDao;
5310-
5311-
public void save(Account account) {
5312-
accountDao.save(account);
5313-
}
5314-
5315-
public void update(Account account){
5316-
accountDao.update(account);
5317-
}
5318-
5319-
public void delete(Integer id) {
5320-
accountDao.delete(id);
5321-
}
5322-
5323-
public Account findById(Integer id) {
5324-
return accountDao.findById(id);
5325-
}
5326-
5327-
public List<Account> findAll() {
5328-
return accountDao.findAll();
5329-
}
5330-
}
5331-
```
5332-
5333-
* resources / jdbc.properties
5334-
5335-
```properties
5336-
jdbc.driver=com.mysql.jdbc.Driver
5337-
jdbc.url=jdbc:mysql://192.168.2.185:3306/spring_db?useSSL=false
5338-
jdbc.username=root
5339-
jdbc.password=123456
5340-
```
5341-
5342-
* java / config / SpringConfig JDBCConfig MyBatisConfig
5343-
5344-
```java
5345-
@Configuration
5346-
@ComponentScan({"config","dao","domain","service"})
5347-
@PropertySource("classpath:jdbc.properties")
5348-
@Import({JDBCConfig.class, MyBatisConfig.class})
5349-
public class SpringConfig {
5350-
}
5351-
```
5352-
5353-
```java
5354-
public class JDBCConfig {
5355-
@Value("${jdbc.driver}")
5356-
private String driver;
5357-
@Value("${jdbc.url}")
5358-
private String url;
5359-
@Value("${jdbc.username}")
5360-
private String userName;
5361-
@Value("${jdbc.password}")
5362-
private String password;
5363-
5364-
@Bean("dataSource")
5365-
public DataSource getDataSource(){
5366-
DruidDataSource ds = new DruidDataSource();
5367-
ds.setDriverClassName(driver);
5368-
ds.setUrl(url);
5369-
ds.setUsername(userName);
5370-
ds.setPassword(password);
5371-
return ds;
5372-
}
5373-
}
5374-
```
5375-
5376-
```java
5377-
public class MyBatisConfig {
5378-
@Bean //DataSource使用自动装配
5379-
public SqlSessionFactoryBean getSSFB(@Autowired DataSource dataSource){
5380-
SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
5381-
ssfb.setTypeAliasesPackage("domain");
5382-
ssfb.setDataSource(dataSource);
5383-
return ssfb;
5384-
}
5385-
@Bean
5386-
public MapperScannerConfigurer getMapperScannerConfigurer(){
5387-
MapperScannerConfigurer msc = new MapperScannerConfigurer();
5388-
msc.setBasePackage("dao");
5389-
return msc;
5390-
}
5391-
5392-
}
5393-
```
5394-
5395-
* 测试类
5396-
5397-
```java
5398-
public class App {
5399-
public static void main(String[] args) {
5400-
ApplicationContext ctx = new AnnotationConfigApplicationContext(
5401-
SpringConfig.class);
5402-
AccountService as = (AccountService) ctx.getBean("accountService");
5403-
Account account = as.findById(3);
5404-
System.out.println(account);
5405-
}
5406-
}
5407-
```
5408-
5409-
5410-
5411-
5412-
5413-
***
5414-
5415-
5416-
5417-
#### Junit
5217+
##### Junit
54185218

54195219
Spring接管Junit的运行权,使用Spring专用的Junit类加载器,为Junit测试用例设定对应的spring容器
54205220

@@ -7424,7 +7224,7 @@ TransactionDefinition 接口中定义了五个表示隔离级别的常量:
74247224
MySQL InnoDB 存储引擎的默认支持的隔离级别是 **REPEATABLE-READ(可重读)**
74257225
74267226
**分布式事务**:允许多个独立的事务资源(transactional resources)参与到一个全局的事务中
7427-
事务资源通常是关系型数据库系统,但也可以是其他类型的资源全局事务要求在其中的所有参与的事务要么都提交,要么都回滚,这对于事务原有的ACID要求又有了提高
7227+
事务资源通常是关系型数据库系统,但也可以是其他类型的资源全局事务要求在其中的所有参与的事务要么都提交,要么都回滚,这对于事务原有的ACID要求又有了提高
74287228
74297229
在使用分布式事务时,InnoDB存储引擎的事务隔离级别必须设置为SERIALIZABLE
74307230
@@ -12086,13 +11886,12 @@ SSM(Spring+SpringMVC+MyBatis)
1208611886
<!--加载properties文件-->
1208711887
<context:property-placeholder location="classpath*:jdbc.properties"/>
1208811888

12089-
<!--数据源-->
11889+
<!--加载数据源-->
1209011890
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
1209111891
<property name="driverClassName" value="${jdbc.driver}"/>
1209211892
<property name="url" value="${jdbc.url}"/>
1209311893
<property name="username" value="${jdbc.username}"/>
1209411894
<property name="password" value="${jdbc.password}"/>
12095-
1209611895
</bean>
1209711896

1209811897
<!--整合mybatis到Spring-->
@@ -12125,7 +11924,7 @@ SSM(Spring+SpringMVC+MyBatis)
1212511924
</bean>
1212611925
</beans>
1212711926
```
12128-
11927+
1212911928
* 业务层接口开启事务
1213011929

1213111930
```java
@@ -12483,6 +12282,10 @@ public class ProjectExceptionAdivce {
1248312282

1248412283
### UserDao.xml
1248512284

12285+
注解:@Param
12286+
12287+
作用:当SQL语句需要多个(大于1)参数时,用来指定参数的对应规则
12288+
1248612289
* 注解替代UserDao映射配置文件:dao.UserDao
1248712290

1248812291
```java
@@ -12525,6 +12328,8 @@ public class ProjectExceptionAdivce {
1252512328

1252612329
### applicationContext.xml
1252712330

12331+
![](https://gitee.com/seazean/images/raw/master/Frame/IoC注解整合MyBatis图解.png)
12332+
1252812333
* JdbcConfig
1252912334

1253012335
```java
@@ -12600,7 +12405,6 @@ public class ProjectExceptionAdivce {
1260012405
@Configuration
1260112406
//等同于<context:component-scan base-package="com.itheima">
1260212407
@ComponentScan(value = {"config","dao","service","system"},excludeFilters =
12603-
//等同于<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
1260412408
@ComponentScan.Filter(type= FilterType.ANNOTATION,classes = {Controller.class}))
1260512409
//等同于<context:property-placeholder location="classpath*:jdbc.properties"/>
1260612410
@PropertySource("classpath:jdbc.properties")
@@ -12619,9 +12423,9 @@ public class ProjectExceptionAdivce {
1261912423
}
1262012424
}
1262112425
```
12622-
1262312426

1262412427

12428+
1262512429
***
1262612430

1262712431

0 commit comments

Comments
 (0)