一.在你建立的工程下创建 Module 选择Spring initializr创建。
二.在Type处选择: Maven Project(项目的构建工具)
三.创建依赖时勾上web,mybatis,mysql(这个看你个人需要吧,可以自主选择)
建立好的项目结构如下:
注意:application.properties和application.yml是同一个东西,均为项目的核心配置文件
内容如下:
#连接数据库 spring.datasource.url=jdbc:mysql://localhost:3306/smbms spring.datasource.username=root spring.datasource.password=1234 spring.datasource.driverClassName=com.mysql.jdbc.Driver #引入mybatis的配置文件 mybatis: mybatis.mapper-locations=classpath:mapper/*.xml mybatis.type-aliases-package=com.example.sprboot.pojo
相应的pom.xml文件
4.0.0 com.example springboot0.0.1-SNAPSHOT jar springboot Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent2.0.5.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-testtest org.mybatis.spring.boot mybatis-spring-boot-starter1.3.0 mysql mysql-connector-javacom.alibaba fastjson1.2.49 org.springframework.boot spring-boot-starter-thymeleaforg.springframework.boot spring-boot-maven-plugin
相应的接口UserMapper如下:
@Repository
public interface UserMapper {
List getList();
}
service如下:
public interface UserService {
List getList();
}
impl如下:
@Service
public class UserServiceImpl implements UserService {
@Resource
private UserMapper userMapper;
@Override
public List getList() {
return userMapper.getList();
}
}
在resources中建一个文件夹mapper里面放mapper.xml文件,代码如下:
select * from smbms_user
在templates文件夹中建html文件(注意:Spring Boot中不能跳转到.jsp文件,所以只能用html)
核心代码如下:
| 工号 | 用户名 | 姓名 | 性别 | 生日 | 电话 | 地址 | 创建时间 |
|---|---|---|---|---|---|---|---|
此处有一个th标签,需要引入一个
并在pom.xml中引入对应的jar包(html中不能使用jstl表达式)
大家可以扩展一下thymeleaf的知识
控制器代码如下:
@Controller
public class UserController {
@Resource
private UserService userService;
@RequestMapping("/")
public String getStuinforList(HttpServletRequest request, Model model){
List list=userService.getList();
model.addAttribute("users",list);
System.out.println(list);
return "/index.html";
}
}
注:在调通的时候,可能会报很多的错,基本上都是注解的使用出错,希望各位能够细心点
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



