springboot+mybatis自动映射器搭建IDEA项目

关于mybatis自动映射器,首先推荐这位仁兄的博客。
https://www.cnblogs.com/yunquan/p/10723086.html#4327407
由于本人基本是纯小白,很多知识涉猎较浅,捣鼓了大半天看了一堆教程还有官方文档都一知半解。这篇博客讲的很简单易懂,对新手十分友好。

1、一开始的项目框架应该是只有启动类,和Springboot的一些自带文件。需要自己建一个表(开始的时候可以先不写记录,只要搭建出表的字段即可),在properties里面将相关数据库的信息配置好,这里我用的是mysql。

mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/miaosha?useUnicode=true&characterEncoding=gbk
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

2、按照上面的博客将mybatis-config.xml文件以及相关依赖配置好,利用maven生成对应的映射文件。这里必须吹一波彩虹屁,mybatis真的好用,不用自己写代码感觉真爽。
pom里添加的依赖和plugin有:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.41</version>
</dependency>
<dependency>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-core</artifactId>
    <version>1.3.7</version>
</dependency>

<plugin>
    <!--Mybatis-generator插件,用于自动生成Mapper和POJO-->
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.7</version>
    <configuration>
        <!--配置文件的位置-->
        <configurationFile>src/main/resources/mybatis-config.xml</configurationFile>
        <verbose>true</verbose>
        <overwrite>true</overwrite>
    </configuration>
    <executions>
        <execution>
            <id>Generate MyBatis Artifacts</id>
            <goals>
                <goal>generate</goal>
            </goals>
        </execution>
    </executions>
    <!--加入下面这个依赖-->
    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.41</version>
        </dependency>
    </dependencies>
</plugin>

注意对应的版本号,不然容易出错。这里我用的mysql是8以下的,所以上面properties里面的数据库驱动是com.mysql.jdbc.Driver,而不是com.mysql.cj.jdbc.Driver。

3、这时候启动完自动生成器,整个项目结构应该是DAO层和entity层都生成好了。你只需要再将controller层和service层写完即可。

4、写的过程中遇到了几个比较头疼的问题,网上搜了以下还挺多人遇到的,现在已经解决了,所以这里也记录一下。
(1)遇到报错:

Description: Field userMapper in com.yxf.demo.service.impl.UserServiceImpl
required a bean of type 'com.yxf.demo.dao.IUserMapperDao’that could not be found.
The injection point has the following annotations:
@org.springframework.beans.factory.annotation.Autowired(required=true)
Action: Consider defining a bean of type ‘com.yxf.demo.dao.IUserMapperDao’ in your configuration.

解决办法参照:https://blog.csdn.net/yang1393214887/article/details/95816191

(2)如果你中间因为什么原因想要重新生成以下映射文件之类的记得把原来的东西给删掉,然后再启动maven,不然也容易出错。
(3)这里@Autowired注解下的对象容易飘红。

Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    public User selectByPrimaryKey(Integer userId) {
        return userMapper.selectByPrimaryKey(userId);
    }
}  

如果有这个错误,只需要在Mapper接口类上加上@Component注解即可。好像飘红也是不影响运行的,但看着毕竟难受嘛哈哈哈

@Component
public interface UserMapper {
    int deleteByPrimaryKey(Integer userId);
    int insert(User record);
    int insertSelective(User record);
    User selectByPrimaryKey(Integer userId);
    int updateByPrimaryKeySelective(User record);
    int updateByPrimaryKey(User record);
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值