spring boot--mybatis

spring boot项目使用mybatis作为持久层的步骤如下:

1.pom.xml添加依赖

<!--mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>
        <!--数据库驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.44</version>
        </dependency>

2.application.properties文件配置相关信息

#配置数据库链接信息
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/growth
spring.datasource.username= root
spring.datasource.password= admin

#配置执行sql是打印的路径及级别
logging.level.com.MrXu.growth=DEBUG

#mybatis的配置文件及mapper位置配置
mybatis.config-locations=classpath:mybatis.xml
mybatis.mapper-locations=classpath:mapper/**/*.xml

其中的 mybatis的配置文件及mapper位置配置根据个人需要,我使用的是XML配置所以我需要添加。如果你不需要xml,而是用@select,@insert等注解就不需要添加。我测试过即使添加也没有关系

使用mybatis中遇到表中字段与实体类属性不一致解决办法:

<resultMap type="com.MrXu.growth.sys.entity.SysUser" id="SysUserMap">
        <!-- 主键映射 -->
        <id column="id" property="id" />

        <!-- 普通属性映射 -->
        <result column="user_name" property="userName" />
        <result column="password" property="password"  />
        <result column="login_code" property="loginCode" />
        <result column="phone" property="phone" />
        <result column="email" property="email" />
    </resultMap>

    <!-- resultMap解决数据库列名与javabean属性名不相同不能映射的状况-->
    <select id="getByLoginCode" resultMap="SysUserMap" >
        select * from sys_user where login_code=#{loginCode}
    </select>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值