SpringBoot 使用yml配置 mybatis+pagehelper+druid+freemarker实例
这是一个简单的SpringBoot整合实例
这里是项目的结构目录
首先是pom.xml
4.0.0 Ming SpringBoot 0.0.1-SNAPSHOT org.springframework.boot spring-boot-starter-parent 1.5.4.RELEASE 1.2.0 5.1.39 1.0.18 1.8 org.springframework.boot spring-boot-starter-web org.mybatis.spring.boot mybatis-spring-boot-starter 1.1.1 mysql mysql-connector-java ${mysql-connector} com.alibaba druid ${druid} com.github.pagehelper pagehelper 4.1.6 org.springframework.boot spring-boot-starter-freemarker
接着在src/main/resources目录下新建一个application.yml文件
server: port: 8080logging: level: learning: tracespring: #数据源配置 datasource: url: jdbc:mysql://localhost:3306/test username: root password: 123456 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分页
代码如下@Configurationpublic 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); ListfindAll();}
service接口以及实现
public interface PlayerService { void add(Player player); ListgetAll();}@Servicepublic class PlayerServiceImpl implements PlayerService{ @Autowired private PlayerDao playerDao; @Override public void add(Player player) { playerDao.save(player); } @Override public List getAll() { PageHelper.startPage(1, 2); return playerDao.findAll(); }}
在src/main/resources 下新建一个包mapppers用来存放mybatis的xml文件
INSERT INTO player (name,points) VALUES (#{name},#{points})
在src/main/resources在新建一个目录templates用来存放freemarker加载的模板
随意编写一个freemarker 的模板html界面
success
<#list list as player> ${player.name} : ${player.points}
然后编写控制层代码
@Controller@RequestMapping("/Test")public class TestController { @Autowired private PlayerService playerService; @RequestMapping("/getAll") @ResponseBody public Object getAll(){ Player player = new Player(); player.setName("杜兰特"); player.setPoints(28.4); 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) { Listlist = playerService.getAll(); model.addAttribute("list",list); return "test/hello"; }
最后编写项目的启动代码主要启动类的路径要在其他要扫描包的同级目录如图AppStart
启动类代码
@SpringBootApplication//这里是扫描dao接口的包用于识别mybatis@MapperScan(basePackages="learning.mapper")public class AppStart { /** * @param args */ public static void main(String[] args) { SpringApplication.run(AppStart.class, args); }}
最后启动AppStart main 方法即可
日志信息
这里是我的项目代码
我将代码上传到了GitHub上了