【 SpringBoot 整合 Mybatis 】—— XML方式

SpringBoot 整合 Mybatis ——XML方式

mybatis-spring-boot-starter

官方说明: MyBatis Spring-Boot-Starter will help you use MyBatis with Spring Boot

mybatis-spring-boot-starter 主要有两种解决方案:

  • 一、使用注解解决一切问题
  • 二、 XML 方式(传统方式)

导入依赖

pom.xml ,与注解版一致

application.yml 添加配置

与注解相比增加了

mybatis:
  config-location: classpath:mybatis/sqlMapConfig.xml
  mapper-locations: classpath:mybatis/mapper/*.xml
  type-aliases-package: cn.ylx.pojo

启动项添加对 mapper 包扫描 @MapperScan

package cn.ylx;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("cn.ylx.mapper")  //扫描:该包下相应的class,主要是MyBatis的持久化类
public class SpringbootMybatisApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootMybatisApplication.class, args);
    }

}

或者直接在 Mapper 类上面添加注解 @Mapper ,建议使用上面那种,不然每个 mapper 加个注解也挺麻烦的。

创建数据库

CREATE TABLE `user` (
  `id` int(11) NOT NULL,
  `age` int(11) NOT NULL,
  `name` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

创建 User

package cn.ylx.pojo;

public class User {

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

    public int getId() {
        return id;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

创建 UserMapper 接口

package cn.ylx.mapper;

import cn.ylx.pojo.User;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface UserMapper {

    @Select("select * from User where name = #{name}")
    public List<User> likeName(String name);

    @Select("select * from User where id = #{id}")
    public User getById(int id);

    @Select("select name from User where id = #{id}")
    public String getNameById(int id);

}

创建 UserService 接口

package cn.ylx.service;

import cn.ylx.mapper.UserMapper;
import cn.ylx.pojo.User;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserService {

    @Resource
    private UserMapper userMapper;

    public List<User> likeName(String name){
        List<User> users = userMapper.likeName(name);
        return users;
    }

}

创建 UserController 接口

package cn.ylx.controller;

import cn.ylx.pojo.User;
import cn.ylx.service.UserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class UserController {

    @Resource
    private UserService userService;

    @RequestMapping("/likeName")
    public List<User> likeName(String name){
        List<User> users = userService.likeName(name);
        return users;
    }

}

浏览器输入:http://localhost:8080/likeName?name=Tom
返回:

[
  {
    "id": 1,
    "age": 11,
    "name": "tom"
  },
  {
    "id": 2,
    "age": 12,
    "name": "tom"
  },
  {
    "id": 3,
    "age": 13,
    "name": "tom"
  },
  {
    "id": 4,
    "age": 14,
    "name": "tom"
  },
  {
    "id": 5,
    "age": 15,
    "name": "tom"
  },
  {
    "id": 6,
    "age": 16,
    "name": "tom"
  },
  {
    "id": 7,
    "age": 17,
    "name": "tom"
  },
  {
    "id": 8,
    "age": 18,
    "name": "tom"
  },
  {
    "id": 9,
    "age": 19,
    "name": "tom"
  },
  {
    "id": 10,
    "age": 20,
    "name": "tom"
  }
]

如果有增、删、改方法,需要加入事务,可以在 service 层添加 @Transactional 注解
例如,增加方法:

Mappper 层:

	/**
	 * 保存数据,自增长的ID
	 */
	@Insert("insert into User(name) values(#{name})")
	@Options(useGeneratedKeys=true,keyProperty="id",keyColumn="id")
	public void save(User user)

Serive 层:

	@Transactional//添加事务.
	public void save(User user){
		demoMappper.save(user);
	}

使用 pagehelper 分页插件

添加配置文件 MyBatisConfiguration

package cn.ylx.config;

import com.github.pagehelper.PageHelper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

@Configuration
public class MyBatisConfiguration {

    @Bean
    public PageHelper pageHelper() {
        System.out.println("MyBatisConfiguration.pageHelper()");
        PageHelper pageHelper = new PageHelper();
        Properties p = new Properties();
        p.setProperty("offsetAsPageNum", "true");
        p.setProperty("rowBoundsWithCount", "true");
        p.setProperty("reasonable", "true");
        pageHelper.setProperties(p);
        return pageHelper;
    }

}

在需要分页的地方添加

例如,在controller中添加

    @RequestMapping("/likeName")
    public List<User> likeName(String name){
        /*
         * 第一个参数:第几页;
         * 第二个参数:每页获取的条数.
         */
        PageHelper.startPage(1, 2);
        List<User> users = userService.likeName(name);
        return users;
    }

结果:

[
  {
    "id": 1,
    "age": 11,
    "name": "tom"
  },
  {
    "id": 2,
    "age": 12,
    "name": "tom"
  }
]

猜你喜欢

转载自blog.csdn.net/weixin_42112635/article/details/84766319