
点击关注,后期更多精彩内容呈上!
在SpringBoot中使用Mybatis非常简单,我们只需要引入依赖即可。
1 创建SpringBoot项目并引入依赖
org.springframework.boot spring-boot-starter-weborg.mybatis.spring.boot mybatis-spring-boot-starter 1.3.2mysql mysql-connector-java 5.1.47
2 创建数据库表及对应的实体类
# 创建表CREATE TABLE `t_demo` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `name` VARCHAR(50) NOT NULL, `telphone` VARCHAR(15) NOT NULL, PRIMARY KEY (`id`)) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8
public class DemoEntity { private Integer id; private String name; private String telphone; @Override public String toString() { return "DemoEntity[id="+this.id+", name="+this.name+", telphone="+this.telphone+"]"; } //省略 get set 方法}
3 定义mapper接口及创建对应的xml文件
package com.xw.base.manager.mapper;import com.xw.base.manager.entity.DemoEntity;public interface DemoMapper { DemoEntity selectById(Integer id); int save(DemoEntity entity); void updateDemo(DemoEntity entity); int deleteById(Integer id);}
<?xml version="1.0" encoding="UTF-8" ?> insert into t_demo (id, name, telphone) values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{telphone,jdbcType=VARCHAR}) SELECT * FROM t_demo WHERE id = #{id,jdbcType=INTEGER} delete FROM t_demo WHERE id = #{id,jdbcType=INTEGER} update t_demo set name = #{name}, telphone = #{telphone} where id = #{id}
4 定义服务接口及对应的实现
public interface DemoService { DemoEntity selectById(Integer id); int save(DemoEntity entity); void updateDemoEntity(DemoEntity entity); int deleteById(Integer id);}
@Servicepublic class DemoServiceImpl implements DemoService { @Autowired private DemoMapper demoMapper; @Override public DemoEntity selectById(Integer id) { return demoMapper.selectById(id); } @Override public int save(DemoEntity entity) { return demoMapper.save(entity); } @Override public void updateDemoEntity(DemoEntity entity) { demoMapper.updateDemo(entity); } @Override public int deleteById(Integer id) { return demoMapper.deleteById(id); }}
5 修改属性配置文件
#jdbc信息spring.datasource.username=rootspring.datasource.password=123456spring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&useSSL=true&serverTimezone=GMT# 指定mapper的xml位置mybatis.mapper-locations=classpath:/mapper/*Mapper.xml
6 修改启动类
@SpringBootApplication//指定要扫描的mybatis的接口所在包(也可以在每个Mapper接口上使用@Mapper)@MapperScan(basePackages = "com.xw.base.manager.mapper")public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}
7 创建访问的controller
@RestControllerpublic class MybatisDemoController { @Autowired private DemoService demoService; @RequestMapping(value="/save", method=RequestMethod.POST) public String save(@RequestParam("name") String name, @RequestParam("telphone") String telphone){ DemoEntity entity = new DemoEntity(); entity.setName(name); entity.setTelphone(telphone); int result = demoService.save(entity); return String.valueOf(result); } @RequestMapping(value="/get", method=RequestMethod.GET) public String getById(@RequestParam("id") Integer id){ DemoEntity entity = demoService.selectById(id); return entity.toString(); } @RequestMapping(value="/delete", method=RequestMethod.POST) public String delete(@RequestParam("id") Integer id){ int result = demoService.deleteById(id); return String.valueOf(result); } @RequestMapping(value="/update", method=RequestMethod.POST) public String update(DemoEntity entity){ demoService.updateDemoEntity(entity); return "success"; }}
启动服务后,即可访问测试。