选择 spring boot devtools ,
等同于下面引入 devtools依赖包
org.springframework.boot
spring-boot-devtools
true
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-freemarker
选择mybatis和mysql driver
注意这里还是不要 选择mysql driver,不然引入的依赖包版本太高,可能不使用本地mysql版本,本例
选择后,引入的mysql driver 版本是 8.以上,导致程序报错。
等同于引入依赖
mysql
mysql-connector-java
5.1.21
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.1.0
spring:
# freemarker 配置
freemarker:
template-loader-path: classpath:/templates/view/
cache: false
charset: utf-8
check-template-location: true
content-type: text/html
expose-request-attributes: false
expose-session-attributes: false
request-context-attribute: request
suffix: .ftl
# mysql 数据源
datasource:
url: jdbc:mysql://127.0.0.1:3306/student?useUnicode=true&characterEncoding=UTF-8
username: root
password: root
driverClassName: com.mysql.jdbc.Driver
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.example.app.user.dto
注意这里 datasource 必须挨着spring ,如果datasource 放在 mybatis后面,
则需要再加上spring
新建 t_user 表,并插入数据
CREATE TABLE t_user (
user_id int(5) NOT NULL AUTO_INCREMENT comment '用户id' ,
name varchar(20) comment '用户名称' ,
passwd varchar(20) comment '用户密码' ,
phone varchar(20) comment '电话' ,
remark varchar(20) comment '备注' ,
PRIMARY KEY (user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 comment '用户信息表';
insert into t_user(name,passwd,phone,remark) values('孙悟空','123456','1388888888','七十二变');
insert into t_user(name,passwd,phone,remark) values('唐三藏','123456','1311111111','大唐佛法');
insert into t_user(name,passwd,phone,remark) values('猪八戒','123456','1322222222','好色好吃');
insert into t_user(name,passwd,phone,remark) values('沙和尚','123456','1322222222','勤劳老实');
文件路径放在 templates\view 下,在 templates下新建路径 view ,然后新建ftl文件 showAllUsers.ftl
hello world! this is spring boot and mybatis and freemarker
<#list datas as user>
${user.user_id} ${(user.name)!} ${(user.remark)!}
#list>
新建 包 user.dao ,user.dto,user.service,user.serviceimpl,以及controller 结构如下
新建java 类UserDto
package com.example.demo.user.dto;
public class UserDto {
private Integer user_id;
private String username;
private String passwd;
private String phone;
private String remark;
public Integer getUser_id() {
return user_id;
}
public void setUser_id(Integer user_id) {
this.user_id = user_id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPasswd() {
return passwd;
}
public void setPasswd(String passwd) {
this.passwd = passwd;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
}
UserDao 接口
package com.example.demo.user.dao;
import com.example.demo.user.dto.UserDto;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
@Mapper
public interface UserDao {
public List getUsers();
}
UserService 接口
package com.example.demo.user.service;
import com.example.demo.user.dto.UserDto;
import java.util.List;
public interface UserService {
public List getUsers();
}
UserService 接口 实现类
package com.example.demo.user.service.impl;
import com.example.demo.user.dao.UserDao;
import com.example.demo.user.dto.UserDto;
import com.example.demo.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service(value = "userService")
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public List getUsers() {
return userDao.getUsers();
}
}
UserController 控制类
package com.example.demo.controller;
import com.example.demo.user.dto.UserDto;
import com.example.demo.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
import java.util.Map;
@Controller
public class UserController {
@Autowired
UserService userService;
@RequestMapping("/showAllUsers")
public String showAllUser(Map map){
List users=userService.getUsers();
map.put("datas",users);
return "showAllUsers";
}
}
DemoApplication 代码如下:
package com.example.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.example.demo.user.dao")
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
在resource 目录下 新建mapper目录,再新建UserMapper 配置文件
http://127.0.0.1:8080/showAllUsers
一般启动解析application.yml错误就是application.yml配置文件格式不规范导致,一定要细心。
配置application.yml文件时一定要注意各个配置间上下级关系,以及同级配置必须有相同的空格缩进,
不能使用tab。其次mysql driver 依赖包选择一定要合适,本例mysql 版本是5.7,所以选择了mysql-connector-java
版本是5.1.21。
路漫漫其修远兮,吾将上下而求索。