SpringBoot和MyBatis集成案例(学习笔记)

来源:互联网 发布:一句一句学古兰经软件 编辑:程序博客网 时间:2024/05/21 22:25

声明:本案例学习http://blog.csdn.net/je_ge,在此感谢je_ge提供的学习用的资料

1、项目目录结构

这里写图片描述

2、pom.xml的内容如下

<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.jege.spring.boot</groupId>    <artifactId>spring-boot-mybatis</artifactId>    <version>1.0.0.RELEASE</version>    <packaging>jar</packaging>    <name>spring-boot-mybatis</name>    <url>http://blog.csdn.net/je_ge</url>    <developers>        <developer>            <id>je_ge</id>            <name>je_ge</name>            <email>1272434821@qq.com</email>            <url></url>            <timezone>8</timezone>        </developer>    </developers>    <!-- 公共spring-boot配置,下面依赖jar文件不用在写版本号 -->    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.4.1.RELEASE</version>        <relativePath />    </parent>    <properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <java.version>1.8</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-test</artifactId>            <scope>test</scope>        </dependency>    </dependencies>    <build>        <finalName>spring-boot-mybatis</finalName>        <plugins>            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-compiler-plugin</artifactId>                <configuration>                    <source>${java.version}</source>                    <target>${java.version}</target>                </configuration>            </plugin>        </plugins>    </build></project>

3、User的内容

package com.jege.spring.boot.mybatis.entity;/** * @author JE哥 * @email 1272434821@qq.com * @description:模型对象 */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;  }  public Long getId() {    return id;  }  public void setId(Long id) {    this.id = id;  }  public String getName() {    return name;  }  public void setName(String name) {    this.name = name;  }  public Integer getAge() {    return age;  }  public void setAge(Integer age) {    this.age = age;  }}

4、UserMapper的内容

package com.jege.spring.boot.mybatis.mapper;import java.util.List;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 com.jege.spring.boot.mybatis.entity.User;/** * @author JE哥 * @email 1272434821@qq.com * @description:持久层接口,由spring自动生成其实现 */@Mapperpublic interface UserMapper {  @Delete("drop table t_user if exists")  void dropTable();  @Insert("create table t_user (id bigint generated by default as identity, age integer, name varchar(255), primary key (id))")  void createTable();  @Insert("insert into t_user(name,age) values(#{name},#{age})")  void insert(User user);  @Select("select id,name,age from t_user")  List<User> findAll();  @Select("select id,name,age from t_user where name like #{name}")  List<User> findByNameLike(String name);  @Delete("delete from t_user")  void deleteAll();}

5、Application的内容

package com.jege.spring.boot;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;/** * @author JE哥 * @email 1272434821@qq.com * @description:spring boot 启动类 */@SpringBootApplicationpublic class Application {  public static void main(String[] args) {    SpringApplication.run(Application.class, args);  }}

6、UserMapperTest内容

package com.jege.spring.boot.mybatis;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;import com.jege.spring.boot.mybatis.entity.User;import com.jege.spring.boot.mybatis.mapper.UserMapper;/** * @author JE哥 * @email 1272434821@qq.com * @description: */@RunWith(SpringJUnit4ClassRunner.class)@SpringBootTest()public class UserMapperTest {  @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("jege" + 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("jege%")).hasSize(10);  }  // 每次执行Test之后清空数据  @After  public void destroy() throws Exception {    userMapper.deleteAll();  }}
原创粉丝点击