springboot项目搭建0050
springboot项目搭建0050
项目地址:.git
摘要:mybatis是优秀的持久化框架,但是引入mybatis后还要频繁的写增删改查,很不方便,所以这边引入刘增辉老师的通用mapper,通用mapper封装了单表基本上所有的操作。
1、加入依赖
mybatis及mapper的版本参数
<mybatis.version>1.3.2</mybatis.version><mapper.version>2.0.3-beta1</mapper.version><pagehelper.version>1.2.10</pagehelper.version>
<!--Mysql--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><!--mybatis--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>${mybatis.version}</version></dependency><!--mapper--><dependency><groupId>tk.mybatis</groupId><artifactId>mapper-spring-boot-starter</artifactId><version>${mapper.version}</version></dependency><!--pagehelper--><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper-spring-boot-starter</artifactId><version>${pagehelper.version}</version></dependency>
2、com.honor.config包下配置自己的mapper接口,以后自己写的mapper继承这个接口就可以了
import tk.mybatis.mapper.common.MySqlMapper;/*** 继承自己的MyMapper** @author liuzh* @since 2015-09-06 21:53*/
public interface Mapper2<T> extends tk.mybatis.mapper.common.Mapper<T>, MySqlMapper<T> {
}
3、资源文件配置通用mapper
#通用mapper
mapper.mappers=com.honor.config.Mapper2
mapper.notEmpty=true
#pagehelper
restart.include.mapper=/mapper-[\\w-\\.]+jar
restart.include.pagehelper=/pagehelper-[\\w-\\.]+jar
#mybatis
mybatis.mapper-locations=classpath:mapper/*.xml,classpath:mapper/*/*.xml
#配置驼峰转下划线
mybatis.configuration.map-underscore-to-camel-case=true
4、com.honor.mapper包下建立自己的mapper-SysUserMapper
import com.honor.config.Mapper2;
import com.honor.model.SysUser;/*** Created by rongyaowen* on 2019/1/3.*/
public interface SysUserMapper extends Mapper2<SysUser> {
}
5、SpringbootDemoApplication启动配置mapper扫描
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;/*** Created by rongyaowen* on 2019/1/3.*/
@SpringBootApplication
@MapperScan(basePackages = {"com.honor.mapper"})
public class SpringbootDemoApplication {public static void main(String[] args) {SpringApplication.run(SpringbootDemoApplication.class);}
}
一定注意,这里的MapperScan是tk包下的,不是spring的。
6、测试
import com.honor.mapper.SysUserMapper;
import com.honor.model.SysUser;
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.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;import java.util.List;/*** Created by rongyaowen* on 2019/1/3.*/
@RunWith(SpringRunner.class)
@SpringBootTest
@WebAppConfiguration
public class Mapper2Test {@Autowiredprivate SysUserMapper sysUserMapper;@Testpublic void selectTest() {List<SysUser> sysUsers = sysUserMapper.selectAll();for (SysUser sysUser : sysUsers) {System.out.println(sysUser);}}}
7、结果
8、配置日志级别打印出sql详细信息
如果是logback日志,直接在资源文件中配置,设置mapper包为trace
logging.level.com.honor.mapper=trace
如果是log4j,在log4j.properties设置
log4j.logger.com.honor.mapper = trace
结果
最新文章
- 俞敏洪大学经典励志演讲:像树一样活着
- WinRAR 3.933.92 的注册码(已经测试)
- win rar如何注册破解
- Application.platform 平台
- 关于C语言编程中include的用法
- 程序与软件的易用性
- 语音识别之HTK入门(四)——HCompV源码解析
- anchors.fill和anchors.centerIn区别
- Web兼容性测试的要点
- 函数popen()
- JAVA关于Calendar类的使用
- winform窗体控件
- IDEA将项目打包成jar包
- Java Integer值范围问题
- MFC链表CList类
- supersqli(SQL注入流程及常用SQL语句)
- C语言程序设计之通讯录