1.应用开发的分层
1.1controller层相关注解
1.2实体类相关注解
1.3服务层
1.4数据层
关于注解中的参数
2.Mybatis-plus
一个mybatis的增强工具
1)引入mybatis依赖
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
2)增加Mybatis-plus规范的服务层和数据层
mapper接口:BaseMapper
public interface CustomerMapper extends BaseMapper<Customer> {
}
service接口:Iservice
public interface CustomerService extends IService<Customer> {
}
重写service实现类 ServiceImpl
@Service
public class CustomerImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {
}
总结:只要加上这三个继承,在controller中直接就可以使用CRUD命令
3)在Bean上加入对主键的标识
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Customer {
@TableId(value = "id",type = IdType.AUTO) //标识根据数据库自增生成主键
String id;
String name;
int age;
}
4)Controller直接调用封装方法
3.动态数据源
动态数据源可以实现在同一个项目中支持多种数据源,并能灵活切换不同的数据源
3.1相关注解
1)添加依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.3.2</version>
</dependency>
2)定义多个数据源
spring.datasource.dynamic.datasource.mysql0111.url=jdbc:mysql://hadoop102:3306/user_profile_manager_0111?characterEncoding=utf-8&useSSL=false
spring.datasource.dynamic.datasource.mysql0111.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.dynamic.datasource.mysql0111.username=root
spring.datasource.dynamic.datasource.mysql0111.password=123456
spring.datasource.dynamic.datasource.mysql0222.url=jdbc:mysql://hadoop102:3306/user_profile_manager_0222?characterEncoding=utf-8&useSSL=false
spring.datasource.dynamic.datasource.mysql0222.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.dynamic.datasource.mysql0222.username=root
spring.datasource.dynamic.datasource.mysql0222.password=123456
3)在service层和mapper的类上增加默认的数据源
@Service
@DS("mysql0111")
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {
@Mapper
@DS("mysql0111")
public interface CustomerMapper extends BaseMapper<Customer> {
4)在特定的方法上增加特定数据源
@Insert("insert into customer(name,age) values (#{customer.name}, #{customer.age} )")
@DS("mysql0222")
public void insertCustomer0111(@Param("customer") Customer customer);