第一步首先配置pom.xml文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Ming</groupId>
<artifactId>SpringBoot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- Spring Boot 启动父依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
</parent>
<properties>
<mybatis-spring-boot>1.2.0</mybatis-spring-boot>
<mysql-connector>5.1.39</mysql-connector>
<druid>1.0.18</druid>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- Spring Boot Web 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot Mybatis 依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!-- MySQL 连接驱动依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector}</version>
</dependency>
<!-- Druid 数据连接池依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid}</version>
</dependency>
<!-- 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
</dependencies>
</project>
第二步:在src/main/resources目录下新建一个application.yml文件
#配置端口号
server:
port: 8080
logging:
level:
learning: trace
spring:
#数据源配置
datasource:
url: jdbc:mysql://localhost:3306/test
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
connectionProperties: druid.stat.mergeSql=true
#模板引擎
freemarker:
#关闭缓存
cache: false
request-context-attribute: request
#模板加载的位置
template-loader-path: classpath:/templates
#前缀
suffix: .htm
#后缀
prefix: /htm/
mybatis:
#实体类所做包
type-aliases-package: learning.model
#mapper.xml所在位置
mapper-locations: classpath:mappers/*.xml
第三步:配置类的编写,在conf包下新建一个MybatisConfig类以配置使用PageHelper分页
@Configuration
public class MybatisConfig {
@Bean
public PageHelper pageHelper(){
PageHelper pageHelper = new PageHelper();
//添加配置,也可指定文件路径
Properties p = new Properties();
p.setProperty("offsetAsPageNum", "true");
p.setProperty("rowBoundsWithCount", "true");
p.setProperty("reasonable", "true");
pageHelper.setProperties(p);
return pageHelper;
}
}
第四步:实例类的编写
public class Player {
private Integer id;
private String name;
private Double points;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPoints() {
return points;
}
public void setPoints(Double points) {
this.points = points;
}
}
第五步:dao接口的编写
public interface PlayerDao {
void save (Player player);
Player findByKey(Integer id);
List<Player> findAll();
}
第六步:service接口的编写
public interface PlayerService {
void add(Player player);
List<Player> getAll();
}
第七步:service接口的实现类进行实现类的编写
@Service
public class PlayerServiceImpl implements PlayerService{
@Autowired
private PlayerDao playerDao;
@Override
public void add(Player player) {
playerDao.save(player);
}
@Override
public List<Player> getAll() {
PageHelper.startPage(1, 2);
return playerDao.findAll();
}
}
第八步:在src/main/resources 下新建一个包mapppers用来存放mybatis的xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="learning.mapper.PlayerDao">
<insert id="save">
INSERT INTO player (name,points) VALUES (#{name},#{points})
</insert>
<select id="findByKey" resultType="learning.model.Player">
SELECT * FROM player WHERE id = #{id}
</select>
<select id="findAll" resultType="learning.model.Player">
SELECT * FROM player
</select>
</mapper>
第九步:在src/main/resources在新建一个目录templates用来存放freemarker加载的模板
第十步:controller层代码的编写
@Controller
@RequestMapping("/Test")
public class TestController {
@Autowired
private PlayerService playerService;
@RequestMapping("/getAll")
@ResponseBody
public Object getAll(){
Player player = new Player();
player.setName("张三丰");
player.setPoints(15.6);
player.setId(3);
return player;
}
@RequestMapping("/all")
@ResponseBody
public Object get(){
return playerService.getAll();
}
@RequestMapping("/add")
public String add(Player player){
playerService.add(player);
return "test/hello";
}
@RequestMapping("/hello")
public String hello(Model model) {
List<Player> list = playerService.getAll();
model.addAttribute("list",list);
return "test/hello";
}