springboot 整合 mybaties

springboot自带很多场景启动器。spring-boot-starter-data 关于对数据的处理。。。data里面支持多种数据框架。。如jpa  mybaties  jdbc 等等 

1:依赖

<dependency>
	<groupId>org.mybatis.spring.boot</groupId>
	<artifactId>mybatis-spring-boot-starter</artifactId>
	<version>1.3.2</version>
</dependency>

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

2:数据源文件

server.port=9999



#DataSouce
# 数据源基本配置
spring.datasource.url=jdbc:mysql://localhost:3306/testwkn?characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.dasource.
spring.jpa.database=MYSQL
# 显示后台处理的SQL语句
spring.jpa.show-sql=true
# 自动检查实体和数据库表是否一致,如果不一致则会进行更新数据库表
spring.jpa.hibernate.ddl-auto=update


3:mapper

package com.example.mybatiesdemo.mapper;

import com.example.mybatiesdemo.entity.UserEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

/**
 * ***GOOD LUCK****
 *
 * @Author : Wukn
 * @Date : 2018/9/
 */
@Component
public interface UserMapper {

    @Select( "select * from t_user  where id = #{id}" )
    public UserEntity findUser(Long id);
}
package com.atguigu.springboot.mapper;

import com.atguigu.springboot.bean.Department;
import org.apache.ibatis.annotations.*;


//指定这是一个操作数据库的mapper
//@Mapper
public interface DepartmentMapper {

    @Select("select * from department where id=#{id}")
    public Department getDeptById(Integer id);

    @Delete("delete from department where id=#{id}")
    public int deleteDeptById(Integer id);

    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into department(department_name) values(#{departmentName})")
    public int insertDept(Department department);

    @Update("update department set department_name=#{departmentName} where id=#{id}")
    public int updateDept(Department department);
}

4:实体类

package com.example.mybatiesdemo.entity;
import com.alibaba.fastjson.JSON;
import java.io.Serializable;

/**
 * com.xinguangnet.tuchao.merchant.manage
 *
 * @Author : Wukn
 * @Date : 2018/2/5
 *
 * 人员实体类
 */

public class UserEntity implements Serializable {


    private Long id;

    private String name;

    /**
     * 1 男
     * 0 女
     */
    private Integer sex;

    private String address;

    public UserEntity() {
    }

    public UserEntity(String name, Integer sex, String address) {
        this.name = name;
        this.sex = sex;
        this.address = address;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getSex() {
        return sex;
    }

    public void setSex(Integer sex) {
        this.sex = sex;
    }


    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return JSON.toJSONString( this );
    }
}

5:

package com.example.mybatiesdemo.web;

import com.example.mybatiesdemo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * ***GOOD LUCK****
 *
 * @Author : Wukn
 * @Date : 2018/6/
 */
@RestController
@RequestMapping(value = "user")
public class UserController {

    @Autowired
   private UserMapper userMapper;




    @GetMapping(value = "find/{id}")
    public Object fidnUser(@PathVariable("id") Long id) {
        return userMapper.findUser( id );
    }





}

注意:

需要添加mybaties注解扫描、、让springboot容器扫描到mybaties组件

1:方式一  在启动类添加注解@MapperScan   全局扫描

2:方式二  在具体的mapper类中添加注解@mapper   springboot在启动时会扫面到这个组件

猜你喜欢

转载自blog.csdn.net/weixin_41404773/article/details/82659195