简介

MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。

看完本章你将会知道

如何在springboot里面使用mybits进行表之间的操作

配置文件

pom

<?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.boot</groupId>
    <artifactId>demo-mybits</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo-mybits</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.16</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

application.yml

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: root
    url: jdbc:mysql://127.0.0.1:3306/boot?characterEncoding=utf-8&useSSL=false
mybatis:
  mapper-locations: classpath*:mapper/*.xml

sql脚本

CREATE TABLE `tab_mybits` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `msg` varchar(255) DEFAULT NULL,
  `remark` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8mb4;

HelloMybits

项目结构

image.png

TabMybits

@Data
public class TabMybits {
    private Integer id;
    private String msg;
    private String remark;
}

TabMybitsRpository

public interface TabMybitsRpository {
    TabMybits save(TabMybits model);

    TabMybits delete(Integer id);

    TabMybits findById(Integer id);

    List<TabMybits> findAll();

    @Select("select * from tab_mybits t where  t.msg = #{msg}")
    TabMybits findTabMybits(@Param("msg") String msg);
}

TabMybitsService

public interface TabMybitsService {
    /**
     * 新增or更新
     */
    TabMybits save(TabMybits model);


    /**
     * Load查询
     */
    TabMybits findById(Integer id);


    /**
     * 查询
     */
    List<TabMybits> findAll();

    /**
     * sql注解查询
     */
    TabMybits findTabMybits(String msg);


    void delete(Integer id);
}

TabMybitsServiceImpl

@Service
public class TabMybitsServiceImpl implements TabMybitsService {
    @Autowired
    private TabMybitsRpository tabMybitsRpository;

    @Override
    public TabMybits save(TabMybits model) {
        return tabMybitsRpository.save(model);
    }

    @Override
    public TabMybits findById(Integer id) {
        return tabMybitsRpository.findById(id);
    }

    @Override
    public List<TabMybits> findAll() {
        return tabMybitsRpository.findAll();
    }

    @Override
    public TabMybits findTabMybits(String msg) {
        return tabMybitsRpository.findTabMybits(msg);
    }

    @Override
    public void delete(Integer id) {
        tabMybitsRpository.delete(id);
    }
}

DemoMybitsApplication

@SpringBootApplication
@MapperScan(basePackages = {"com.boot.repository"})
public class DemoMybitsApplication {

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

TabMybitsMapper.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.boot.repository.TabMybitsRpository">
    <resultMap id="BaseResultMap" type="com.boot.dataObject.TabMybits">
        <id column="id" jdbcType="INTEGER" property="id" />
        <result column="msg" jdbcType="VARCHAR" property="msg" />
        <result column="remark" jdbcType="VARCHAR" property="remark" />
    </resultMap>

    <select id="save" resultMap="BaseResultMap">
       INSERT INTO tab_mybits (msg,remark) VALUES (#{msg},#{remark})
    </select>
    <select id="update" resultMap="BaseResultMap">
       UPDATE tab_mybits t SET t.msg = #{msg},t.remark = #{remark} WHERE t.id = #{id};
    </select>
    <select id="delete" resultMap="BaseResultMap">
        DELETE FROM tab_mybits   WHERE id = #{id}
    </select>
    <select id="findAll" resultMap="BaseResultMap">
        select * from tab_mybits
    </select>
    <select id="findById" resultMap="BaseResultMap">
        select * from tab_mybits t where  t.id = #{id}
    </select>

</mapper>

TabMybitsServiceTest

@Slf4j
@Component
public class TabMybitsServiceTest  extends DemoMybitsApplicationTests {


    
    @Autowired
    private TabMybitsService tabMybitsService;

    @Test
    public void save() {
        TabMybits model = new TabMybits();
        model.setMsg("firstJpa");
        model.setRemark(new Date().toString());
        TabMybits save = tabMybitsService.save(model);
        log.info("save:{}",save);
    }

    @Test
    public void delete() {
        tabMybitsService.delete(10);
    }

    @Test
    public void findById() {
        TabMybits mybits = tabMybitsService.findById(11);
        log.info("mybits:{}",mybits);
    }

    @Test
    public void findAll() {
        List<TabMybits> all = tabMybitsService.findAll();
        log.info("all:{}",all);
    }

    @Test
    public void findTabMybits() {
        TabMybits ccc = tabMybitsService.findTabMybits("ccc");
        log.info("ccc:{}",ccc);

    }
}

DemoMybitsApplicationTests

@RunWith(SpringRunner.class)
@SpringBootTest
@MapperScan(basePackages = {"com.boot.repository"})
public class DemoMybitsApplicationTests {

    @Test
    public void contextLoads() {
    }

}

git地址

gitee

Last Updated: 6/13/2019, 7:10:41 PM