spring-boot、mybatis整合

一、MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。相信很多人在使用mybatis的时候都有自己的感受吧,mybatis的框架使用很好的提高了我们自身的开发效率,和sql编写能力。为了简化编写和配置过程,这里我介绍spring-boot和mybatis的二种整合方式。

二、我这里为了简化项目,将两种方式写在一起的,下面我会单独介绍相关的配置。首先需要的jar包(pom.xml)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.troy</groupId>
    <artifactId>springboot_mybatis</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>1.5.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.0</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.9</version>
        </dependency>
    </dependencies>
</project>

三、目录结构

四、application.yml配置

server:
  port: 8080
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/model?useUnicode=true&amp;characterEncoding=utf8
    username: root
    password: root
mybatis:
  mapper-locations: classpath:/mapper/*

五、第一种方式:采用@mapper的注解方式来实现数据库的访问

1、controller层

@RestController
@RequestMapping(value = "/api/login")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/init")
    public String init() {
        return "hello world";
    }

    @RequestMapping(value = "/findAll")
    public List<Map<String,Object>> findAll() {
        List<Map<String,Object>> list = userService.findAllMapper();
        return list;
    }

    @RequestMapping(value = "/findById")
    public Map<String,Object> findById() {
        Long id = 1L;
        Map<String,Object> map = userService.findByIdMapper(id);
        return map;
    }
}

2、service层

1)接口

public interface UserService {
    public List<Map<String,Object>> findAllMapper();
    public Map<String,Object> findByIdMapper(Long id);
}

2)实现类

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<Map<String, Object>> findAllMapper() {
        return userMapper.findAll();
    }

    @Override
    public Map<String, Object> findByIdMapper(Long id) {
        return userMapper.findById(id);
    }
}

3、mapper层

@Mapper
@Repository
public interface UserMapper {

    @Select(value = "select * from user")
    public List<Map<String,Object>> findAll();

    @Select(value = "select * from user where id=#{id}")
    public Map<String,Object> findById(Long id);
}

六、第二种方式:采用xml配置文件的方式

1、controller层

@RestController
@RequestMapping(value = "/api/login")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/init")
    public String init() {
        return "hello world";
    }

    @RequestMapping(value = "/findAll")
    public List<Map<String,Object>> findAll() {
        return userService.findAll();
    }

    @RequestMapping(value = "/findById")
    public Map<String,Object> findById() {
        Long id = 1L;
        return userService.findById(id);
    }
}

2、service层

1)接口

public interface UserService {

    public List<Map<String,Object>> findAll();
    public Map<String,Object> findById(Long id);
}

2)实现类

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public List<Map<String,Object>> findAll(){
        return userDao.findAll();
    }

    @Override
    public Map<String, Object> findById(Long id) {
        return userDao.findById(id);
    }
}

3、dao层

1)接口

@Repository
public interface UserDao {

    public List<Map<String,Object>> findAll();
    public Map<String,Object> findById(Long id);
}

2)实现类

@Repository
public class UserDaoImpl implements UserDao{

    @Autowired
    private SqlSessionTemplate sqlSession;

    @Override
    public List<Map<String, Object>> findAll() {
        return sqlSession.selectList("user.findAll");
    }

    @Override
    public Map<String, Object> findById(Long id) {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("id",id);
        return sqlSession.selectOne("user.findById",map);
    }
}

4、userMapper.xml配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
        "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">

<mapper namespace="user">
    <select id="findAll" resultType="java.util.Map">
        select * from user
    </select>

    <select id="findById" parameterType="java.util.Map" resultType="java.util.Map">
        select * from user where id=#{id}
    </select>
</mapper>

5、application.yml里面配置

mybatis:
  mapper-locations: classpath:/mapper/*

七、总结:总体来说两种使用方式都是很不错的,注解的方式在使用上面很方便,也方便管理。xml配置文件的方式在使用上面更灵活,针对于sql量比较大的方式处理比较合理。在会涉及逻辑判断的时候,xml的方式使用就会体现的更加灵活。

 

posted @ 2017-08-22 11:05  小不点丶  阅读(511)  评论(0编辑  收藏  举报