目录
一.SpringBoot整合Mybatis与Mybatis-Plus
一.SpringBoot整合Mybatis与Mybatis-Plus
步骤:
1.坐标
XML
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>3.1.1</version>
</dependency>
注意:mp坐标添加后,mybatis坐标移除(mp坐标包含mybatis)
2.配置数据源以及mp
先导入德鲁伊数据源
XML
<!--druid坐标-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.8</version>
</dependency>
XML
#数据源
spring:
datasource:
url: jdbc:mysql://localhost:3306/库?serverTimezone=GMT
username: root
password: 密码
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
filters: stat,wall
mybatis:
configuration:
map-underscore-to-camel-case: true #自动驼峰映射
type-aliases-package: com.apesource.pojo #起别名
mybatis-plus:
configuration:
map-underscore-to-camel-case: true #自动驼峰映射(默认开启)
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #日志
3.编写注解配置实体类与关系表映射关系(truncate清空表以及主键)
@TableName(value = "关联表名称")=========================》修饰在类
@TableField(value = "关联字段名称")======================》修饰在属性
exist = "忽略字段"
@TableLogic(value="原值",delval="修改值")
注解参数
value = "" 未删除的值,默认值为0
delval = "" 删除后的值,默认值为1
@TableId(type="指定主键生成策略,默认雪花算法")=============》修饰在属性
AUTO(0), NONE(1), INPUT(2), ASSIGN_ID(3), ASSIGN_UUID(4);
java
@NoArgsConstructor//表示无参构造
@AllArgsConstructor//全参构造
@Data//所有属性setget方法以及toString()
@TableName("t_student")
public class Student {
@TableId(value = "stu_id",type = IdType.AUTO)
private int stuId;
@TableField(value = "stu_name")
private String stuName;
@TableField(value = "stu_nick")
private String stuNick;
@TableField(value = "stu_hobby")
private String stuHobby;
@TableLogic(value = "0",delval = "1")
private int isdeleted;
@TableField(exist = false)
private MultipartFile mpf;
public Student(String stuName, String stuNick, String stuHobby) {
this.stuName = stuName;
this.stuNick = stuNick;
this.stuHobby = stuHobby;
}
}
4.使用
注入mapper:
java
@Mapper=====>注册+注入 作用:在mapper类上
@MapperScan(value = "mapper所在的包")=>批量注册+注入 作用:在启动类上
BaseMapper===========》公共的数据访问层
java
@Mapper
public interface HotelMapper extends BaseMapper<Hotel> {
}
IService/ServiceImp==》公共的业务层
java
public interface IHotelService extends IService<Hotel> {
}
java
@Service
public class HotelService extends ServiceImpl<HotelMapper, Hotel> implements IHotelService {
}
二.SpringBoot切换druid数据源
1.导入坐标
XML
<!--druid坐标-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.8</version>
</dependency>
2.做配置
XML
#数据源
spring:
datasource:
url: jdbc:mysql://localhost:3306/库?serverTimezone=GMT
username: root
password: 密码
driver-class-name: com.mysql.cj.jdbc.Driver
#切换数据源类型
type: com.alibaba.druid.pool.DruidDataSource
#Druid监控平台配置
filters: stat,wall
mybatis:
configuration:
map-underscore-to-camel-case: true #自动驼峰映射
type-aliases-package: com.apesource.pojo #起别名
mybatis-plus:
configuration:
map-underscore-to-camel-case: true #自动驼峰映射(默认开启)
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #日志
3.1DRUID配置参数
3.2Druid监控平台
java
filters: stat,wall
@Configuration
public class DruidConfig {
//1.注入数据源
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource(){
return new DruidDataSource();
}
//2.配置servlet
@Bean
public ServletRegistrationBean registrationBean(){
//1.创建servlet注册类
ServletRegistrationBean<StatViewServlet> servletRegistrationBean = new ServletRegistrationBean<StatViewServlet>();
//2.创建制作页面的servlet
StatViewServlet statViewServlet = new StatViewServlet();
//3.绑定servlet
servletRegistrationBean.setServlet(statViewServlet);
servletRegistrationBean.setUrlMappings(Arrays.asList("/druid/*"));
//4.参数绑定
Map<String,String> maps = new HashMap<String,String>();
maps.put(StatViewServlet.PARAM_NAME_USERNAME,"admin");
maps.put(StatViewServlet.PARAM_NAME_PASSWORD,"123");
maps.put(StatViewServlet.PARAM_NAME_ALLOW,"");//白名单
maps.put(StatViewServlet.PARAM_NAME_DENY,"192.168.0.12");//黑名单
servletRegistrationBean.setInitParameters(maps);
return servletRegistrationBean;
}
//3.配置filter
@Bean
public FilterRegistrationBean filterRegistrationBean(){
FilterRegistrationBean<WebStatFilter> bean = new FilterRegistrationBean<WebStatFilter>();
bean.setFilter(new WebStatFilter());
//所有请求进行监控处理
bean.setUrlPatterns(Arrays.asList("/*"));
Map<String, String> initPrams = new HashMap<>();
//添加不需要忽略的格式信息
initPrams.put(WebStatFilter.PARAM_NAME_EXCLUSIONS, "*.js,*.css,/druid/*");
bean.setInitParameters(initPrams);
return bean;
}
}