project:springboot-02!!!
一、模拟一个数据库

pojo.Department
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Department {
private Integer id;
private String departmentName;
}
pojo.Employee
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Employee {
private Integer id;
private String lastName;
private String email;
private Integer gender;//0女1男
private Department department;
private Date birth;
}
dao.DepartmentDao
@Repository
public class DepartmentDao {
//模拟数据库中的数据
private static Map<Integer, Department> departments = null;
static {
//Map中的key代表数据库的索引,value代表索引对应的这一行记录
departments = new HashMap<Integer, Department>();//创建一个部门数据表
departments.put(101, new Department(101, "教学部"));
departments.put(102, new Department(102, "市场部"));
departments.put(103, new Department(103, "教研部"));
departments.put(104, new Department(104, "运营部"));
departments.put(105, new Department(105, "后勤部"));
}
//获取所有部门信息
public Collection<Department> getDepartments(){
return departments.values();
}
//通过id得到部门
public Department getDepartmentById(Integer id){
return departments.get(id);
}
}
dao.EmployeeDao
@Repository
public class EmployeeDao {
//模拟数据库中的数据
private static Map<Integer, Employee> employees = null;
@Autowired
private DepartmentDao departmentDao;
static {
//Map中的key代表数据库的索引,value代表索引对应的这一行记录
employees = new HashMap<Integer, Employee>();
employees.put(1001, new Employee(1001, "AA", "392851349@qq.com", 0, new Department(101, "教学部"), new Date()));
employees.put(1002, new Employee(1002, "BB", "378985149@qq.com", 0, new Department(102, "市场部"), new Date()));
employees.put(1003, new Employee(1003, "CC", "848695349@qq.com", 1, new Department(103, "教研部"), new Date()));
employees.put(1004, new Employee(1004, "DD", "648695349@qq.com", 1, new Department(104, "运营部"), new Date()));
employees.put(1005, new Employee(1005, "EE", "248695349@qq.com", 1, new Department(105, "后勤部"), new Date()));
}
//主键自增
private static Integer initId = 1006;
//增加一个员工
public void addEmployee(Employee employee){
if(employee.getId()==null){
employee.setId(initId++);
}
employee.setDepartment(departmentDao.getDepartmentById(employee.getDepartment().getId()));
employees.put(employee.getId(), employee);
}
//查询全部员工
public Collection<Employee> getAll(){
return employees.values();
}
//通过id查询员工
public Employee getEmployeeById(Integer id){
return employees.get(id);
}
//通过id删除员工
public void delete(Integer id){
employees.remove(id);
}
}
@Repository注解
@repository跟@Service,@Compent,@Controller这4种注解是没什么本质区别,都是声明作用,取不同的名字只是为了更好区分各自的功能.
- 对DepartmentDao注解上@Repository后,
- @Autowire
- private DepartmentDao departmentDao;
- 就可以这样自动注入了,而不需要new对象
二、导入静态资源

具体资源在java后端开发文件的狂神springboot静态资源目录
- 注意这里面的html放在templates下,css、js、图片放在static下
三、首页
controller层:
//首页
@Controller
public class IndexController {
@RequestMapping({"/", "/index", "/index.html"})
//不管是访问"/", "/index", "/index.html"中的哪一个,都可以跳转到index.html页面
public String index(){
return "index";
}
}
- 在这里终于搞明白了return的作用,也就是说当请求localhost:8080/index的时候,视图控制器跳转到resources/templates/index.html。
- return的字符串其实就是templates下的文件名
结果:

- 但是这里的index首页中一些css样式和图片没有加进来,下一步就要把html文件中添加css、img
- 通过thymeleaf模板
扩展MVC
除了通过上述方式跳转到主页index.html以外,还可以通过自己定义的方式去扩展mvc
config.MyMvcConfig类:
@Configuration
//@EnableWebMvc
public class MyMvcConfig implements WebMvcConfigurer{
@Override
public void addViewControllers(ViewControllerRegistry registry){
registry.addViewController("/").setViewName("index");
registry.addViewController("/index").setViewName("index");
registry.addViewController("/index.html").setViewName("index");
}
}
- @Configuration表示是一个配置类
- 不能加上@EnableWebMvc,加上后表示springboot 的所有默认配置都失效了,但是我们只想更改部分默认配置
-
registry.addViewController("/").setViewName("index");//访问根目录时,跳转到index.html文件
但是这里的index首页中一些css样式和图片没有加载进来,下一步就要把html文件中添加css、img。
通过thymeleaf模板引擎来在html加载静态资源
1、在html中添加thymeleaf命名空间
<html lang="en" xmlns:th="http://www.thymeleaf.org">
2、 更改html中的超链接:
<link href="asserts/css/bootstrap.min.css" rel="stylesheet">
<link th:href="@{/css/bootstrap.min.css}" rel="stylesheet">
3、......
在application.properties中关闭模板引擎的缓存,否则更改的html会因为缓存而无效
spring.thymeleaf.cache=false
四、首页的国际化
1、首先在File-setting-Editor-File Encodings中确保都是utf8,否则会乱码
2、在resources下新建一个i18n文件夹(全称:internationalization,i和n之间有18个字母)
新建三个名为:login.properties、login_en_US.properties、login_zh_CN.properties的配置文件,springboot会根据文件名自动识别其功能,并把它们自动合并在Resource Bundle 'login'

login.properties(默认)
login.tip=请登录
login.password=密码
login.remember=记住我
login.username=用户名
login.btn=登录
login_en_US.properties(英文)
login.tip=Please sign in
login.password=password
login.remember=remember me
login.username=user name
login
672




被折叠的 条评论
为什么被折叠?



