码迷,mamicode.com
首页 > 编程语言 > 详细

SpringBoot与Mybatis整合

时间:2018-03-31 00:47:38      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:select   tar   sel   not   art   pid   type   bean   连接   

(1)pom.xml中引入jar包,如下:这里不需要引入spring-boot-starter-jdbc依赖,因为mybatis-spring-boot-starter中已经包含了此依赖

技术分享图片
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>
技术分享图片

  (2)在application.properties配置数据库连接信息,如下:

#mysql数据库配置
spring.datasource.url=jdbc:mysql://172.31.19.20:3306/springboot
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

  (3)新建User实体类,如下:

技术分享图片
package springboot.domain;

public class User {

    private String id;
    private String name;
    private String age;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    @Override
    public String toString() {
        return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
    }
    
}
技术分享图片

  (4)新建UserMapper类

技术分享图片
package springboot.dao;

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.domain.User;

@Mapper
public interface UserMapper {

    @Insert("insert into user(name,age) values(#{name},#{age})")
    int addUser(@Param("name")String name,@Param("age")String age);
    
    @Select("select * from user where id =#{id}")
    User findById(@Param("id") String id);
    
    @Update("update user set name=#{name} where id=#{id}")
    void updataById(@Param("id")String id,@Param("name")String name);
    
    @Delete("delete from user where id=#{id}")
    void deleteById(@Param("id")String id);
    
}
技术分享图片

  这里使用mybatis注解版,相比以前少了一个与dao层相对应的xml文件,还是挺方便的。

  (5)Service层代码如下:

技术分享图片
package springboot.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import springboot.dao.UserMapper;
import springboot.domain.User;


@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;
    
    
    public User findById(String id){
        return userMapper.findById(id);
    }
    
    public int addUser(String name,String age){
        return userMapper.addUser(name,age);
    }
    
    public void updataById(String id,String name){
         userMapper.updataById(id,name);
    }
    
    public void deleteById(String id){
        userMapper.deleteById(id);
    }
}
技术分享图片

  (6)Controller层,代码如下:

技术分享图片
package springboot.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springboot.domain.User;
import springboot.service.UserService;

@RestController
public class HelloController {
    
    @Autowired
    private UserService userService;
    
    @RequestMapping("/adduser")
    public int addUser(@RequestParam("name")String name,@RequestParam("age")String age){
        return userService.addUser(name, age);
    }
    @RequestMapping("/findUser")
    public User findUser(@RequestParam("id") String id){
        return userService.findById(id);
    }
    @RequestMapping("/updataById")
    public String updataById(@RequestParam("id") String id,@RequestParam("name") String name){
        try {
            userService.updataById(id, name);
        } catch (Exception e) {
            return "error";
        }
        return "success";
    }
    
    @RequestMapping("/deleteById")
    public String deleteById(@RequestParam("id") String id){
        try {
            userService.deleteById(id);
        } catch (Exception e) {
            return "error";
        }
        return "success";
    }
}
技术分享图片

SpringBoot与Mybatis整合

标签:select   tar   sel   not   art   pid   type   bean   连接   

原文地址:https://www.cnblogs.com/Andrew520/p/8679267.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!