當(dāng)前位置:
首頁(yè) >
前端技术
> javascript
>内容正文
javascript
Spring Boot MyBatis
生活随笔
收集整理的這篇文章主要介紹了
Spring Boot MyBatis
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
MyBatis簡(jiǎn)介
MyBatis 本是apache的一個(gè)開(kāi)源項(xiàng)目iBatis, 2010年這個(gè)項(xiàng)目由apache 遷移到了google code,并且改名為MyBatis 。
集成spring boot 的時(shí)候必須在mapper接口上面標(biāo)注@Mapper注解
項(xiàng)目圖片
pom.xml
只需要在pom.xml引入需要的數(shù)據(jù)庫(kù)配置,就會(huì)自動(dòng)訪(fǎng)問(wèn)此數(shù)據(jù)庫(kù),如果需要配置其他數(shù)據(jù)庫(kù),可以在application.properties進(jìn)行添加
默認(rèn)使用org.apache.tomcat.jdbc.pool.DataSource創(chuàng)建連接池
<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配置,下面依賴(lài)jar文件不用在寫(xiě)版本號(hào) --><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內(nèi)存數(shù)據(jù)庫(kù) --><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>模型對(duì)象User
package com.jege.spring.boot.mybatis.entity;/*** 模型對(duì)象*/ 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;} }持久層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;/*** 持久層接口,由spring自動(dòng)生成其實(shí)現(xiàn)*/ @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();}無(wú)需application.properties
測(cè)試類(lèi)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;@RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest() public class UserMapperTest {@AutowiredUserMapper userMapper;// 每次執(zhí)行Test之前先刪除表,創(chuàng)建表@Beforepublic void before() throws Exception {userMapper.dropTable();userMapper.createTable();}// 打印出class com.sun.proxy.$Proxy66表示spring注入通過(guò)jdk動(dòng)態(tài)代理獲取接口的子類(lèi)@Testpublic void proxy() throws Exception {System.out.println(userMapper.getClass());}@Testpublic void save() throws Exception {for (int i = 0; i < 10; i++) {User user = new User("jege" + i, 25 + i);userMapper.insert(user);}}@Testpublic void all() throws Exception {save();assertThat(userMapper.findAll()).hasSize(10);}@Testpublic void findByName() throws Exception {save();assertThat(userMapper.findByNameLike("jege%")).hasSize(10);}// 每次執(zhí)行Test之后清空數(shù)據(jù)@Afterpublic void destroy() throws Exception {userMapper.deleteAll();}}掃一掃獲取更多相關(guān)資訊喲!!!
總結(jié)
以上是生活随笔為你收集整理的Spring Boot MyBatis的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: C++布尔类型
- 下一篇: Java时间日期的处理