springboot整合mybatis

1.使用数据库是mysql,首先是在application.properties配置文件中添加mysql的配置文件

#mysql
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/sshe
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5
server.session.timeout=10
server.tomcat.uri-encoding=UTF-8

2.在pom.xml文件中,添加mysql依赖包,和mybatis的依赖包

 <dependency>
              <groupId>mysql</groupId>
              <artifactId>mysql-connector-java</artifactId>
          </dependency>
        

<!-- 集成mybatis依赖包 --> 
     <dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.0</version>
    </dependency>
   

3.mysql建表语句

CREATE TABLE `demo` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8

4.实体类

package springboot.demo.bean;
public class Demo {
private int id;
private String name;
  public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} 
}
5.DemoMapper接口

package springboot.demo.mapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import springboot.demo.bean.Demo;
@Mapper
public interface DemoMapper {    
//增加用户
@Insert("insert into demo(name) values(#{name})")
    public int save(Demo demo); 
//删除用户
@Delete("delete from demo where id=#{id}")
public int delete(int id);
//修改用户
@Update("update demo set name=#{name} where id=#{id}")
public int update(@Param("id") int id,@Param("name") String name);
  //查询用户
@Select("select * from demo where id=#{id}")
    public Demo selete(int id);  
}

6.DemoService类

package springboot.demo.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import springboot.demo.bean.Demo;
import springboot.demo.mapper.DemoMapper;
@Service
public class DemoService {
       @Autowired
private DemoMapper demoMapper;
@Transactional
public int save(Demo demo){
return demoMapper.save(demo);
}
@Transactional
public int delete(int id){
return demoMapper.delete(id);
}
@Transactional
public int update(int id,String name){
return demoMapper.update(id,name);
}
@Transactional
public Demo selete(int id){
return demoMapper.selete(id);
}

}
7.DemoControl

package springboot.demo.control;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springboot.demo.bean.Demo;
import springboot.demo.service.DemoService;
@RestController
@RequestMapping("user")
public class DemoControl {
    @Autowired
private DemoService demoService;
    //增加用户
@GetMapping("/save")
public int save(Demo demo){
  return demoService.save(demo);
}
//删除用户
@GetMapping("/delete")
public int delete(int id){
  return demoService.delete(id);
}
//更新用户
@GetMapping("/update")
public int update(int id,String name){
return demoService.update(id,name);
}
//查询用户
@GetMapping("/selete")
public Demo selete(int id){
Demo demo=demoService.selete(id);
    System.out.println(demo.getId());
    System.out.println(demo.getName());
    return demo; 
}

}

8.springboot启动类

package springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 @SpringBootApplication
public class SpringbootApplication {
 public static void main(String[] args) {
     SpringApplication app=new SpringApplication(SpringbootApplication.class);
app.run(args);
}
}

9.访问如下

增加数据 http://localhost:8080/user/save?name=sony

删除数据 http://localhost:8080/user/delete?id=1

修改数据 http://localhost:8080/user/update?id=1&name=link

查询数据 http://localhost:8080/user/selete?id=1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值