加了requestmapping后前端访问不到_SpringBoot:数据库访问之Mybatis

a1304caa6953eeb1c640ef289b8b78a9.png

点击关注,后期更多精彩内容呈上!

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

启动服务后,即可访问测试。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值