//1.Employee
public class Employee {
private Integer id;
private String lastName;
private Integer gender;
private String email;
private Integer dId;
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;
}
}
//2.Department
public class Department {
private Integer 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;
}
}
简单介绍一下
jdbc(链接场景),mybatis,数据源配置druid
基本pom文件中依赖
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.1.2
mysql
mysql-connector-java
5.1.6
com.alibaba
druid
1.0.9
见SpringBoot2.0x整合Druid数据源以及Druid监控
//指定这是一个操作数据库的mapper
//@Mapper
public interface DepartmentMapper {
@Select("select * from department where id=#{id}")
public Department getDeptById(Integer id);
@Delete("delete from department where id=#{id}")
public int deleteDeptById(Integer id);
//开启自动使用自动生成的主键
@Options(useGeneratedKeys = true,keyProperty = "id")
@Insert("insert into department(department_name) values(#{departmentName})")
public int insertDept(Department department);
@Update("update department set department_name=#{departmentName} where id=#{id}")
public int updateDept(Department department);
}
自定义MyBatis的配置规则;给容器中添加一个ConfigurationCustomizer;
@org.springframework.context.annotation.Configuration
public class MybatisConfig {
@Bean
public ConfigurationCustomizer configurationCustomizer(){
return new ConfigurationCustomizer(){
@Override
public void customize(Configuration configuration) {
configuration.setMapUnderscoreToCamelCase(true);
}
};
}
}
@MapperScan(value = "com.nadou.springboot.mapper")
@SpringBootApplication
public class SpringBoot06MybatisApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBoot06MybatisApplication.class, args);
}
}
mybatis:
config-location: classpath:mybatis/mybatis-config.xml 指定全局配置文件位置
mapper-locations: classpath:mybatis/mapper/*.xml 指定sql映射文件位置
IDEA中resources包下mybatis主配置文件config与映射配置文件mapper:
https://blog.csdn.net/qq_43640120/article/details/105788116
开启驼峰式命名规则的映射
@GetMapping("/dept")
public Department insertDept(Department department){
departmentMapper.insertDept(department);
return department;
}