网站站内搜索代码,网站设计功能,wordpress文章被篡改,网站规划的认识MyBatis简介
MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache 迁移到了google code#xff0c;并且改名为MyBatis 。
集成spring boot 的时候必须在mapper接口上面标注Mapper注解
项目图片 pom.xml
只需要在pom.xml引入需要的数据库配置#xff0c;就会…MyBatis简介
MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache 迁移到了google code并且改名为MyBatis 。
集成spring boot 的时候必须在mapper接口上面标注Mapper注解
项目图片 pom.xml
只需要在pom.xml引入需要的数据库配置就会自动访问此数据库如果需要配置其他数据库可以在application.properties进行添加
默认使用org.apache.tomcat.jdbc.pool.DataSource创建连接池
project xmlnshttp://maven.apache.org/POM/4.0.0 xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdcom.jege.spring.boot/groupIdartifactIdspring-boot-mybatis/artifactIdversion0.0.1-SNAPSHOT/versionpackagingjar/packagingnamespring-boot-mybatis/nameurlhttp://maven.apache.org/url!-- 公共spring-boot配置下面依赖jar文件不用在写版本号 --parentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion1.4.1.RELEASE/versionrelativePath //parentpropertiesproject.build.sourceEncodingUTF-8/project.build.sourceEncodingjava.version1.8/java.version/propertiesdependencies!-- 持久层 --dependencygroupIdorg.mybatis.spring.boot/groupIdartifactIdmybatis-spring-boot-starter/artifactIdversion1.1.1/version/dependency!-- h2内存数据库 --dependencygroupIdcom.h2database/groupIdartifactIdh2/artifactIdscoperuntime/scope/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependency/dependenciesbuildfinalNamespring-boot-mybatis/finalNamepluginsplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-compiler-plugin/artifactIdconfigurationsource${java.version}/sourcetarget${java.version}/target/configuration/plugin/plugins/build
/project模型对象User
package com.jege.spring.boot.mybatis.entity;/*** 模型对象*/
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自动生成其实现*/
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)ListUser findAll();Select(select id,name,age from t_user where name like #{name})ListUser findByNameLike(String name);Delete(delete from t_user)void deleteAll();}
无需application.properties
测试类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;// 每次执行Test之前先删除表创建表Beforepublic void before() throws Exception {userMapper.dropTable();userMapper.createTable();}// 打印出class com.sun.proxy.$Proxy66表示spring注入通过jdk动态代理获取接口的子类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);}// 每次执行Test之后清空数据Afterpublic void destroy() throws Exception {userMapper.deleteAll();}}扫一扫获取更多相关资讯哟