天天看點

加了requestmapping後前端通路不到_SpringBoot:資料庫通路之Mybatis

加了requestmapping後前端通路不到_SpringBoot:資料庫通路之Mybatis

點選關注,後期更多精彩内容呈上!

在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";    }}
           

啟動服務後,即可通路測試。