开发者学堂课程【SpringBoot 快速掌握 - 高级应用:基本环境搭建】学习笔记,与课程紧密联系,让用户快速学习知识
课程地址:https://developer.aliyun.com/learning/course/613/detail/9288
基本环境搭建
Spring Boot 的版本模块为1.5.12
Core 中选择 Cache 模块,引用 Web 模块及其他数据结构模块,用 SQL 及 MyBatis数据库。
创建完工程之后
package com. atguigu. cache;
import
/**
*搭建基本环境
*1、导入数据库文件的创建出 department 和 employee 表
*2、创建 javaBean 封装数据
*3、整合 MyBatis 操作数据库
⑴.配置数据源信息
spring, datasource . url=jdbc:mysql://localhost:33θ6/spring _ cache
spring. datasource . username=root
spring, datasource , password=123456
spring,datasource, driver-class-name=com.mysq1.jdbc.Driver
⑵使用注解版的 MyBatis
①@ MapperScan 指定需要扫描的 Mappe r接口所在的包
package com. atguigu. cache. mapper;
import org. apache. ibatis. annotations . Mapper;
@Mapper
public interface EmployeeMapper {
@Select("SELECT* FROMemployeeWHEREid =#{id}")
public Employee getEmpByīd(Integer id);
@Update(" UPDATEemployeeSET1astNamez #{lastNameJˈ/ˈ)
public void updateEmp(Employee employee);
@Delete(" DELETEFROMemployeeWHEREid =#{id}")
public void deleteEmpById (Integer id);
@Insert("INSERTIN")
public void insertEmployee (Employee employee);
}
package com. atguigu. cache;
import...
@RunWith( SpringRunner .class)
@ SpringBootTest
public class Spring bootθ 1CacheApplicationTests {
@Autowired
EmployeeMapper employee Mapper;
@Test
public void context Loads()){
Employee empByI&= employeeMapper . getEmpById (1);
System. out. println(empById);
}
}
结果运行成功,出现数据
package com. atguigu. cache. service;
import com. atguigu. cache. bean. Employee;
import com. atguigu. cache. mapper. EmployeeMapper ;
import org. springframework . beans. factory. annotation . Autowired;
import org. springframework . stereotype . Service;
@Service
public class EmployeeService {
@Autowired
EmployeeMapper employee Mapper;
public Employee get Emp(Integer id){
System. out. println("查询"id+"号员工");
Employee emp= emp1oyeeMapper . getEmpById (id);
return emp;
}
@ SpringBootApplication
public class Springboot θ 1CacheApplication {
public static void main(String[]orgs] Spring4pplication . run( Springboot θ 1CacheApplication .}
What is the name of the name of the British
American Revolutionary .
package com. atguigu. cache. bean;
public class Department {
private Integer id;
private String departmentName ;
public Department (){
super();
//TODO Auto-generated constructor stub
}
public Department (Integer id, String depart mentName){
super();
this. id=id;
this. departmentName = departmentName ;
}
public Integer getId(){return id;}
public void setId(Integer id){this. id=id;
public String getDepartmentName (){return depart mentName;}
public void setDepartmentMame (String departmentName )(this. departmentMame = development /sume)
@OverridepublicStringto String()(return" Department [id="+id+", departmentNames "+ departmentName+
来分装表的数据
最后的结果:
{
id:2,
last Name:“李四",
email:" lisi@atguigu.com ",
gender:1,
dId 1
}