资讯专栏INFORMATION COLUMN

SpringBoot五步配置Mybatis超简教程

stormgens / 845人阅读

摘要:全局启用或禁用延迟加载。当禁用时,所有关联对象都会即时加载。需要驱动器支持。如果设为了,这个设置将强制使用被生成的主键,有一些驱动器不兼容不过仍然可以执行。

第一步:Maven里面添加mybatis的引用jar包:



    org.mybatis.spring.boot
    mybatis-spring-boot-starter
    1.3.1


    com.github.pagehelper
    pagehelper-spring-boot-starter
    1.2.3


    mysql
    mysql-connector-java


    org.springframework
    spring-tx

第二步:在application.properties文件里面添加如下代码

#配置mysql数据源
spring.datasource.url=jdbc:mysql://your-mysql-url/database-name?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
spring.datasource.username=username
spring.datasource.password=password
spring.datasource.driverClassName=com.mysql.jdbc.Driver

#security.basic.enabled=false

mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl


## Mybatis 配置
mybatis.type-aliases-package=com.xfind.core.entity.xianyu
mybatis.mapper-locations=classpath:mapper/*.xml
#使全局的映射器启用或禁用缓存。
mybatis.configuration.cache-enabled=true
#全局启用或禁用延迟加载。当禁用时,所有关联对象都会即时加载。
mybatis.configuration.lazy-loading-enabled=true
#当启用时,有延迟加载属性的对象在被调用时将会完全加载任意属性。否则,每种属性将会按需要加载。
mybatis.configuration.aggressive-lazy-loading=true
#是否允许单条sql 返回多个数据集  (取决于驱动的兼容性) default:true
mybatis.configuration.multiple-result-sets-enabled=true
#是否可以使用列的别名 (取决于驱动的兼容性) default:true
mybatis.configuration.use-column-label=true
#允许JDBC 生成主键。需要驱动器支持。如果设为了true,这个设置将强制使用被生成的主键,有一些驱动器不兼容不过仍然可以执行。  default:false
mybatis.configuration.use-generated-keys=true
#指定 MyBatis 如何自动映射 数据基表的列 NONE:不隐射u3000PARTIAL:部分  FULL:全部
mybatis.configuration.auto-mapping-behavior=partial
#这是默认的执行类型  (SIMPLE: 简单; REUSE: 执行器可能重复使用prepared statements语句;BATCH: 执行器可以重复执行语句和批量更新)
mybatis.configuration.default-executor-type=simple
#使用驼峰命名法转换字段。
mybatis.configuration.map-underscore-to-camel-case=true
#设置本地缓存范围 session:就会有数据的共享  statement:语句范围 (这样就不会有数据的共享 ) defalut:session
mybatis.configuration.local-cache-scope=session
#设置但JDBC类型为空时,某些驱动程序 要指定值,default:OTHER,插入空值时不需要指定类型
mybatis.configuration.jdbc-type-for-null=null
#如果数据为空的字段,则该字段省略不显示,可以通过添加配置文件,规定查询数据为空是则返回null。
mybatis.configuration.call-setters-on-nulls=true

第三步:设置启动类:

//开启定时任务
//@EnableScheduling
@SpringBootApplication
@EnableTransactionManagement//开启事务管理
@MapperScan("com.xfind.core.mybatis")//与dao层的@Mapper二选一写上即可(主要作用是扫包)
public class StartUp {
    public static void main(String[] args) {
        SpringApplication.run(StartUp.class, args);
    }
}

第四步:添加mapper文件和编写dao代码以及service和controller代码,
1、我是在core的modules里面的resources文件夹下新建mapper文件夹,下面保存所有数据库访问的sql。
2、新建实体类,我是在entity文件夹下创建的
2、在dao层下新建mapper里面的方法
3、在service层新建调用dao层类的逻辑代码
4、在controller层新建调用service层的逻辑代码

UserMapper.xml




    

User.java

package com.xfind.core.entity.xianyu;

import com.fasterxml.jackson.annotation.JsonIgnore;

import java.util.Date;

/**
 * Created by zhangwei on 2018/6/1.
 */
public class User {
    private String id;
    private String username;
    private String phone;
    private String email;
    @JsonIgnore
    private String password;
    private String ip;
    private String mac;
    private int type;
    private int delFlag;
    private String memo;
    private Date lastPasswordResetDate;

    private Date lastLoginDate;

    private int iosTest;

