Springboot集成Mybatis之注解篇

来源:互联网 发布:网站源码小偷怎么用 编辑:程序博客网 时间:2024/06/06 17:01

Springboot集成Mybatis与 Springboot实现JPA
类似。
项目结构图如下:
这里写图片描述
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.zhenqi</groupId>    <artifactId>springboot-mybatis</artifactId>    <version>0.0.1-SNAPSHOT</version>    <packaging>jar</packaging>    <name>springboot-mybatis</name>    <description>Demo project for Spring Boot</description>    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.5.4.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.7</java.version>    </properties>    <dependencies>        <!-- 持久层 -->        <dependency>            <groupId>org.mybatis.spring.boot</groupId>            <artifactId>mybatis-spring-boot-starter</artifactId>            <version>1.1.1</version>        </dependency>        <!-- h2内存数据库 -->        <dependency>            <groupId>com.h2database</groupId>            <artifactId>h2</artifactId>            <scope>runtime</scope>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-test</artifactId>            <scope>test</scope>        </dependency>    </dependencies>    <build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>            </plugin>        </plugins>    </build></project>

创建实体类 User.java

package com.zhenqi.study.entity;/** * Created by wuming on 2017/7/19. */public class User {    private Long id;    private String name;    private Integer age;    public User() {    }    public User(String name, Integer age) {        this.name = name;        this.age = age;    }}

与实体类相关的Mapper接口,UserMapper.java

package com.zhenqi.study.mapper;import com.zhenqi.study.entity.User;import org.apache.ibatis.annotations.Delete;import org.apache.ibatis.annotations.Insert;import org.apache.ibatis.annotations.Mapper;import org.apache.ibatis.annotations.Select;import java.util.List;/** * Created by wuming on 2017/7/19. */@Mapperpublic interface UserMapper {    @Delete("drop table user if exists")    void dropTable();    @Insert("create table user (id bigint generated by default as identity, age integer, name varchar(255), primary key (id))")    void createTable();    @Insert("insert into user(name,age) values(#{name},#{age})")    void insert(User user);    @Select("select id,name,age from user")    List<User> findAll();    @Select("select id,name,age from user where name like #{name}")    List<User> findByNameLike(String name);    @Delete("delete from user")    void deleteAll();}

测试类

package com.zhenqi.study;import com.zhenqi.study.entity.User;import com.zhenqi.study.mapper.UserMapper;import static org.assertj.core.api.Assertions.assertThat;import org.junit.After;import org.junit.Before;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)@SpringBootTestpublic class SpringbootMybatisApplicationTests {    @Autowired    UserMapper userMapper;    // 每次执行Test之前先删除表,创建表    @Before    public void before() throws Exception {        userMapper.dropTable();        userMapper.createTable();    }    // 打印出class com.sun.proxy.$Proxy66表示spring注入通过jdk动态代理获取接口的子类    @Test    public void proxy() throws Exception {        System.out.println(userMapper.getClass());    }    @Test    public void save() throws Exception {        for (int i = 0; i < 10; i++) {            User user = new User("zhenqi" + i, 25 + i);            userMapper.insert(user);        }    }    @Test    public void all() throws Exception {        save();        assertThat(userMapper.findAll()).hasSize(10);    }    @Test    public void findByName() throws Exception {        save();        assertThat(userMapper.findByNameLike("zhen%")).hasSize(10);    }    // 每次执行Test之后清空数据    @After    public void destroy() throws Exception {        userMapper.deleteAll();    }}

这个不需要配置文件 application.properties。
这里写图片描述

原创粉丝点击