spring boot 整合mybatis ,分两块mybatis 整合,分页整合。
1.pom文件增加
org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.2 com.github.pagehelper pagehelper 4.1.6 mysql mysql-connector-java com.alibaba druid 1.0.26
2.创建表
CREATE TABLE `sale_order` ( `ID_` varchar(64) NOT NULL DEFAULT '' COMMENT '主键', `NAME_` varchar(64) DEFAULT NULL COMMENT '订单名称', `TOTAL_` decimal(14,2) DEFAULT NULL COMMENT '合计', `CREATOR_` varchar(64) DEFAULT NULL COMMENT '制单人', `CREATE_TIME_` datetime DEFAULT NULL COMMENT '创建时间', `TENANT_ID_` varchar(64) DEFAULT NULL, PRIMARY KEY (`ID_`))
3.创建SaleOrder.map.xml 文件
INSERT INTO SALE_ORDER (ID_,NAME_,TOTAL_,CREATOR_,CREATE_TIME_) VALUES (#{id,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, #{total,jdbcType=NUMERIC}, #{creator,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP}) UPDATE SALE_ORDER SET NAME_=#{name,jdbcType=VARCHAR}, TOTAL_=#{total,jdbcType=NUMERIC}, CREATOR_=#{creator,jdbcType=VARCHAR} WHERE ID_=#{id} DELETE FROM SALE_ORDER WHERE ID_=#{id}
注意命名空间
com.neo.dao.SaleOrderDao
配置map文件扫描。
mybatis: typeAliasesPackage: com.neo.model mapperLocations: classpath:mapper/*.xml
4.增加Dao类
package com.neo.dao;import com.github.pagehelper.Page;import com.neo.model.SaleOrder;public interface SaleOrderDao { int create(SaleOrder record); void update(SaleOrder record); SaleOrder get(String id); Pagequery();}
5.配置dao类扫描。
package com;import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import com.neo.filter.ApplicationStartedEventListener;import com.neo.filter.ApplicationStartedEventListener2;import com.neo.filter.ApplicationStartingEventListener;@SpringBootApplication@MapperScan({ "com.neo.dao"}) public class DemoApplication { public static void main(String[] args) { SpringApplication app=new SpringApplication(DemoApplication.class); app.addListeners(new ApplicationStartedEventListener()); app.addListeners(new ApplicationStartingEventListener()); app.addListeners(new ApplicationStartedEventListener2()); app.run(args); }}
这里配置了MapperScan 注解。
6.配置分页
package com.neo.conf;import java.util.Properties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import com.github.pagehelper.PageHelper;@Configurationpublic class PageConfiguration { @Bean public PageHelper pageHelper(){ PageHelper pageHelper = new PageHelper(); Properties properties = new Properties(); properties.setProperty("offsetAsPageNum","true"); properties.setProperty("rowBoundsWithCount","true"); properties.setProperty("reasonable","true"); properties.setProperty("dialect","mysql"); //配置mysql数据库的方言 pageHelper.setProperties(properties); return pageHelper; }}
7.编写业务类
package com.neo.service;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.github.pagehelper.Page;import com.github.pagehelper.PageHelper;import com.neo.dao.SaleOrderDao;import com.neo.model.SaleOrder;@Servicepublic class SaleOrderService { @Autowired SaleOrderDao orderDao; public void create(SaleOrder order){ orderDao.create(order); } public SaleOrder get(String id){ return orderDao.get(id); } public PagegetAll(int page){ PageHelper.startPage(page, 2); return orderDao.query(); }}
8.配置打印SQL
在application.yml 文件中增加
logging: level: com.neo.dao: debug
9.编写控制器代码
@RequestMapping("/add") public void add(){ SaleOrder order=new SaleOrder(); order.setId(System.currentTimeMillis() +""); order.setName("zyg"); order.setCreator("AA"); order.setTotal(33D); saleOrderService.create(order); } @RequestMapping("/get/{id}") public SaleOrder get(@PathVariable(value="id") String id){ SaleOrder order= saleOrderService.get(id); return order; } @RequestMapping("/page/{page}") public Pagepage(@PathVariable(value="page") int page){ Page order= saleOrderService.getAll(page); return order; }