MyBatis

MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache 迁移到了google code,并且改名为MyBatis 。2013年11月迁移到Github。

集成spring boot 的时候必须在mapper接口上面标注@Mapper注解

项目图片

pom.xml

-只需要在pom.xml引入需要的数据库配置,就会自动访问此数据库,如果需要配置其他数据库,可以在application.properties进行添加

-默认使用org.apache.tomcat.jdbc.pool.DataSource创建连接池

<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>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>spring-boot-mybatis</name>
<url>http://maven.apache.org</url> <!-- 公共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>

User.java

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;
}
}

UserMapper.java

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自动生成其实现
*/
@Mapper
public 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(); }

Application.java

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 启动类
*/ @SpringBootApplication
public class Application { public static void main(String[] args) {
SpringApplication.run(Application.class, args);
} }

不需要application.properties

UserMapperTest.java

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();
} }

源码地址

https://github.com/je-ge/spring-boot

如果觉得我的文章对您有帮助,请予以打赏。您的支持将鼓励我继续创作!谢谢!



最新文章

  1. Net作业调度(四)—quartz.net持久化和集群
  2. Windows 安装Kafka
  3. XE2 泛型练习1
  4. freemarker 数字格式化函数
  5. Spark Programming--Actions
  6. 爬虫技术(四)-- 简单爬虫抓取示例(附c#代码)
  7. 1、elasticsearch简介
  8. JS 遍历对象 jQuery遍历对象
  9. Android初级教程理论知识(第九章多媒体编程)
  10. [Usaco2005 nov]Grazing on the Run 边跑边吃草 BZOJ1742
  11. AI - TensorFlow - 示例02:影评文本分类
  12. jstack命令
  13. Python之 操作 MySQL 数据库
  14. Thread中的一些方法
  15. word&amp;excel&amp;ppt文档加密方式
  16. d3 选择器
  17. 源码编译安装keepalived
  18. shell脚本中,将所有的参数值否赋给一个变量或者说将所有的参数合成一个字符串,获取所有参数
  19. java笔记--String类格式化当天日期转换符文档
  20. Executor / Executors / ExecutorService /

热门文章

  1. 4、MySql的存储过程
  2. kettle连接Hbase中数据导出(7)
  3. CPU高问题排查
  4. 洛谷-哥德巴赫猜想(升级版)-BOSS战-入门综合练习1
  5. robotium和appium的一些区别
  6. 解决curl中errno为51和60的错误
  7. 通过httplib2 探索的学习的最佳方式
  8. Spring框架--AOP编程
  9. swift 手势之UIPanGestureRecognizer
  10. MapReduce初级案例