【初学入门 Demo 注解版】SpringBoot ,java 面试大全下载
public class User implements Serializable {
private Integer id;
private String username;
private String password;
private int age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public
String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public User(Integer id, String username, String password, int age) {
this.id = id;
this.username = username;
this.password = password;
this.age = age;
}
public User() {
}
}
UserDao.java
package com.example.dao;
import com.example.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/**
@author Nopi
*/
@Mapper
public interface UserDao {
/**
获取所有用户信息数据接口
@return List<User>
*/
@Select("select * from User")
List<User> getUserList();
}
@Select 是查询类的注解,所有的查询均使用这个
@Result 修饰返回的结果集,关联实体类属性和数据库字段一一对应,如果实体类属性和数据库属性名保持一致,就不需要这个属性来修饰。
@Insert 插入数据库使用,直接传入实体类会自动解析属性到对应的值
@Update 负责修改,也可以直接传入对象
@delete 负责删除
UserService.java
package com.example.service;
import com.example.entity.User;
import java.util.List;
/**
@author Nopi
*/
public interface UserService {
/**
获取所有用户信息业务接口
@return List<User>
*/
List<User> getUserList();
}
UserServiceImpl.java
package com.example.service.impl;
import com.example.dao.UserDao;
import com.example.entity.User;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
@author Nopi
*/
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public List<User> getUserList() {
return userDao.getUserList();
}
}
UserController.java
package com.example.controller;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
/**
@author Nopi
*/
@Controller
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/getUserList")
public String getUserList(Model model){
model.addAttribute("uList",userService.getUserList());
return "UserListShow";
}
}
DemoApplication.java
package com.example.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan("com.example.*")
@MapperScan("com.example.dao")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
UserListShow.html
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>用户展示</title>
</head>
<body>
<table cellpadding="0" cellspacing="0" width="700px">
<tr th:each="user:${uList}">
<td th:text="${user.id}"></td>
<td th:text="${user.username}"></td>
<td th:text="${user.password}"></td>
<td th:text="${user.age}"></td>
</tr>
</table>
评论