美文网首页
mybatis简易项目配置

mybatis简易项目配置

作者: livesxu | 来源:发表于2020-09-27 23:15 被阅读0次

1.pom

    <!--mybatis - https://mybatis.org/mybatis-3/zh/index.html -->
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>1.3.2</version>
    </dependency>
    <!--通用mapper - https://apidoc.gitee.com/free/Mapper/ -->
    <dependency>
      <groupId>tk.mybatis</groupId>
      <artifactId>mapper-spring-boot-starter</artifactId>
      <version>2.1.5</version>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
    </dependency>

2.application.yaml

mybatis:
  configuration:
#    开启驼峰
    map-underscore-to-camel-case: true
#  让mybatis自动扫描到自定义POJO
  type-aliases-package: com.livesxu.pojo
#  配置mapper  **:任意多级目录
  mapper-locations: classpath:mappers/**/*.xml

3.配置pojo

//lombok库 帮助主动生成set、get等
@Data
public class MyPojo {

    int id;

    String location;

    int worktime;
}

4.App配置扫描mapper的包

支持通用mapper需要使用对应库下的MapperScan
import tk.mybatis.spring.annotation.MapperScan;

@SpringBootApplication
@MapperScan(value = {"com.livesxu.mapper","com.livesxu.hello.myBatisTest"})
public class App 
{
    public static void main( String[] args )
    {
        SpringApplication.run(App.class,args);
    }
}

5.创建mapper接口

public interface MyMapper {

    List<MyPojo> findAll();

    MyPojo findOne();
}
  • -- 通用mapper示例
    {
    通用mapper不需要配置xml,但是pojo需多一些注解标识::比如
    1.对类标识表名@Table(name = "tb_user");
    2.标识主键
    3.标识不持久化
    @Id
    @KeySql(useGeneratedKeys = true)//作为主键自增长
    private int id;

    //瞬时,不作为sql字段持久化
    @Transient
    private String note;

}

import tk.mybatis.mapper.common.Mapper;

public interface UserMapper extends Mapper<User> {
}

6.resources下配置mapper需要的xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.livesxu.hello.myBatisTest.MyMapper">
    <select id="findOne" resultType="com.livesxu.pojo.MyPojo">
        select * from worker where id=2
    </select>
    <select id="findAll" resultType="com.livesxu.pojo.MyPojo">
        select * from worker
    </select>
<!--    <resultMap id="" type=""-->
<!--    resultMap=""-->
</mapper>

7.测试调用:

@RestController
@RequestMapping("mybt")
public class BatisTest {

    @Autowired
    MyMapper myMapper;

    @RequestMapping("findAll")
    public List<MyPojo> findAllTest() {

        return myMapper.findAll();
    }

    @RequestMapping("findOne")
    public MyPojo findOne() {

        return myMapper.findOne();
    }
}
  • -- 通用mapper可以使用定义好的一些api
@Repository
public class RegisterDaoImpl implements RegisterDao {

    @Autowired
    private UserMapper userMapper;

    @Override
    public void registerUser(User user) {

        userMapper.insert(user);
        //delete...
    }
}

https://mybatis.org/mybatis-3/zh/index.html
https://apidoc.gitee.com/free/Mapper/

相关文章

网友评论

      本文标题:mybatis简易项目配置

      本文链接:https://www.haomeiwen.com/subject/ghlruktx.html