《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门,即可获取!
org.springframework.boot
spring-boot-starter-parent
2.2.2.RELEASE
org.springframework.boot
spring-boot-starter-data-jdbc
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.1.1
mysql
mysql-connector-java
runtime
com.alibaba
druid
1.1.8
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
3. 编写数据源的相关配置
application.yml
spring:
datasource:
数据源基本配置
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/springboot_mybatis?serverTimezone=GMT%2B8
type: com.alibaba.druid.pool.DruidDataSource
数据源其他配置
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall’用于防火墙
filters: stat,wall,slf4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
initialization-mode: always
schema:
- classpath:sql/department.sql
- classpath:sql/employee.sql
编写一个配置类,将数据源引入过来
@Configuration
public class DruidConfig {
@ConfigurationProperties(prefix = “spring.datasource”)
@Bean
public DataSource druid(){
return new DruidDataSource();
}
// 配置Druid的监控
// 配置一个管理后台的Servlet
@Bean
public ServletRegistrationBean statViewServlet(){
ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), “/druid/*”);
Map<String,String> initParams = new HashMap<>();
initParams.put(“loginUsername”,“admin”);
initParams.put(“loginPassword”,“123456”);
initParams.put(“allow”,“”);//默认就是允许所有访问
bean.setInitParameters(initParams);
return bean;
}
// 配置一个web监控的filter
@Bean
public FilterRegistrationBean webStatFilter(){
FilterRegistrationBean bean = new FilterRegistrationBean();
bean.setFilter(new WebStatFilter());
Map<String,String> initParams = new HashMap<>();
initParams.put(“exclusions”,“.js,.css,/druid/*”);
bean.setInitParameters(initParams);
bean.setUrlPatterns(Arrays.asList(“/*”));
return bean;
}
}
二、利用注解的形式整合
1. 编写实体类
Employee
public class Employee {
private Integer id; // 员工id
private String lastName; // 员工姓名
private Integer gender; // 员工性别
private String email; // 员工邮箱
private Integer dId; // 部门id
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getdId() {
return dId;
}
public void setdId(Integer dId) {
this.dId = dId;
}
}
Department
public class Department {
private Integer id; // 部门id
private String departmentName; // 部门名称
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDepartmentName() {
return departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
}
2. 编写 Mapper接口层
DepartmentMapper
@Mapper // 指定这是一个操作数据库的mapper,将接口扫描到容器中
public interface DepartmentMapper {
/**
-
查询
-
@param id
-
@return
*/
@Select(“select * from department where id=#{id}”)
public Department getDeptById(Integer id);
/**
-
删除
-
@param id
-
@return
*/
@Delete(“delete from department where id=#{id}”)
public int deleteDeptById(Integer id);
/**
-
新增
-
@param department
-
@return
*/
@Options(useGeneratedKeys = true,keyProperty = “id”)
@Insert(“Insert into department(department_name) values(#{departmentName})”)
public int insertDept(Department department);
/**
-
更新
-
@param department
-
@return
*/
@Update(“update department set department_name=#{departmentName} where id=#{id}”)
public int updateDept(Department department);
}
3. 编写 Controller 层
DeptController
@RestController
public class DeptController {
@Autowired
DepartmentMapper departmentMapper;
/**
-
根据id进行查询
-
@param id
-
@return
*/
@GetMapping(“/dept/{id}”)
public Department getDepartment(@PathVariable(“id”) Integer id) {
return departmentMapper.getDeptById(id);
}
/**
-
新增部门信息
-
@param department
-
@return
*/
@GetMapping(“/dept”)
public Department insertDept(Department department) {
departmentMapper.insertDept(department);
return department;
}
}
在不写配置文件的情况 自定义mybatis的配置规则,开启驼峰命名法
@org.springframework.context.annotation.Configuration
public class MybatisConfig {
@Bean
public ConfigurationCustomizer configurationCustomizer() {
return new ConfigurationCustomizer() {
@Override
public void customize(Configuration configuration) {
configuration.setMapUnderscoreToCamelCase(true);
}
};
}
}
4. 测试
插入一条数据
查询刚才插入的数据
去数据库表表里面查询,数据插入进去了
二、利用配置文件的形式整合
1. 编写 Mapper 接口层
EmployeeMapper
@Mapper
public interface EmployeeMapper {
/**
-
根据id查询员工信息
-
@param id
-
@return
*/
public Employee getEmpById(Integer id);
/**
总结
这份面试题几乎包含了他在一年内遇到的所有面试题以及答案,甚至包括面试中的细节对话以及语录,可谓是细节到极致,甚至简历优化和怎么投简历更容易得到面试机会也包括在内!也包括教你怎么去获得一些大厂,比如阿里,腾讯的内推名额!
某位名人说过成功是靠99%的汗水和1%的机遇得到的,而你想获得那1%的机遇你首先就得付出99%的汗水!你只有朝着你的目标一步一步坚持不懈的走下去你才能有机会获得成功!
成功只会留给那些有准备的人!
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门,即可获取!
224102231408.png)
去数据库表表里面查询,数据插入进去了
二、利用配置文件的形式整合
1. 编写 Mapper 接口层
EmployeeMapper
@Mapper
public interface EmployeeMapper {
/**
-
根据id查询员工信息
-
@param id
-
@return
*/
public Employee getEmpById(Integer id);
/**
总结
这份面试题几乎包含了他在一年内遇到的所有面试题以及答案,甚至包括面试中的细节对话以及语录,可谓是细节到极致,甚至简历优化和怎么投简历更容易得到面试机会也包括在内!也包括教你怎么去获得一些大厂,比如阿里,腾讯的内推名额!
某位名人说过成功是靠99%的汗水和1%的机遇得到的,而你想获得那1%的机遇你首先就得付出99%的汗水!你只有朝着你的目标一步一步坚持不懈的走下去你才能有机会获得成功!
成功只会留给那些有准备的人!
[外链图片转存中…(img-8ASqyzVQ-1714747138846)]
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门,即可获取!