    private Date createdDt;

    private Date updatedDt;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public String getMac() {
        return mac;
    }

    public void setMac(String mac) {
        this.mac = mac;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public int getDelFlag() {
        return delFlag;
    }

    public void setDelFlag(int delFlag) {
        this.delFlag = delFlag;
    }

    public String getMemo() {
        return memo;
    }

    public void setMemo(String memo) {
        this.memo = memo;
    }

    public Date getLastPasswordResetDate() {
        return lastPasswordResetDate;
    }

    public void setLastPasswordResetDate(Date lastPasswordResetDate) {
        this.lastPasswordResetDate = lastPasswordResetDate;
    }

    public Date getLastLoginDate() {
        return lastLoginDate;
    }

    public void setLastLoginDate(Date lastLoginDate) {
        this.lastLoginDate = lastLoginDate;
    }

    public int getIosTest() {
        return iosTest;
    }

    public void setIosTest(int iosTest) {
        this.iosTest = iosTest;
    }

    public Date getCreatedDt() {
        return createdDt;
    }

    public void setCreatedDt(Date createdDt) {
        this.createdDt = createdDt;
    }

    public Date getUpdatedDt() {
        return updatedDt;
    }

    public void setUpdatedDt(Date updatedDt) {
        this.updatedDt = updatedDt;
    }
}

UserDao.java

@Repository
public interface UserDao {
    List findAllUser();
}

UserService.java

public interface XyUserService {
    List selectUsers();
}

UserServiceImpl.java

@Service
public class XyUserServiceImpl implements XyUserService {

    @Autowired
    UserDao userDao;

    @Override
    public List selectUsers() {
        return userDao.findAllUser();
    }
}

UserController.java

@RestController
@RequestMapping("/xianyu")
public class UserController {

    @Autowired
    XyUserServiceImpl xyUserService;

    @GetMapping("/user")
    public ResponseEntity getUsers(){
        List users = xyUserService.selectUsers();
        return ResponseEntity.ok(users);
    }
}

第五步:访问试试是否已经设置成功并返回数据

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/69605.html

相关文章

  • Maven管理SpringBoot Profile

    摘要:的配置文件默认为或,此外仅以配置为说明。的由的标签管理。管理由于构建是基于或,此处仅以说明。管理分五步,以下详细介绍。并且为表示,会将文件内容的替换为相应的变量如文件中的会替换为属性值。 1. Spring Profile Spring可使用Profile决定程序在不同环境下执行情况,包含配置、加载Bean、依赖等。 Spring的Profile一般项目包含:dev(开发), test...

    wenzi 评论0 收藏0
  • springboot(二)——springboot自动配置解析

    摘要:前言用过的肯定很熟悉,它其中有个重要的特性,就是自动配置平时习惯的一些设置的配置作为默认配置。提倡无配置文件的理念,使用生成的应用完全不会生成任何配置代码与配置文件。 前言 用过springboot的肯定很熟悉,它其中有个重要的特性,就是自动配置(平时习惯的一些设置的配置作为默认配置)。springboot提倡无XML配置文件的理念,使用springboot生成的应用完全不会生成任何配...

    张率功 评论0 收藏0
  • 一份最中肯的Java学习路线+资源分享(拒绝傻逼式分享)

    摘要:因为某些原因,不方便在这里直接发送百度链接,关注我的微信公众号面试通关手册回复资源分享第一波即可领取。然后大家还有什么问题的话,可以在我的微信公众号后台面试通关手册给我说或者加我微信,我会根据自己的学习经验给了说一下自己的看法。 这是一篇针对Java初学者,或者说在Java学习路线上出了一些问题(不知道该学什么、不知道整体的学习路线是什么样的) 第一步:Java基础(一个月左右) 推荐...

    hearaway 评论0 收藏0
  • SpringBoot 实战 (十三) | 整合 MyBatis (XML 版)

    摘要:如要运行多次,请把上次生成的映射文件代码删除再运行。层启动类扫描接口,必须加上提一嘴,这个注解非常的关键,这个对应了项目中所对应的包路径,必须加上,否则会导致异常。另外,关注之后在发送可领取免费学习资料。 微信公众号:一个优秀的废人如有问题或建议,请后台留言,我会尽力解决你的问题。 前言 如题,今天介绍 SpringBoot 与 Mybatis 的整合以及 Mybatis 的使用,之前...

    _Zhao 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<